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

Staging Release for v24.15.0 #2046

Merged
merged 3 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ name = "pypi"
[packages]
argon2-cffi = "==23.1.0"
authlib = "==1.2.1"
boto3 = "==1.34.65"
boto3 = "==1.34.75"
celery = "==5.3.6"
django = "==4.2.10"
django-environ = "==0.11.2"
Expand Down Expand Up @@ -48,7 +48,7 @@ redis-om = "==0.2.1"

[dev-packages]
black = "==23.9.1"
boto3-stubs = {extras = ["s3", "boto3"], version = "==1.34.65"}
boto3-stubs = {extras = ["s3", "boto3"], version = "==1.34.75"}
coverage = "==7.4.0"
debugpy = "==1.8.1"
django-coverage-plugin = "==3.1.0"
Expand Down
29 changes: 15 additions & 14 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion care/facility/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class AmbulanceDriverAdmin(DjangoQLSearchMixin, admin.ModelAdmin):


class PatientAdmin(DjangoQLSearchMixin, admin.ModelAdmin):
list_display = ("id", "name", "age", "gender")
list_display = ("id", "name", "year_of_birth", "gender")
djangoql_completion_enabled_by_default = True


Expand Down
23 changes: 14 additions & 9 deletions care/facility/api/serializers/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,12 @@ class Meta:
"created_by",
"deleted",
"ongoing_medication",
"year_of_birth",
"meta_info",
"countries_travelled_old",
"allergies",
"external_id",
)
read_only = TIMESTAMP_FIELDS
read_only = TIMESTAMP_FIELDS + ("death_datetime",)


class PatientContactDetailsSerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -223,12 +222,16 @@ class Meta:
model = PatientRegistration
exclude = (
"deleted",
"year_of_birth",
"countries_travelled_old",
"external_id",
)
include = ("contacted_patients",)
read_only = TIMESTAMP_FIELDS + ("last_edited", "created_by", "is_active")
read_only = TIMESTAMP_FIELDS + (
"last_edited",
"created_by",
"is_active",
"death_datetime",
)

# def get_last_consultation(self, obj):
# last_consultation = PatientConsultation.objects.filter(patient=obj).last()
Expand All @@ -250,13 +253,15 @@ def validate_countries_travelled(self, value):

