From d64d76dd4d091d4212160ed2ca0ce368ed9bf6d3 Mon Sep 17 00:00:00 2001 From: cp-Coder Date: Wed, 5 Oct 2022 17:24:11 +0530 Subject: [PATCH] Updated value of health_details field in consultation API --- .../api/serializers/patient_consultation.py | 70 ++++++++++++++----- 1 file changed, 52 insertions(+), 18 deletions(-) diff --git a/care/facility/api/serializers/patient_consultation.py b/care/facility/api/serializers/patient_consultation.py index 91b1019d43..6bead9ea92 100644 --- a/care/facility/api/serializers/patient_consultation.py +++ b/care/facility/api/serializers/patient_consultation.py @@ -39,7 +39,9 @@ class PatientConsultationSerializer(serializers.ModelSerializer): id = serializers.CharField(source="external_id", read_only=True) - facility_name = serializers.CharField(source="facility.name", read_only=True) + facility_name = serializers.CharField( + source="facility.name", read_only=True + ) suggestion_text = ChoiceField( choices=PatientConsultation.SUGGESTION_CHOICES, read_only=True, @@ -58,10 +60,14 @@ class PatientConsultationSerializer(serializers.ModelSerializer): referred_to = ExternalIdSerializerField( queryset=Facility.objects.all(), required=False ) - patient = ExternalIdSerializerField(queryset=PatientRegistration.objects.all()) + patient = ExternalIdSerializerField( + queryset=PatientRegistration.objects.all() + ) facility = ExternalIdSerializerField(read_only=True) - assigned_to_object = UserAssignedSerializer(source="assigned_to", read_only=True) + assigned_to_object = UserAssignedSerializer( + source="assigned_to", read_only=True + ) assigned_to = serializers.PrimaryKeyRelatedField( queryset=User.objects.all(), required=False, allow_null=True @@ -78,7 +84,9 @@ class PatientConsultationSerializer(serializers.ModelSerializer): required=False, ) - review_time = serializers.IntegerField(default=-1, write_only=True, required=False) + review_time = serializers.IntegerField( + default=-1, write_only=True, required=False + ) last_edited_by = UserBaseMinimumSerializer(read_only=True) created_by = UserBaseMinimumSerializer(read_only=True) @@ -119,7 +127,9 @@ def get_icd11_diagnoses_objects_by_ids(self, diagnoses_ids): return diagnosis_objects def get_icd11_diagnoses_object(self, consultation): - return self.get_icd11_diagnoses_objects_by_ids(consultation.icd11_diagnoses) + return self.get_icd11_diagnoses_objects_by_ids( + consultation.icd11_diagnoses + ) def get_icd11_provisional_diagnoses_object(self, consultation): return self.get_icd11_diagnoses_objects_by_ids( @@ -150,7 +160,11 @@ def update(self, instance, validated_data): if instance.discharge_date: raise ValidationError( - {"consultation": ["Discharged Consultation data cannot be updated"]} + { + "consultation": [ + "Discharged Consultation data cannot be updated" + ] + } ) if instance.suggestion == SuggestionChoices.OP: @@ -209,7 +223,10 @@ def update(self, instance, validated_data): pass if "assigned_to" in validated_data: - if validated_data["assigned_to"] != _temp and validated_data["assigned_to"]: + if ( + validated_data["assigned_to"] != _temp + and validated_data["assigned_to"] + ): NotificationGenerator( event=Notification.Event.PATIENT_CONSULTATION_ASSIGNMENT, caused_by=self.context["request"].user, @@ -241,12 +258,16 @@ def create(self, validated_data): # Authorisation Check - allowed_facilities = get_home_facility_queryset(self.context["request"].user) + allowed_facilities = get_home_facility_queryset( + self.context["request"].user + ) if not allowed_facilities.filter( id=self.validated_data["patient"].facility.id ).exists(): raise ValidationError( - {"facility": "Consultation creates are only allowed in home facility"} + { + "facility": "Consultation creates are only allowed in home facility" + } ) # End Authorisation Checks @@ -265,7 +286,9 @@ def create(self, validated_data): if validated_data["patient"].last_consultation: if not validated_data["patient"].last_consultation.discharge_date: raise ValidationError( - {"consultation": "Exists please Edit Existing Consultation"} + { + "consultation": "Exists please Edit Existing Consultation" + } ) if "is_kasp" in validated_data: @@ -296,7 +319,8 @@ def create(self, validated_data): }, ) serializer.is_valid(raise_exception=True) - serializer.save() + health_details = serializer.save() + consultation.health_details = health_details.external_id.hex except KeyError as error: if consultation.patient.last_consultation is None: @@ -336,7 +360,9 @@ def create(self, validated_data): if action != -1: patient.action = action if review_time > 0: - patient.review_time = localtime(now()) + timedelta(minutes=review_time) + patient.review_time = localtime(now()) + timedelta( + minutes=review_time + ) patient.save() NotificationGenerator( @@ -365,9 +391,9 @@ def validate(self, attrs): # TODO Add Bed Authorisation Validation if "suggestion" in validated: - if validated["suggestion"] is SuggestionChoices.R and not validated.get( - "referred_to" - ): + if validated[ + "suggestion" + ] is SuggestionChoices.R and not validated.get("referred_to"): raise ValidationError( { "referred_to": [ @@ -400,7 +426,11 @@ def validate(self, attrs): ) if validated["review_time"] <= 0: raise ValidationError( - {"review_time": ["This field value is must be greater than 0."]} + { + "review_time": [ + "This field value is must be greater than 0." + ] + } ) from care.facility.static_data.icd11 import ICDDiseases @@ -434,8 +464,12 @@ def validate(self, attrs): class PatientConsultationIDSerializer(serializers.ModelSerializer): - consultation_id = serializers.UUIDField(source="external_id", read_only=True) - patient_id = serializers.UUIDField(source="patient.external_id", read_only=True) + consultation_id = serializers.UUIDField( + source="external_id", read_only=True + ) + patient_id = serializers.UUIDField( + source="patient.external_id", read_only=True + ) class Meta: model = PatientConsultation