From 6cba5a501008ee477fa6a7f0bc1c1b84f1f41ff3 Mon Sep 17 00:00:00 2001 From: himanshu-sharmav Date: Thu, 7 Nov 2024 05:47:04 +0000 Subject: [PATCH] changes for pr test --- care/facility/api/serializers/prescription.py | 46 ++++++++++--------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/care/facility/api/serializers/prescription.py b/care/facility/api/serializers/prescription.py index ef1e116b77..db24d83d11 100644 --- a/care/facility/api/serializers/prescription.py +++ b/care/facility/api/serializers/prescription.py @@ -117,30 +117,32 @@ def _remove_irrelevant_fields(self, attrs: dict, keep_fields: set[str]) -> None: def validate_medicine(self, attrs): """Validate the medicine field and check for duplicate prescriptions.""" - attrs["medicine"] = get_object_or_404( - MedibaseMedicine, external_id=attrs["medicine"] - ) - - # Check for existing prescription - if ( - not self.instance - and Prescription.objects.filter( - consultation__external_id=self.context["request"].parser_context[ - "kwargs" - ]["consultation_external_id"], - medicine=attrs["medicine"], - discontinued=False, - ).exists() - ): - raise serializers.ValidationError( - { - "medicine": ( - "This medicine is already prescribed to this patient. " - "Discontinue the existing prescription to prescribe again." - ) - } + if "medicine" in attrs: + medicine_id = attrs["medicine"] + attrs["medicine"] = get_object_or_404( + MedibaseMedicine, external_id=medicine_id ) + # Check for existing prescription + if ( + not self.instance + and Prescription.objects.filter( + consultation__external_id=self.context["request"].parser_context[ + "kwargs" + ]["consultation_external_id"], + medicine=attrs["medicine"], + discontinued=False, + ).exists() + ): + raise serializers.ValidationError( + { + "medicine": ( + "This medicine is already prescribed to this patient. " + "Discontinue the existing prescription to prescribe again." + ) + } + ) + def _validate_max_dosage_presence(self, base_dosage: str, max_dosage: str) -> None: if max_dosage and not base_dosage: raise serializers.ValidationError(