-
Notifications
You must be signed in to change notification settings - Fork 249
/
daily_round.py
354 lines (311 loc) · 14 KB
/
daily_round.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
from datetime import timedelta
from uuid import uuid4
from django.db import transaction
from django.utils import timezone
from django.utils.timezone import localtime, now
from rest_framework import serializers
from rest_framework.exceptions import ValidationError
from care.facility.events.handler import create_consultation_events
# from care.facility.api.serializers.bed import BedSerializer
from care.facility.models import (
CATEGORY_CHOICES,
COVID_CATEGORY_CHOICES,
PatientRegistration,
)
from care.facility.models.bed import Bed
from care.facility.models.daily_round import DailyRound
from care.facility.models.notification import Notification
from care.facility.models.patient_base import (
CURRENT_HEALTH_CHOICES,
SYMPTOM_CHOICES,
SuggestionChoices,
)
from care.facility.models.patient_consultation import PatientConsultation
from care.users.api.serializers.user import UserBaseMinimumSerializer
from care.utils.notification_handler import NotificationGenerator
from care.utils.queryset.facility import get_home_facility_queryset
from config.serializers import ChoiceField
class DailyRoundSerializer(serializers.ModelSerializer):
id = serializers.CharField(source="external_id", read_only=True)
additional_symptoms = serializers.MultipleChoiceField(
choices=SYMPTOM_CHOICES, required=False
)
deprecated_covid_category = ChoiceField(
choices=COVID_CATEGORY_CHOICES, required=False
) # Deprecated
patient_category = ChoiceField(choices=CATEGORY_CHOICES, required=False)
current_health = ChoiceField(choices=CURRENT_HEALTH_CHOICES, required=False)
action = ChoiceField(
choices=PatientRegistration.ActionChoices, write_only=True, required=False
)
review_interval = serializers.IntegerField(
source="consultation__review_interval", required=False
)
taken_at = serializers.DateTimeField(required=True)
rounds_type = ChoiceField(choices=DailyRound.RoundsTypeChoice, required=True)
# Critical Care Components
consciousness_level = ChoiceField(
choices=DailyRound.ConsciousnessChoice, required=False
)
left_pupil_light_reaction = ChoiceField(
choices=DailyRound.PupilReactionChoice, required=False
)
right_pupil_light_reaction = ChoiceField(
choices=DailyRound.PupilReactionChoice, required=False
)
limb_response_upper_extremity_right = ChoiceField(
choices=DailyRound.LimbResponseChoice, required=False
)
limb_response_upper_extremity_left = ChoiceField(
choices=DailyRound.LimbResponseChoice, required=False
)
limb_response_lower_extremity_left = ChoiceField(
choices=DailyRound.LimbResponseChoice, required=False
)
limb_response_lower_extremity_right = ChoiceField(
choices=DailyRound.LimbResponseChoice, required=False
)
rhythm = ChoiceField(choices=DailyRound.RythmnChoice, required=False)
ventilator_interface = ChoiceField(
choices=DailyRound.VentilatorInterfaceChoice, required=False
)
ventilator_mode = ChoiceField(
choices=DailyRound.VentilatorModeChoice, required=False
)
ventilator_oxygen_modality = ChoiceField(
choices=DailyRound.VentilatorOxygenModalityChoice, required=False
)
insulin_intake_frequency = ChoiceField(
choices=DailyRound.InsulinIntakeFrequencyChoice, required=False
)
clone_last = serializers.BooleanField(
write_only=True, default=False, required=False
)
last_edited_by = UserBaseMinimumSerializer(read_only=True)
created_by = UserBaseMinimumSerializer(read_only=True)
# bed_object = BedSerializer(read_only=True)
class Meta:
model = DailyRound
read_only_fields = (
"last_updated_by_telemedicine",
"created_by_telemedicine",
"glasgow_total_calculated",
"total_intake_calculated",
"total_output_calculated",
"consultation",
)
exclude = ("deleted",)
def update(self, instance, validated_data):
instance.last_edited_by = self.context["request"].user
if instance.consultation.discharge_date:
raise ValidationError(
{"consultation": ["Discharged Consultation data cannot be updated"]}
)
if (
"action" in validated_data
or "consultation__review_interval" in validated_data
):
patient = instance.consultation.patient
if "action" in validated_data:
action = validated_data.pop("action")
patient.action = action
if "consultation__review_interval" in validated_data:
review_interval = validated_data.pop("consultation__review_interval")
instance.consultation.review_interval = review_interval
instance.consultation.save(update_fields=["review_interval"])
if review_interval >= 0:
patient.review_time = localtime(now()) + timedelta(
minutes=review_interval
)
else:
patient.review_time = None
patient.save()
validated_data["last_updated_by_telemedicine"] = False
if self.context["request"].user == instance.consultation.assigned_to:
validated_data["last_updated_by_telemedicine"] = True
instance.consultation.save(update_fields=["last_updated_by_telemedicine"])
NotificationGenerator(
event=Notification.Event.PATIENT_CONSULTATION_UPDATE_UPDATED,
caused_by=self.context["request"].user,
caused_object=instance,
facility=instance.consultation.patient.facility,
).generate()
return super().update(instance, validated_data)
def update_last_daily_round(self, daily_round_obj):
consultation = daily_round_obj.consultation
consultation.last_daily_round = daily_round_obj
consultation.save()
NotificationGenerator(
event=Notification.Event.PATIENT_CONSULTATION_UPDATE_CREATED,
caused_by=self.context["request"].user,
caused_object=daily_round_obj,
facility=daily_round_obj.consultation.patient.facility,
).generate()
def create(self, validated_data):
consultation: PatientConsultation = validated_data["consultation"]
# Authorisation Checks
if (
not get_home_facility_queryset(self.context["request"].user)
.filter(id=consultation.facility_id)
.exists()
):
raise ValidationError(
{"facility": "Daily Round creates are only allowed in home facility"}
)
# Authorisation Checks End
# Patient needs to have a bed assigned for admission
if (
not consultation.current_bed
and consultation.suggestion == SuggestionChoices.A
):
raise ValidationError(
{
"bed": "Patient does not have a bed assigned. Please assign a bed first"
}
)
with transaction.atomic():
if (
validated_data.get("rounds_type")
== DailyRound.RoundsType.TELEMEDICINE.value
and consultation.suggestion != SuggestionChoices.DC
):
raise ValidationError(
{
"rounds_type": "Telemedicine Rounds are only allowed for Domiciliary Care patients"
}
)
if "clone_last" in validated_data:
should_clone = validated_data.pop("clone_last")
if should_clone:
last_objects = DailyRound.objects.filter(
consultation=consultation
).order_by("-created_date")
if not last_objects.exists():
raise ValidationError(
{"daily_round": "No Daily Round record available to copy"}
)
if "rounds_type" not in validated_data:
raise ValidationError(
{"daily_round": "Rounds type is required to clone"}
)
rounds_type = validated_data.get("rounds_type")
if rounds_type == DailyRound.RoundsType.NORMAL.value:
fields_to_clone = [
"consultation_id",
"patient_category",
"taken_at",
"additional_symptoms",
"other_symptoms",
"physical_examination_info",
"other_details",
"bp",
"pulse",
"resp",
"temperature",
"rhythm",
"rhythm_detail",
"ventilator_spo2",
"consciousness_level",
]
cloned_daily_round_obj = DailyRound()
for field in fields_to_clone:
value = getattr(last_objects[0], field)
setattr(cloned_daily_round_obj, field, value)
else:
cloned_daily_round_obj = last_objects[0]
cloned_daily_round_obj.pk = None
cloned_daily_round_obj.rounds_type = rounds_type
cloned_daily_round_obj.created_by = self.context["request"].user
cloned_daily_round_obj.last_edited_by = self.context["request"].user
cloned_daily_round_obj.created_date = timezone.now()
cloned_daily_round_obj.modified_date = timezone.now()
cloned_daily_round_obj.external_id = uuid4()
cloned_daily_round_obj.save()
self.update_last_daily_round(cloned_daily_round_obj)
return self.update(cloned_daily_round_obj, validated_data)
if (
"action" in validated_data
or "consultation__review_interval" in validated_data
):
patient = validated_data["consultation"].patient
if "action" in validated_data:
action = validated_data.pop("action")
patient.action = action
if "consultation__review_interval" in validated_data:
review_interval = validated_data.pop(
"consultation__review_interval"
)
if review_interval >= 0:
validated_data["consultation"].review_interval = review_interval
patient.review_time = localtime(now()) + timedelta(
minutes=review_interval
)
else:
patient.review_time = None
patient.save()
validated_data["created_by_telemedicine"] = False
validated_data["last_updated_by_telemedicine"] = False
if (
self.context["request"].user
== validated_data["consultation"].assigned_to
):
validated_data["created_by_telemedicine"] = True
validated_data["last_updated_by_telemedicine"] = True
daily_round_obj: DailyRound = super().create(validated_data)
daily_round_obj.created_by = self.context["request"].user
daily_round_obj.last_edited_by = self.context["request"].user
daily_round_obj.consultation.last_updated_by_telemedicine = validated_data[
"last_updated_by_telemedicine"
]
daily_round_obj.consultation.save(
update_fields=["last_updated_by_telemedicine", "review_interval"]
)
daily_round_obj.save(
update_fields=[
"created_by",
"last_edited_by",
]
)
if daily_round_obj.rounds_type != DailyRound.RoundsType.AUTOMATED.value:
self.update_last_daily_round(daily_round_obj)
create_consultation_events(
daily_round_obj.consultation_id,
daily_round_obj,
daily_round_obj.created_by_id,
daily_round_obj.created_date,
)
return daily_round_obj
def validate(self, attrs):
validated = super().validate(attrs)
validated["consultation"] = self.context["consultation"]
if validated["consultation"].discharge_date:
raise ValidationError(
{"consultation": ["Discharged Consultation data cannot be updated"]}
)
if "action" in validated:
if validated["action"] == PatientRegistration.ActionEnum.REVIEW:
if "consultation__review_interval" not in validated:
raise ValidationError(
{
"review_interval": [
"This field is required as the patient has been requested Review."
]
}
)
if validated["consultation__review_interval"] <= 0:
raise ValidationError(
{
"review_interval": [
"This field value is must be greater than 0."
]
}
)
if "bed" in validated:
external_id = validated.pop("bed")["external_id"]
if external_id:
# TODO add authorisation checks
bed_object = Bed.objects.filter(external_id=external_id).first()
if not bed_object:
raise ValidationError({"bed": ["Object not found."]})
validated["bed_id"] = bed_object.id
return validated