Skip to content

Commit

Permalink
Updated serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
aeswibon committed Jul 7, 2022
1 parent 085a69f commit 2dfa17d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
23 changes: 1 addition & 22 deletions care/facility/api/serializers/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
from care.facility.api.serializers.facility import FacilityBasicInfoSerializer, FacilitySerializer
from care.facility.api.serializers.patient_consultation import PatientConsultationSerializer
from care.facility.models import (
DISEASE_CHOICES,
GENDER_CHOICES,
Disease,
Facility,
FacilityPatientStatsHistory,
PatientContactDetails,
Expand Down Expand Up @@ -96,10 +94,6 @@ def to_internal_value(self, data):


class PatientDetailSerializer(PatientListSerializer):
class MedicalHistorySerializer(serializers.Serializer):
disease = ChoiceField(choices=DISEASE_CHOICES)
details = serializers.CharField(required=False, allow_blank=True)

class PatientTeleConsultationSerializer(serializers.ModelSerializer):
class Meta:
model = PatientTeleConsultation
Expand All @@ -108,7 +102,6 @@ class Meta:
phone_number = PhoneNumberIsPossibleField()

facility = ExternalIdSerializerField(queryset=Facility.objects.all(), required=False)
medical_history = serializers.ListSerializer(child=MedicalHistorySerializer(), required=False)

tele_consultation_history = serializers.ListSerializer(child=PatientTeleConsultationSerializer(), read_only=True)
last_consultation = PatientConsultationSerializer(read_only=True)
Expand Down Expand Up @@ -175,7 +168,6 @@ def check_external_entry(self, srf_id):

def create(self, validated_data):
with transaction.atomic():
medical_history = validated_data.pop("medical_history", [])
meta_info = validated_data.pop("meta_info", {})
contacted_patients = validated_data.pop("contacted_patients", [])

Expand All @@ -188,13 +180,7 @@ def create(self, validated_data):
self.check_external_entry(validated_data["srf_id"])

validated_data["created_by"] = self.context["request"].user
patient = super().create(validated_data)
diseases = []

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

if meta_info:
meta_info_obj = PatientMetaInfo.objects.create(**meta_info)
Expand All @@ -219,7 +205,6 @@ def create(self, validated_data):

def update(self, instance, validated_data):
with transaction.atomic():
medical_history = validated_data.pop("medical_history", [])
meta_info = validated_data.pop("meta_info", {})
contacted_patients = validated_data.pop("contacted_patients", [])

Expand All @@ -233,12 +218,6 @@ 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 = []
for disease in medical_history:
diseases.append(Disease(patient=patient, **disease))
if diseases:
Disease.objects.bulk_create(diseases, ignore_conflicts=True)

if meta_info:
for key, value in meta_info.items():
Expand Down
24 changes: 24 additions & 0 deletions care/facility/api/serializers/patient_consultation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
from care.facility.models import CATEGORY_CHOICES, Facility, PatientRegistration
from care.facility.models.bed import Bed, ConsultationBed
from care.facility.models.notification import Notification
from care.facility.models.patient import Disease
from care.facility.models.patient_base import (
BLOOD_GROUP_CHOICES,
DISEASE_CHOICES,
SYMPTOM_CHOICES,
SuggestionChoices,
vaccineChoices
Expand All @@ -28,6 +30,11 @@
from config.serializers import ChoiceField

class PatientConsultationSerializer(serializers.ModelSerializer):
class MedicalHistorySerializer(serializers.Serializer):
medical_date = serializers.DateField(required=True)
disease = ChoiceField(choices=DISEASE_CHOICES)
other_disease = ChoiceField(choices=DISEASE_CHOICES)
details = serializers.CharField(required=False, allow_blank=True)

id = serializers.CharField(source="external_id", read_only=True)
facility_name = serializers.CharField(source="facility.name", read_only=True)
Expand Down Expand Up @@ -70,6 +77,7 @@ class PatientConsultationSerializer(serializers.ModelSerializer):
current_bed = ConsultationBedSerializer(read_only=True)

bed = ExternalIdSerializerField(queryset=Bed.objects.all(), required=False)
medical_history = serializers.ListSerializer(child=MedicalHistorySerializer(), required=False)

class Meta:
model = PatientConsultation
Expand Down Expand Up @@ -118,6 +126,15 @@ def update(self, instance, validated_data):
)
patient.save()

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


validated_data["last_updated_by_telemedicine"] = (
self.context["request"].user == instance.assigned_to
)
Expand Down Expand Up @@ -189,6 +206,13 @@ def create(self, validated_data):
consultation.last_edited_by = self.context["request"].user
consultation.save()

medical_history = validated_data.pop("medical_history", [])
diseases = []
for disease in medical_history:
diseases.append(Disease(patient=patient, **disease))
if diseases:
Disease.objects.bulk_create(diseases, ignore_conflicts=True)

if bed:
consultation_bed = ConsultationBed(
bed=bed, consultation=consultation, start_date=consultation.created_date
Expand Down

0 comments on commit 2dfa17d

Please sign in to comment.