From 1205f51e345adcb759d401368eb358ecc0b88212 Mon Sep 17 00:00:00 2001 From: Pranshu Aggarwal <70687348+Pranshu1902@users.noreply.github.com> Date: Tue, 23 Apr 2024 00:25:08 +0530 Subject: [PATCH] Add Ordering Filter for Discharge Patients Viewset Class (#2074) * Add Ordering Filter for Discharge Patients Viewset Class * add all fields * add tests * reduce the filter fields --------- Co-authored-by: Vignesh Hari --- care/facility/api/viewsets/patient.py | 13 ++++++- .../test_patient_and_consultation_access.py | 35 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/care/facility/api/viewsets/patient.py b/care/facility/api/viewsets/patient.py index 293165d8c7..7835435d29 100644 --- a/care/facility/api/viewsets/patient.py +++ b/care/facility/api/viewsets/patient.py @@ -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", @@ -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( diff --git a/care/facility/tests/test_patient_and_consultation_access.py b/care/facility/tests/test_patient_and_consultation_access.py index 7235602695..b3decb5d63 100644 --- a/care/facility/tests/test_patient_and_consultation_access.py +++ b/care/facility/tests/test_patient_and_consultation_access.py @@ -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) @@ -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.