Skip to content

Commit

Permalink
Fix patient viewset distinct issue (#1953)
Browse files Browse the repository at this point in the history
* Distinct on the ordering field if ordering present for Patient Viewset Queryset

* Add tests

* fix typo
  • Loading branch information
rithviknishad committed Mar 7, 2024
1 parent 56f2357 commit bd161ef
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions care/facility/api/viewsets/patient.py
Expand Up @@ -277,7 +277,7 @@ def filter_queryset(self, request, queryset, view):
q_filters |= Q(consultations__facility__id__in=allowed_facilities)
q_filters |= Q(last_consultation__assigned_to=request.user)
q_filters |= Q(assigned_to=request.user)
queryset = queryset.filter(q_filters).distinct("id")
queryset = queryset.filter(q_filters)
return queryset

def filter_list_queryset(self, request, queryset, view):
Expand Down Expand Up @@ -314,7 +314,7 @@ def filter_queryset(self, request, queryset, view):
)
).order_by(ordering)

return queryset
return queryset.distinct(ordering.lstrip("-") if ordering else "id")


@extend_schema_view(history=extend_schema(tags=["patient"]))
Expand Down
17 changes: 17 additions & 0 deletions care/facility/tests/test_patient_and_consultation_access.py
Expand Up @@ -52,6 +52,9 @@ def setUpTestData(cls) -> None:
)
cls.patient = cls.create_patient(cls.district, cls.remote_facility)

def list_patients(self, **kwargs):
return self.client.get("/api/v1/patient/", data=kwargs)

def retrieve_patient(self, patient):
return self.client.get(f"/api/v1/patient/{patient.external_id}/")

Expand Down Expand Up @@ -90,6 +93,10 @@ def test_patient_consultation_access(self):
self.client.force_authenticate(user=self.remote_doctor)
res = self.retrieve_discharged_patients_of_facility(self.remote_facility)
self.assertNotContains(res, self.patient.external_id)
res = self.list_patients(
is_active="true", ordering="-last_consultation__current_bed__bed__name"
)
self.assertContains(res, self.patient.external_id)
res = self.retrieve_patient(self.patient)
self.assertEqual(res.status_code, status.HTTP_200_OK)
res = self.retieve_patient_consultations(self.patient)
Expand All @@ -114,6 +121,12 @@ def test_patient_consultation_access(self):
self.client.force_authenticate(user=self.remote_doctor)
res = self.discharge(remote_consultation, discharge_date="2024-01-02T00:00:00Z")
self.assertEqual(res.status_code, status.HTTP_200_OK)
res = self.list_patients(is_active="true", ordering="created_date")
self.assertNotContains(res, self.patient.external_id)
res = self.list_patients(
is_active="false", ordering="-last_consultation__current_bed__bed__name"
)
self.assertContains(res, self.patient.external_id)

# Admit to home facility
self.client.force_authenticate(user=self.home_doctor)
Expand All @@ -129,6 +142,8 @@ def test_patient_consultation_access(self):
res = self.retrieve_discharged_patients_of_facility(self.remote_facility)
self.assertContains(res, self.patient.external_id)
res = self.retrieve_patient(self.patient)
res = self.list_patients(is_active="true", ordering="-category_severity")
self.assertContains(res, self.patient.external_id)
self.assertEqual(res.status_code, status.HTTP_200_OK)
res = self.retieve_patient_consultations(self.patient)
self.assertContains(res, remote_consultation.external_id)
Expand All @@ -145,6 +160,8 @@ def test_patient_consultation_access(self):
# Discharge the patient from home facility.
res = self.discharge(home_consultation, discharge_date="2024-01-04T00:00:00Z")
self.assertEqual(res.status_code, status.HTTP_200_OK)
res = self.list_patients(is_active="false", ordering="name")
self.assertContains(res, self.patient.external_id)
res = self.retrieve_discharged_patients_of_facility(self.home_facility)
self.assertContains(res, self.patient.external_id)
self.assertContains(res, home_consultation.external_id)
Expand Down

0 comments on commit bd161ef

Please sign in to comment.