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

Merge develop to staging v24.18.0 #2115

Merged
merged 9 commits into from
Apr 24, 2024
10 changes: 10 additions & 0 deletions care/facility/api/serializers/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,16 @@ def validate_countries_travelled(self, value):
value = [value]
return value

def validate_date_of_birth(self, value):
if value and value > now().date():
raise serializers.ValidationError("Enter a valid DOB such that age > 0")
return value

def validate_year_of_birth(self, value):
if value and value > now().year:
raise serializers.ValidationError("Enter a valid year of birth")
return value

def validate(self, attrs):
validated = super().validate(attrs)
if not self.partial and not (
Expand Down
4 changes: 2 additions & 2 deletions care/facility/api/viewsets/ambulance.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def get_serializer_class(self):

@extend_schema(tags=["ambulance"])
@action(methods=["POST"], detail=True)
def add_driver(self, request):
def add_driver(self, request, *args, **kwargs):
ambulance = self.get_object()
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
Expand All @@ -78,7 +78,7 @@ def add_driver(self, request):

@extend_schema(tags=["ambulance"])
@action(methods=["DELETE"], detail=True)
def remove_driver(self, request):
def remove_driver(self, request, *args, **kwargs):
ambulance = self.get_object()
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
Expand Down
59 changes: 45 additions & 14 deletions care/facility/api/viewsets/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
DISEASE_STATUS_DICT,
NewDischargeReasonEnum,
)
from care.facility.models.patient_consultation import PatientConsultation
from care.users.models import User
from care.utils.cache.cache_allowed_facilities import get_accessible_facilities
from care.utils.filters.choicefilter import CareChoiceFilter
Expand Down Expand Up @@ -300,8 +301,11 @@ def filter_queryset(self, request, queryset, view):
allowed_facilities = get_accessible_facilities(request.user)
q_filters = Q(facility__id__in=allowed_facilities)
if view.action == "retrieve":
q_filters |= Q(consultations__facility__id__in=allowed_facilities)
queryset = queryset.distinct("id")
q_filters |= Q(
id__in=PatientConsultation.objects.filter(
facility__id__in=allowed_facilities
).values("patient_id")
)
q_filters |= Q(last_consultation__assigned_to=request.user)
q_filters |= Q(assigned_to=request.user)
queryset = queryset.filter(q_filters)
Expand Down Expand Up @@ -449,14 +453,28 @@ class PatientViewSet(
CSV_EXPORT_LIMIT = 7

def get_queryset(self):
# filter_query = self.request.query_params.get("disease_status")
queryset = super().get_queryset()
# if filter_query:
# disease_status = filter_query if filter_query.isdigit() else DiseaseStatusEnum[filter_query].value
# return queryset.filter(disease_status=disease_status)

# if self.action == "list":
# queryset = queryset.filter(is_active=self.request.GET.get("is_active", True))
queryset = super().get_queryset().order_by("modified_date")

if self.action == "list":
queryset = queryset.annotate(
no_consultation_filed=Case(
When(
Q(last_consultation__isnull=True)
| ~Q(last_consultation__facility__id=F("facility__id"))
| (
Q(last_consultation__discharge_date__isnull=False)
& Q(is_active=True)
),
then=True,
),
default=False,
output_field=models.BooleanField(),
)
).order_by(
"-no_consultation_filed",
"modified_date",
)

return queryset

def get_serializer_class(self):
Expand Down Expand Up @@ -592,7 +610,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 @@ -611,12 +633,21 @@ 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(
Q(consultations__facility__external_id=self.kwargs["facility_external_id"])
& Q(consultations__discharge_date__isnull=False)
).distinct()
id__in=PatientConsultation.objects.filter(
discharge_date__isnull=False,
facility__external_id=self.kwargs["facility_external_id"],
).values_list("patient_id")
)


class FacilityPatientStatsHistoryFilterSet(filters.FilterSet):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Generated by Django 4.2.10 on 2024-04-17 04:32

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("facility", "0427_dailyround_is_parsed_by_ocr"),
]

