Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds functionality for last_consultation_admitted_bed_type_list in discharge patient filters #2204

Merged
merged 23 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion care/facility/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from care.facility.models.ambulance import Ambulance, AmbulanceDriver
from care.facility.models.asset import Asset
from care.facility.models.bed import AssetBed, Bed
from care.facility.models.bed import AssetBed, Bed, ConsultationBed
from care.facility.models.file_upload import FileUpload
from care.facility.models.patient_consultation import (
PatientConsent,
Expand Down Expand Up @@ -214,6 +214,7 @@ class FacilityUserAdmin(DjangoQLSearchMixin, admin.ModelAdmin, ExportCsvMixin):
admin.site.register(AssetBed)
admin.site.register(Asset)
admin.site.register(Bed)
admin.site.register(ConsultationBed)
admin.site.register(PatientConsent)
admin.site.register(FileUpload)
admin.site.register(PatientConsultation)
91 changes: 69 additions & 22 deletions care/facility/api/viewsets/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
ShiftingRequest,
)
from care.facility.models.base import covert_choice_dict
from care.facility.models.bed import AssetBed
from care.facility.models.bed import AssetBed, ConsultationBed
from care.facility.models.icd11_diagnosis import (
INACTIVE_CONDITION_VERIFICATION_STATUSES,
ConditionVerificationStatus,
Expand Down Expand Up @@ -98,6 +98,9 @@


class PatientFilterSet(filters.FilterSet):

last_consultation_field = "last_consultation"

source = filters.ChoiceFilter(choices=PatientRegistration.SourceChoices)
disease_status = CareChoiceFilter(choice_dict=DISEASE_STATUS_DICT)
facility = filters.UUIDFilter(field_name="facility__external_id")
Expand All @@ -110,14 +113,14 @@
allow_transfer = filters.BooleanFilter(field_name="allow_transfer")
name = filters.CharFilter(field_name="name", lookup_expr="icontains")
patient_no = filters.CharFilter(
field_name="last_consultation__patient_no", lookup_expr="iexact"
field_name=f"{last_consultation_field}__patient_no", lookup_expr="iexact"
)
gender = filters.NumberFilter(field_name="gender")
age = filters.NumberFilter(field_name="age")
age_min = filters.NumberFilter(field_name="age", lookup_expr="gte")
age_max = filters.NumberFilter(field_name="age", lookup_expr="lte")
deprecated_covid_category = filters.ChoiceFilter(
field_name="last_consultation__deprecated_covid_category",
field_name=f"{last_consultation_field}__deprecated_covid_category",
choices=COVID_CATEGORY_CHOICES,
)
category = filters.ChoiceFilter(
Expand Down Expand Up @@ -169,24 +172,24 @@
state = filters.NumberFilter(field_name="state__id")
state_name = filters.CharFilter(field_name="state__name", lookup_expr="icontains")
# Consultation Fields
is_kasp = filters.BooleanFilter(field_name="last_consultation__is_kasp")
is_kasp = filters.BooleanFilter(field_name=f"{last_consultation_field}__is_kasp")
last_consultation_kasp_enabled_date = filters.DateFromToRangeFilter(
field_name="last_consultation__kasp_enabled_date"
field_name=f"{last_consultation_field}__kasp_enabled_date"
)
last_consultation_encounter_date = filters.DateFromToRangeFilter(
field_name="last_consultation__encounter_date"
field_name=f"{last_consultation_field}__encounter_date"
)
last_consultation_discharge_date = filters.DateFromToRangeFilter(
field_name="last_consultation__discharge_date"
field_name=f"{last_consultation_field}__discharge_date"
)
last_consultation_admitted_bed_type_list = MultiSelectFilter(
method="filter_by_bed_type",
)
last_consultation_medico_legal_case = filters.BooleanFilter(
field_name="last_consultation__medico_legal_case"
field_name=f"{last_consultation_field}__medico_legal_case"
)
last_consultation_current_bed__location = filters.UUIDFilter(
field_name="last_consultation__current_bed__bed__location__external_id"
field_name=f"{last_consultation_field}__current_bed__bed__location__external_id"
)

def filter_by_bed_type(self, queryset, name, value):
Expand All @@ -205,21 +208,21 @@
return queryset.filter(filter_q)

last_consultation_admitted_bed_type = CareChoiceFilter(
field_name="last_consultation__current_bed__bed__bed_type",
field_name=f"{last_consultation_field}__current_bed__bed__bed_type",
choice_dict=REVERSE_BED_TYPES,
)
last_consultation__new_discharge_reason = filters.ChoiceFilter(
field_name="last_consultation__new_discharge_reason",
field_name=f"{last_consultation_field}__new_discharge_reason",
choices=NewDischargeReasonEnum.choices,
)
last_consultation_assigned_to = filters.NumberFilter(
field_name="last_consultation__assigned_to"
field_name=f"{last_consultation_field}__assigned_to"
)
last_consultation_is_telemedicine = filters.BooleanFilter(
field_name="last_consultation__is_telemedicine"
field_name=f"{last_consultation_field}__is_telemedicine"
)
ventilator_interface = CareChoiceFilter(
field_name="last_consultation__last_daily_round__ventilator_interface",
field_name=f"{last_consultation_field}__last_daily_round__ventilator_interface",
choice_dict=VENTILATOR_CHOICES,
)

Expand Down Expand Up @@ -599,19 +602,65 @@
return Response(data=response_serializer.data, status=status.HTTP_200_OK)


class DischargePatientFilterSet(PatientFilterSet):

last_consultation_field = "last_discharge_consultation"

def filter_by_bed_type(self, queryset, name, value):
sainak marked this conversation as resolved.
Show resolved Hide resolved
if not value:
return queryset

Check warning on line 611 in care/facility/api/viewsets/patient.py

View check run for this annotation

Codecov / codecov/patch

care/facility/api/viewsets/patient.py#L611

Added line #L611 was not covered by tests

values = value.split(",")
filter_q = Q()
last_consultation_bed = (
ConsultationBed.objects.filter(end_date__isnull=False)
.order_by("consultation__patient_id", "-end_date")
.distinct("consultation__patient_id")
.values_list("id", "consultation_id")
)
if "None" in values:
filter_q |= ~Q(
last_discharge_consultation__id__in=[
x[1] for x in last_consultation_bed
shivankacker marked this conversation as resolved.
Show resolved Hide resolved
]
)
values.remove("None")

if values:
shivankacker marked this conversation as resolved.
Show resolved Hide resolved
filter_q |= Q(
last_discharge_consultation__id__in=ConsultationBed.objects.filter(
id__in=[x[0] for x in last_consultation_bed],
bed__bed_type__in=values,
).values("consultation_id")
)

return queryset.filter(filter_q)


@extend_schema_view(tags=["patient"])
class FacilityDischargedPatientViewSet(GenericViewSet, mixins.ListModelMixin):
permission_classes = (IsAuthenticated, DRYPermissions)
lookup_field = "external_id"
serializer_class = PatientListSerializer
filter_backends = (
PatientDRYFilter,
filters.DjangoFilterBackend,
rest_framework_filters.OrderingFilter,
PatientCustomOrderingFilter,
)
filterset_class = PatientFilterSet
filterset_class = DischargePatientFilterSet
queryset = (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shivankacker This seems like a pretty complex query. Can you add a simple comment about what this filter does?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anroopak I have added some documentation for the filter, please have a look

PatientRegistration.objects.select_related(
PatientRegistration.objects.annotate(
shivankacker marked this conversation as resolved.
Show resolved Hide resolved
last_discharge_consultation__id=Subquery(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You dont need to explicitly specify it as a subquery, it should automatically understand that its a subquery.
ids = Something.objects.values_list('id', flat=True)
SomethingElse.objects.filter(something_id__in=ids)

Will only have one query.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But will it not be required to use OuterRef?

PatientConsultation.objects.filter(
patient_id=OuterRef("id"),
discharge_date__isnull=False,
)
.order_by("-discharge_date")
.values("id")[:1]
)
)
.select_related(
"local_body",
"district",
"state",
Expand All @@ -622,8 +671,6 @@
"facility__local_body",
"facility__district",
"facility__state",
"last_consultation",
"last_consultation__assigned_to",
"last_edited",
"created_by",
)
Expand Down Expand Up @@ -668,9 +715,9 @@
"date_declared_positive",
"date_of_result",
"last_vaccinated_date",
"last_consultation_encounter_date",
"last_consultation_discharge_date",
"last_consultation_symptoms_onset_date",
"last_discharge_consultation_encounter_date",
"last_discharge_consultation_discharge_date",
"last_discharge_consultation_symptoms_onset_date",
]

ordering_fields = [
Expand All @@ -679,7 +726,7 @@
"created_date",
"modified_date",
"review_time",
"last_consultation__current_bed__bed__name",
"last_discharge_consultation__current_bed__bed__name",
"date_declared_positive",
]

Expand Down
148 changes: 147 additions & 1 deletion care/facility/tests/test_patient_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from enum import Enum

from django.utils.timezone import now
from django.utils.timezone import now, timedelta
from rest_framework import status
from rest_framework.test import APITestCase

Expand Down Expand Up @@ -432,6 +432,152 @@ def test_filter_by_review_missed(self):
self.assertIsNone(patient["review_time"])


class DischargePatientFilterTestCase(TestUtils, APITestCase):
@classmethod
def setUpTestData(cls):
cls.state = cls.create_state()
cls.district = cls.create_district(cls.state)
cls.local_body = cls.create_local_body(cls.district)
cls.super_user = cls.create_super_user("su", cls.district)
cls.facility = cls.create_facility(cls.super_user, cls.district, cls.local_body)
cls.location = cls.create_asset_location(cls.facility)
cls.user = cls.create_user("user", cls.district, user_type=15)

cls.iso_bed = cls.create_bed(cls.facility, cls.location, bed_type=1, name="ISO")
cls.icu_bed = cls.create_bed(cls.facility, cls.location, bed_type=2, name="ICU")
cls.oxy_bed = cls.create_bed(cls.facility, cls.location, bed_type=6, name="OXY")
cls.nor_bed = cls.create_bed(cls.facility, cls.location, bed_type=7, name="NOR")

cls.patient_iso = cls.create_patient(cls.district, cls.facility)
cls.patient_icu = cls.create_patient(cls.district, cls.facility)
cls.patient_oxy = cls.create_patient(cls.district, cls.facility)
cls.patient_nor = cls.create_patient(cls.district, cls.facility)
cls.patient_nb = cls.create_patient(cls.district, cls.facility)

cls.consultation_iso = cls.create_consultation(
patient=cls.patient_iso,
facility=cls.facility,
discharge_date=now(),
)
cls.consultation_icu = cls.create_consultation(
patient=cls.patient_icu,
facility=cls.facility,
discharge_date=now(),
)
cls.consultation_oxy = cls.create_consultation(
patient=cls.patient_oxy,
facility=cls.facility,
discharge_date=now(),
)
cls.consultation_nor = cls.create_consultation(
patient=cls.patient_nor,
facility=cls.facility,
discharge_date=now(),
)

cls.consultation_nb = cls.create_consultation(
patient=cls.patient_nb,
facility=cls.facility,
discharge_date=now(),
)

cls.consultation_bed_iso = cls.create_consultation_bed(
cls.consultation_iso,
cls.iso_bed,
end_date=now(),
)
cls.consultation_bed_icu = cls.create_consultation_bed(
cls.consultation_icu,
cls.icu_bed,
end_date=now(),
)
cls.consultation_bed_oxy = cls.create_consultation_bed(
cls.consultation_oxy,
cls.oxy_bed,
end_date=now(),
)
cls.consultation_bed_nor = cls.create_consultation_bed(
cls.consultation_nor,
cls.nor_bed,
end_date=now(),
)

def get_base_url(self) -> str:
return (
"/api/v1/facility/"
+ str(self.facility.external_id)
+ "/discharged_patients/"
)

def test_filter_by_admitted_to_bed(self):
self.client.force_authenticate(user=self.super_user)
sainak marked this conversation as resolved.
Show resolved Hide resolved
choices = ["1", "2", "6", "7", "None"]

res = self.client.get(
self.get_base_url(),
{"last_consultation_admitted_bed_type_list": ",".join([choices[0]])},
)

self.assertContains(res, self.patient_iso.external_id)
self.assertNotContains(res, self.patient_icu.external_id)
self.assertNotContains(res, self.patient_oxy.external_id)
self.assertNotContains(res, self.patient_nor.external_id)
self.assertNotContains(res, self.patient_nb.external_id)

res = self.client.get(
self.get_base_url(),
{"last_consultation_admitted_bed_type_list": ",".join(choices[1:3])},
)

self.assertNotContains(res, self.patient_iso.external_id)
self.assertContains(res, self.patient_icu.external_id)
self.assertContains(res, self.patient_oxy.external_id)
self.assertNotContains(res, self.patient_nor.external_id)
self.assertNotContains(res, self.patient_nb.external_id)

res = self.client.get(
self.get_base_url(),
{"last_consultation_admitted_bed_type_list": ",".join(choices)},
)

self.assertContains(res, self.patient_iso.external_id)
self.assertContains(res, self.patient_icu.external_id)
self.assertContains(res, self.patient_oxy.external_id)
self.assertContains(res, self.patient_nor.external_id)
self.assertContains(res, self.patient_nb.external_id)

res = self.client.get(
self.get_base_url(),
{"last_consultation_admitted_bed_type_list": ",".join(choices[3:])},
)

self.assertNotContains(res, self.patient_iso.external_id)
self.assertNotContains(res, self.patient_icu.external_id)
self.assertNotContains(res, self.patient_oxy.external_id)
self.assertContains(res, self.patient_nor.external_id)
self.assertContains(res, self.patient_nb.external_id)

def test_admitted_to_bed_after_readmission(self):
self.client.force_authenticate(user=self.super_user)
self.create_consultation_bed(
self.consultation_icu, self.iso_bed, end_date=now() + timedelta(days=1)
)

res = self.client.get(
self.get_base_url(),
{"last_consultation_admitted_bed_type_list": "1"},
)

self.assertContains(res, self.patient_icu.external_id)

res = self.client.get(
self.get_base_url(),
{"last_consultation_admitted_bed_type_list": "2"},
)

self.assertNotContains(res, self.patient_icu.external_id)


class PatientTransferTestCase(TestUtils, APITestCase):
@classmethod
def setUpTestData(cls):
Expand Down