def validate(self, attrs):
validated = super().validate(attrs)
if (
not self.partial
and not validated.get("age")
and not validated.get("date_of_birth")
if not self.partial and not (
validated.get("year_of_birth") or validated.get("date_of_birth")
):
raise serializers.ValidationError(
{"non_field_errors": ["Either age or date_of_birth should be passed"]}
{
"non_field_errors": [
"Either year_of_birth or date_of_birth should be passed"
]
}
)

if validated.get("is_vaccinated"):
Expand Down
87 changes: 67 additions & 20 deletions care/facility/api/viewsets/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,18 @@
from django.conf import settings
from django.contrib.postgres.search import TrigramSimilarity
from django.db import models
from django.db.models import Case, OuterRef, Q, Subquery, When
from django.db.models import (
Case,
ExpressionWrapper,
F,
Func,
OuterRef,
Q,
Subquery,
Value,
When,
)
from django.db.models.functions import Coalesce, ExtractDay, Now
from django.db.models.query import QuerySet
from django_filters import rest_framework as filters
from djqscsv import render_to_csv_response
Expand Down Expand Up @@ -333,25 +344,61 @@ class PatientViewSet(
]
permission_classes = (IsAuthenticated, DRYPermissions)
lookup_field = "external_id"
queryset = PatientRegistration.objects.all().select_related(
"local_body",
"district",
"state",
"ward",
"assigned_to",
"facility",
"facility__ward",
"facility__local_body",
"facility__district",
"facility__state",
# "nearest_facility",
# "nearest_facility__local_body",
# "nearest_facility__district",
# "nearest_facility__state",
"last_consultation",
"last_consultation__assigned_to",
"last_edited",
"created_by",
queryset = (
PatientRegistration.objects.all()
.select_related(
"local_body",
"district",
"state",
"ward",
"assigned_to",
"facility",
"facility__ward",
"facility__local_body",
"facility__district",
"facility__state",
# "nearest_facility",
# "nearest_facility__local_body",
# "nearest_facility__district",
# "nearest_facility__state",
"last_consultation",
"last_consultation__assigned_to",
"last_edited",
"created_by",
)
.annotate(
coalesced_dob=Coalesce(
"date_of_birth",
Func(
F("year_of_birth"),
Value(1),
Value(1),
function="MAKE_DATE",
output_field=models.DateField(),
),
output_field=models.DateField(),
),
age_end=Case(
When(death_datetime__isnull=True, then=Now()),
default=F("death_datetime__date"),
),
)
.annotate(
age=Func(
Value("year"),
Func(
F("age_end"),
F("coalesced_dob"),
function="age",
),
function="date_part",
output_field=models.IntegerField(),
),
age_days=ExpressionWrapper(
ExtractDay(F("age_end") - F("coalesced_dob")),
output_field=models.IntegerField(),
),
)
)
ordering_fields = [
"facility__name",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 4.2.10 on 2024-03-22 11:21

import django.core.validators
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("facility", "0421_merge_20240318_1434"),
]

operations = [
migrations.AlterField(
model_name="facilityinventorylog",
name="quantity",
field=models.FloatField(
default=0, validators=[django.core.validators.MinValueValidator(0.0)]
),
),
migrations.AlterField(
model_name="facilityinventoryminquantity",
name="min_quantity",
field=models.FloatField(
default=0, validators=[django.core.validators.MinValueValidator(0.0)]
),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Generated by Django 4.2.8 on 2024-03-13 07:03

import django.core.validators
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("facility", "0423_patientconsultation_consent_records_and_more"),
]

def populate_patientregistration_death_datetime(apps, schema_editor):
PatientRegistration = apps.get_model("facility", "PatientRegistration")

patients = (
PatientRegistration.objects.only("last_consultation")
.filter(last_consultation__death_datetime__isnull=False)
.annotate(new_death_datetime=models.F("last_consultation__death_datetime"))
)

for patient in patients:
patient.death_datetime = patient.new_death_datetime

PatientRegistration.objects.bulk_update(
patients, ["death_datetime"], batch_size=1000
)

operations = [
migrations.RemoveField(
model_name="historicalpatientregistration",
name="age",
),
migrations.RemoveField(
model_name="patientregistration",
name="age",
),
migrations.AddField(
model_name="historicalpatientregistration",
name="death_datetime",
field=models.DateTimeField(default=None, null=True),
),
migrations.AddField(
model_name="patientregistration",
name="death_datetime",
field=models.DateTimeField(default=None, null=True),
),
migrations.AlterField(
model_name="historicalpatientregistration",
name="year_of_birth",
field=models.IntegerField(
null=True, validators=[django.core.validators.MinValueValidator(1900)]
),
),
migrations.AlterField(
model_name="patientregistration",
name="year_of_birth",
field=models.IntegerField(
null=True, validators=[django.core.validators.MinValueValidator(1900)]
),
),
migrations.RunPython(
populate_patientregistration_death_datetime,
migrations.RunPython.noop,
),
]
5 changes: 3 additions & 2 deletions care/facility/models/inventory.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.contrib.auth import get_user_model
from django.core.validators import MinValueValidator
from django.db import models
from django.db.models import Index

Expand Down Expand Up @@ -108,7 +109,7 @@ class FacilityInventoryLog(FacilityBaseModel, FacilityRelatedPermissionMixin):
)
current_stock = models.FloatField(default=0)
quantity_in_default_unit = models.FloatField(default=0)
quantity = models.FloatField(default=0)
quantity = models.FloatField(default=0, validators=[MinValueValidator(0.0)])
unit = models.ForeignKey(
FacilityInventoryUnit, on_delete=models.SET_NULL, null=True, blank=False
)
Expand Down Expand Up @@ -157,7 +158,7 @@ class FacilityInventoryMinQuantity(FacilityBaseModel, FacilityRelatedPermissionM
item = models.ForeignKey(
FacilityInventoryItem, on_delete=models.SET_NULL, null=True, blank=False
)
min_quantity = models.FloatField(default=0)
min_quantity = models.FloatField(default=0, validators=[MinValueValidator(0.0)])

class Meta:
constraints = [
Expand Down
Loading
Loading