class PatientListSerializer(serializers.ModelSerializer): id = serializers.CharField(source="external_id", read_only=True) facility = serializers.UUIDField(source="facility.external_id", allow_null=True, read_only=True) facility_object = FacilityBasicInfoSerializer(source="facility", read_only=True) ward_object = WardSerializer(source="ward", read_only=True) local_body_object = LocalBodySerializer(source="local_body", read_only=True) district_object = DistrictSerializer(source="district", read_only=True) state_object = StateSerializer(source="state", read_only=True) blood_group = ChoiceField(choices=BLOOD_GROUP_CHOICES, required=True) disease_status = ChoiceField(choices=DISEASE_STATUS_CHOICES, default=DiseaseStatusEnum.SUSPECTED.value) source = ChoiceField(choices=PatientRegistration.SourceChoices) class Meta: model = PatientRegistration exclude = ( "created_by", "deleted", "ongoing_medication", "patient_search_id", "year_of_birth", "meta_info", "countries_travelled_old", "allergies", "external_id", ) read_only = TIMESTAMP_FIELDS
class FacilityBasicInfoSerializer(serializers.ModelSerializer): id = serializers.UUIDField(source="external_id", read_only=True) ward_object = WardSerializer(source="ward", read_only=True) local_body_object = LocalBodySerializer(source="local_body", read_only=True) district_object = DistrictSerializer(source="district", read_only=True) state_object = StateSerializer(source="state", read_only=True) facility_type = serializers.SerializerMethodField() def get_facility_type(self, facility): return { "id": facility.facility_type, "name": facility.get_facility_type_display() } class Meta: model = Facility fields = ( "id", "name", "local_body", "district", "state", "ward_object", "local_body_object", "district_object", "state_object", "facility_type", )
def get_all_local_body(self, *args, **kwargs): district = self.get_object() data = [] for lsg_object in LocalBody.objects.filter(district=district): local_body_object = LocalBodySerializer(lsg_object).data local_body_object["wards"] = WardSerializer( Ward.objects.filter(local_body=lsg_object), many=True).data data.append(local_body_object) return Response(data)
class PatientExternalTestSerializer(serializers.ModelSerializer): ward_object = WardSerializer(source="ward", read_only=True) local_body_object = LocalBodySerializer(source="local_body", read_only=True) district_object = DistrictSerializer(source="district", read_only=True) sample_collection_date = serializers.DateField(input_formats=["%Y-%m-%d"], required=False) result_date = serializers.DateField(input_formats=["%Y-%m-%d"], required=False) def validate_empty_values(self, data, *args, **kwargs): # if "is_repeat" in data: # is_repeat = data["is_repeat"] # if is_repeat.lower() == "yes": # data["is_repeat"] = True # else: # data["is_repeat"] = False district_obj = None if "district" in data: district = data["district"] district_obj = District.objects.filter( name__icontains=district).first() if district_obj: data["district"] = district_obj.id else: raise ValidationError( {"district": ["District Does not Exist"]}) else: raise ValidationError( {"district": ["District Not Present in Data"]}) local_body_obj = None if "local_body" in data and district_obj: if not data["local_body"]: raise ValidationError( {"local_body": ["Local Body Cannot Be Empty"]}) local_body = data["local_body"] local_body_obj = LocalBody.objects.filter( name__icontains=local_body, district=district_obj).first() if local_body_obj: data["local_body"] = local_body_obj.id else: raise ValidationError( {"local_body": ["Local Body Does not Exist"]}) else: raise ValidationError( {"local_body": ["Local Body Not Present in Data"]}) if "ward" in data and local_body_obj: if data["ward"]: ward_obj = Ward.objects.filter( number=data["ward"], local_body=local_body_obj).first() if ward_obj: data["ward"] = ward_obj.id else: raise ValidationError({"ward": ["Ward Does not Exist"]}) return super().validate_empty_values(data, *args, **kwargs) # def validate_ward(self, value): # print(value) class Meta: model = PatientExternalTest fields = "__all__"