operations = [
migrations.AlterField(
model_name="patientmetainfo",
name="occupation",
field=models.IntegerField(
blank=True,
choices=[
(1, "STUDENT"),
(2, "BUSINESSMAN"),
(3, "HEALTH_CARE_WORKER"),
(4, "HEALTH_CARE_LAB_WORKER"),
(5, "ANIMAL_HANDLER"),
(6, "OTHERS"),
(7, "HEALTHCARE_PRACTITIONER"),
(8, "PARADEMICS"),
(9, "BUSINESS_RELATED"),
(10, "ENGINEER"),
(11, "TEACHER"),
(12, "OTHER_PROFESSIONAL_OCCUPATIONS"),
(13, "OFFICE_ADMINISTRATIVE"),
(14, "CHEF"),
(15, "PROTECTIVE_SERVICE"),
(16, "HOSPITALITY"),
(17, "CUSTODIAL"),
(18, "CUSTOMER_SERVICE"),
(19, "SALES_SUPERVISOR"),
(20, "RETAIL_SALES_WORKER"),
(21, "INSURANCE_SALES_AGENT"),
(22, "SALES_REPRESENTATIVE"),
(23, "REAL_ESTATE"),
(24, "CONSTRUCTION_EXTRACTION"),
(25, "AGRI_NATURAL"),
(26, "PRODUCTION_OCCUPATION"),
(27, "PILOT_FLIGHT"),
(28, "VEHICLE_DRIVER"),
(29, "MILITARY"),
(30, "HOMEMAKER"),
(31, "UNKNOWN"),
(32, "NOT_APPLICABLE"),
],
null=True,
),
),
]
26 changes: 26 additions & 0 deletions care/facility/models/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,32 @@ class OccupationEnum(enum.Enum):
HEALTH_CARE_LAB_WORKER = 4
ANIMAL_HANDLER = 5
OTHERS = 6
HEALTHCARE_PRACTITIONER = 7
PARADEMICS = 8
BUSINESS_RELATED = 9
ENGINEER = 10
TEACHER = 11
OTHER_PROFESSIONAL_OCCUPATIONS = 12
OFFICE_ADMINISTRATIVE = 13
CHEF = 14
PROTECTIVE_SERVICE = 15
HOSPITALITY = 16
CUSTODIAL = 17
CUSTOMER_SERVICE = 18
SALES_SUPERVISOR = 19
RETAIL_SALES_WORKER = 20
INSURANCE_SALES_AGENT = 21
SALES_REPRESENTATIVE = 22
REAL_ESTATE = 23
CONSTRUCTION_EXTRACTION = 24
AGRI_NATURAL = 25
PRODUCTION_OCCUPATION = 26
PILOT_FLIGHT = 27
VEHICLE_DRIVER = 28
MILITARY = 29
HOMEMAKER = 30
UNKNOWN = 31
NOT_APPLICABLE = 32

OccupationChoices = [(item.value, item.name) for item in OccupationEnum]

Expand Down
42 changes: 42 additions & 0 deletions care/facility/models/tests/test_patient.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from datetime import timedelta

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

from care.facility.models import DiseaseStatusEnum
Expand All @@ -23,3 +27,41 @@ def test_disease_state_recovery_is_aliased_to_recovered(self):
patient.refresh_from_db()

self.assertEqual(patient.disease_status, DiseaseStatusEnum.RECOVERED.value)

def test_date_of_birth_validation(self):
dist_admin = self.create_user("dist_admin", self.district, user_type=30)
sample_data = {
"facility": self.facility.external_id,
"blood_group": "AB+",
"gender": 1,
"date_of_birth": now().date() + timedelta(days=365),
"year_of_birth": None,
"disease_status": "NEGATIVE",
"emergency_phone_number": "+919000000666",
"is_vaccinated": "false",
"number_of_doses": 0,
"phone_number": "+919000044343",
}
self.client.force_authenticate(user=dist_admin)
response = self.client.post("/api/v1/patient/", sample_data, format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertIn("date_of_birth", response.data)

def test_year_of_birth_validation(self):
dist_admin = self.create_user("dist_admin", self.district, user_type=30)
sample_data = {
"facility": self.facility.external_id,
"blood_group": "AB+",
"gender": 1,
"date_of_birth": None,
"year_of_birth": now().year + 1,
"disease_status": "NEGATIVE",
"emergency_phone_number": "+919000000666",
"is_vaccinated": "false",
"number_of_doses": 0,
"phone_number": "+919000044343",
}
self.client.force_authenticate(user=dist_admin)
response = self.client.post("/api/v1/patient/", sample_data, format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertIn("year_of_birth", response.data)
Loading
Loading