Skip to content

Commit

Permalink
Add Ordering Filter for Discharge Patients Viewset Class (#2074)
Browse files Browse the repository at this point in the history
* Add Ordering Filter for Discharge Patients Viewset Class

* add all fields

* add tests

* reduce the filter fields

---------

Co-authored-by: Vignesh Hari <[email protected]>
  • Loading branch information
Pranshu1902 and vigneshhari authored Apr 22, 2024
1 parent 690b861 commit 1205f51
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
13 changes: 12 additions & 1 deletion care/facility/api/viewsets/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,11 @@ class FacilityDischargedPatientViewSet(GenericViewSet, mixins.ListModelMixin):
permission_classes = (IsAuthenticated, DRYPermissions)
lookup_field = "external_id"
serializer_class = PatientListSerializer
filter_backends = (filters.DjangoFilterBackend,)
filter_backends = (
filters.DjangoFilterBackend,
rest_framework_filters.OrderingFilter,
PatientCustomOrderingFilter,
)
filterset_class = FacilityDischargedPatientFilterSet
queryset = PatientRegistration.objects.select_related(
"local_body",
Expand All @@ -625,6 +629,13 @@ class FacilityDischargedPatientViewSet(GenericViewSet, mixins.ListModelMixin):
"created_by",
)

ordering_fields = [
"id",
"name",
"created_date",
"modified_date",
]

def get_queryset(self) -> QuerySet:
qs = super().get_queryset()
return qs.filter(
Expand Down
35 changes: 35 additions & 0 deletions care/facility/tests/test_patient_and_consultation_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def setUpTestData(cls) -> None:
user_type=15,
)
cls.patient = cls.create_patient(cls.district, cls.remote_facility)
cls.patient1 = cls.create_patient(cls.district, cls.remote_facility)

def list_patients(self, **kwargs):
return self.client.get("/api/v1/patient/", data=kwargs)
Expand Down Expand Up @@ -78,6 +79,40 @@ def discharge(self, consultation, **kwargs):
format="json",
)

def test_discharge_patient_ordering_filter(self):
consultation1 = self.create_consultation(
self.patient,
self.home_facility,
suggestion="A",
encounter_date=make_aware(datetime.datetime(2024, 1, 3)),
)
consultation2 = self.create_consultation(
self.patient1,
self.home_facility,
suggestion="A",
encounter_date=make_aware(datetime.datetime(2024, 1, 1)),
)
self.discharge(consultation1, discharge_date="2024-01-04T00:00:00Z")
self.discharge(consultation2, discharge_date="2024-01-02T00:00:00Z")

# order by reverse modified date
patients_order = [self.patient1, self.patient]
response = self.client.get(
f"/api/v1/facility/{self.home_facility.external_id}/discharged_patients/?ordering=-modified_date",
)
response = response.json()["results"]
for i in range(len(response)):
self.assertEqual(str(patients_order[i].external_id), response[i]["id"])

# order by modified date
patients_order = patients_order[::-1]
response = self.client.get(
f"/api/v1/facility/{self.home_facility.external_id}/discharged_patients/?ordering=modified_date",
)
response = response.json()["results"]
for i in range(len(response)):
self.assertEqual(str(patients_order[i].external_id), response[i]["id"])

def test_patient_consultation_access(self):
# In this test, a patient is admitted to a remote facility and then later admitted to a home facility.

Expand Down

0 comments on commit 1205f51

Please sign in to comment.