Skip to content

Commit

Permalink
Annotate CSV export fields with patient age for sample test and shift…
Browse files Browse the repository at this point in the history
…ing (#2101)

* Annotate CSV export fields with patient age for sample test and shifting

* use consistent naming
  • Loading branch information
rithviknishad authored May 2, 2024
1 parent 1ed6c07 commit cf6902b
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 5 deletions.
6 changes: 4 additions & 2 deletions care/facility/api/viewsets/patient_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,10 @@ def list(self, request, *args, **kwargs):
raise PermissionDenied()

if settings.CSV_REQUEST_PARAMETER in request.GET:
queryset = self.filter_queryset(self.get_queryset()).values(
*PatientSample.CSV_MAPPING.keys()
queryset = (
self.filter_queryset(self.get_queryset())
.annotate(**PatientSample.CSV_ANNOTATE_FIELDS)
.values(*PatientSample.CSV_MAPPING.keys())
)
return render_to_csv_response(
queryset,
Expand Down
6 changes: 4 additions & 2 deletions care/facility/api/viewsets/shifting.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,10 @@ def transfer(self, request, *args, **kwargs):

def list(self, request, *args, **kwargs):
if settings.CSV_REQUEST_PARAMETER in request.GET:
queryset = self.filter_queryset(self.get_queryset()).values(
*ShiftingRequest.CSV_MAPPING.keys()
queryset = (
self.filter_queryset(self.get_queryset())
.annotate(**ShiftingRequest.CSV_ANNOTATE_FIELDS)
.values(*ShiftingRequest.CSV_MAPPING.keys())
)
return render_to_csv_response(
queryset,
Expand Down
42 changes: 41 additions & 1 deletion care/facility/models/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from django.contrib.postgres.aggregates import ArrayAgg
from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models
from django.db.models import JSONField
from django.db.models import Case, F, Func, JSONField, Value, When
from django.db.models.functions import Coalesce, Now
from django.utils import timezone
from simple_history.models import HistoricalRecords

Expand Down Expand Up @@ -773,3 +774,42 @@ class PatientNotesEdit(models.Model):

class Meta:
ordering = ["-edited_date"]


class PatientAgeFunc(Func):
"""
Expression to calculate the age of a patient based on date of birth/year of
birth and death date time.
Eg:
```
PatientSample.objects.annotate(patient_age=PatientAgeFunc())
```
"""

function = "date_part"

def __init__(self) -> None:
super().__init__(
Value("year"),
Func(
Case(
When(patient__death_datetime__isnull=True, then=Now()),
default=F("patient__death_datetime__date"),
),
Coalesce(
"patient__date_of_birth",
Func(
F("patient__year_of_birth"),
Value(1),
Value(1),
function="MAKE_DATE",
output_field=models.DateField(),
),
output_field=models.DateField(),
),
function="age",
),
output_field=models.IntegerField(),
)
5 changes: 5 additions & 0 deletions care/facility/models/patient_sample.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.db import models

from care.facility.models import FacilityBaseModel, PatientRegistration, reverse_choices
from care.facility.models.patient import PatientAgeFunc
from care.users.models import User

SAMPLE_TYPE_CHOICES = [
Expand Down Expand Up @@ -125,6 +126,10 @@ class PatientSample(FacilityBaseModel):
"date_of_result": "Date of Result",
}

CSV_ANNOTATE_FIELDS = {
"patient__age": PatientAgeFunc(),
}

CSV_MAKE_PRETTY = {
"sample_type": (lambda x: REVERSE_SAMPLE_TYPE_CHOICES.get(x, "-")),
"status": (
Expand Down
5 changes: 5 additions & 0 deletions care/facility/models/shifting.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from care.facility.models import (
FACILITY_TYPES,
FacilityBaseModel,
PatientAgeFunc,
pretty_boolean,
reverse_choices,
)
Expand Down Expand Up @@ -137,6 +138,10 @@ class ShiftingRequest(FacilityBaseModel):
"reason": "Reason for Shifting",
}

CSV_ANNOTATE_FIELDS = {
"patient__age": PatientAgeFunc(),
}

CSV_MAKE_PRETTY = {
"status": (lambda x: REVERSE_SHIFTING_STATUS_CHOICES.get(x, "-")),
"is_up_shift": pretty_boolean,
Expand Down

0 comments on commit cf6902b

Please sign in to comment.