-
Notifications
You must be signed in to change notification settings - Fork 248
/
patient_external_test.py
132 lines (109 loc) · 5 KB
/
patient_external_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
from rest_framework import serializers
from rest_framework.exceptions import ValidationError
from care.facility.models import PatientExternalTest
from care.facility.models.patient import PatientRegistration
from care.users.api.serializers.lsg import (
DistrictSerializer,
LocalBodySerializer,
WardSerializer,
)
from care.users.models import REVERSE_LOCAL_BODY_CHOICES, District, LocalBody, Ward
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)
local_body_type = serializers.CharField(required=False, write_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"]})
if "local_body_type" not in data:
raise ValidationError(
{"local_body_type": ["local_body_type is not present in data"]}
)
if not data["local_body_type"]:
raise ValidationError(
{"local_body_type": ["local_body_type cannot be empty"]}
)
if data["local_body_type"].lower() not in REVERSE_LOCAL_BODY_CHOICES:
raise ValidationError({"local_body_type": ["Invalid Local Body Type"]})
local_body_type = REVERSE_LOCAL_BODY_CHOICES[data["local_body_type"].lower()]
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,
body_type=local_body_type,
).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:
try:
int(data["ward"])
except Exception:
raise ValidationError({"ward": ["Ward must be an integer value"]})
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"]})
del data["local_body_type"]
return super().validate_empty_values(data, *args, **kwargs)
def create(self, validated_data):
if "srf_id" in validated_data:
if PatientRegistration.objects.filter(
srf_id__iexact=validated_data["srf_id"]
).exists():
validated_data["patient_created"] = True
return super().create(validated_data)
class Meta:
model = PatientExternalTest
fields = "__all__"
read_only_fields = ("patient_created",)
class PatientExternalTestUpdateSerializer(serializers.ModelSerializer):
local_body = serializers.PrimaryKeyRelatedField(
queryset=LocalBody.objects.all(), required=False
)
ward = serializers.PrimaryKeyRelatedField(
queryset=Ward.objects.all(), required=False
)
class Meta:
model = PatientExternalTest
fields = ("address", "ward", "local_body", "patient_created")
def update(self, instance, validated_data):
if "ward" in validated_data:
validated_data["local_body"] = validated_data["ward"].local_body
if "local_body" in validated_data:
validated_data["local_body_type"] = validated_data["local_body"].body_type
if validated_data["local_body"].district != instance.district:
raise ValidationError(
{"local_body": "Only supported within same district"}
)
return super().update(instance, validated_data)