Skip to content

Commit

Permalink
Updated health_details and medical_history API
Browse files Browse the repository at this point in the history
* Added vaccine, medical history nad disease model
* Added the migrations with function to migrate the data
* Added required serializers
* Reformatted the code
  • Loading branch information
aeswibon committed Oct 19, 2022
1 parent c164c4e commit f711fb8
Show file tree
Hide file tree
Showing 13 changed files with 615 additions and 212 deletions.
4 changes: 2 additions & 2 deletions care/facility/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from .models import (
Building,
Disease,
Diseases,
Facility,
FacilityCapacity,
FacilityInventoryItem,
Expand Down Expand Up @@ -201,7 +201,7 @@ class FacilityUserAdmin(DjangoQLSearchMixin, admin.ModelAdmin, ExportCsvMixin):
admin.site.register(PatientRegistration, PatientAdmin)
admin.site.register(PatientTeleConsultation)
admin.site.register(PatientSample, PatientSampleAdmin)
admin.site.register(Disease)
admin.site.register(Diseases)
admin.site.register(FacilityInventoryUnit)
admin.site.register(FacilityInventoryUnitConverter)
admin.site.register(FacilityInventoryItem)
Expand Down
79 changes: 79 additions & 0 deletions care/facility/api/serializers/medical_history.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
from django.db import transaction
from rest_framework import serializers

from care.facility.models import MedicalHistory, PatientRegistration
from care.facility.models.patient import Diseases
from care.facility.models.patient_consultation import PatientConsultation
from care.utils.serializer.external_id_field import ExternalIdSerializerField


class DiseaseSerializer(serializers.ModelSerializer):
date = serializers.DateField()

class Meta:
model = Diseases
fields = (
"disease",
"details",
"date",
"precision",
)


class MedicalHistorySerializer(serializers.ModelSerializer):
id = serializers.CharField(source="external_id", read_only=True)

patient = ExternalIdSerializerField(
queryset=PatientRegistration.objects.all(), required=False
)

consultation = ExternalIdSerializerField(
queryset=PatientConsultation.objects.all(), required=False
)

patient_diseases = serializers.ListSerializer(
child=DiseaseSerializer(),
required=False,
)

class Meta:
model = MedicalHistory
exclude = ("deleted", "external_id")

def create(self, validated_data):
with transaction.atomic():
consultation = validated_data["consultation"]
patient_diseases = validated_data.pop("patient_diseases", [])
medical_history = super().create(validated_data)
diseases = []
for disease in patient_diseases:
diseases.append(
Diseases(
medical_history=medical_history,
**disease,
)
)
if diseases:
Diseases.objects.bulk_create(diseases, ignore_conflicts=True)
consultation.last_medical_history = medical_history
consultation.save(update_fields=["last_medical_history"])
return medical_history

def update(self, instance, validated_data):
with transaction.atomic():
patient_diseases = validated_data.pop("patient_diseases", [])
medical_history = super().update(instance, validated_data)
Diseases.objects.filter(medical_history=medical_history).update(
deleted=True
)
diseases = []
for disease in patient_diseases:
diseases.append(
Diseases(
medical_history=medical_history,
**disease,
)
)
if diseases:
Diseases.objects.bulk_create(diseases, ignore_conflicts=True)
return medical_history
12 changes: 6 additions & 6 deletions care/facility/api/serializers/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from care.facility.models import (
DISEASE_CHOICES,
GENDER_CHOICES,
Disease,
Diseases,
Facility,
FacilityPatientStatsHistory,
PatientContactDetails,
Expand Down Expand Up @@ -259,9 +259,9 @@ def create(self, validated_data):
diseases = []

for disease in medical_history:
diseases.append(Disease(patient=patient, **disease))
diseases.append(Diseases(patient=patient, **disease))
if diseases:
Disease.objects.bulk_create(diseases, ignore_conflicts=True)
Diseases.objects.bulk_create(diseases, ignore_conflicts=True)

if meta_info:
meta_info_obj = PatientMetaInfo.objects.create(**meta_info)
Expand Down Expand Up @@ -305,12 +305,12 @@ def update(self, instance, validated_data):
self.check_external_entry(validated_data["srf_id"])

patient = super().update(instance, validated_data)
Disease.objects.filter(patient=patient).update(deleted=True)
Diseases.objects.filter(patient=patient).update(deleted=True)
diseases = []
for disease in medical_history:
diseases.append(Disease(patient=patient, **disease))
diseases.append(Diseases(patient=patient, **disease))
if diseases:
Disease.objects.bulk_create(diseases, ignore_conflicts=True)
Diseases.objects.bulk_create(diseases, ignore_conflicts=True)

if meta_info:
for key, value in meta_info.items():
Expand Down
Loading

0 comments on commit f711fb8

Please sign in to comment.