Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added more fields to Consultation Form #979

Closed
wants to merge 44 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
9e01ad5
Added Patient Health Details model
aeswibon Aug 15, 2022
c41a133
Added migrations for patient health details model
aeswibon Aug 15, 2022
7ae6f87
Added migrations for new fields
aeswibon Aug 15, 2022
a3726f4
Added logic to create health details for existing patients
aeswibon Aug 15, 2022
b28c257
Added Patient Health details serializer
aeswibon Aug 15, 2022
45d054f
Added viewset for patient health details
aeswibon Aug 15, 2022
864040e
Added api route for patient health details
aeswibon Aug 15, 2022
7a2766d
Added created_in_consultation in patient_consultation serializer
aeswibon Aug 31, 2022
c37a6b3
Migrated patient health serializer to new file and added new fields
aeswibon Aug 31, 2022
6bc2ad1
Migrated height and weight fields to patient health details model
aeswibon Aug 31, 2022
1b63c8b
Added migrations for migrating height and weight fields
aeswibon Aug 31, 2022
bc956b2
Minor change in import statement
aeswibon Aug 31, 2022
1da70bc
Formatted the code
aeswibon Aug 31, 2022
52840a6
Added migrations to merge conflicting migrations
aeswibon Aug 31, 2022
9bdb1cf
Added data migration for created_in_consultation field in patient hea…
aeswibon Sep 1, 2022
c177d5f
Updated the logic for creation of patient health details instance
aeswibon Sep 1, 2022
8bd2609
Updated the logic of creating new patient health record
aeswibon Sep 3, 2022
44fb81d
Added the logic of updating the patient health record
aeswibon Sep 3, 2022
63c02b9
Fixed blood group field for csv
aeswibon Sep 13, 2022
4203a8b
Moved VACCINE_CHOICES to patient_base file
aeswibon Sep 13, 2022
02f5898
Added Vaccine model
aeswibon Sep 13, 2022
9737a01
Updated blood_group field for csv and reformatted the code
aeswibon Sep 13, 2022
d987f5f
Added migrations for merging previous migrations
aeswibon Sep 13, 2022
2bb7d28
* Added migrations for Vaccine model
aeswibon Sep 13, 2022
2857e47
* Added serializer for Vaccine model
aeswibon Sep 13, 2022
56788da
Removed vaccine name from patient serializer
aeswibon Sep 13, 2022
69edd0b
Removed vaccine related field from patient viewset
aeswibon Sep 13, 2022
fc4d332
Added logic for creating/updating vaccine records
aeswibon Sep 13, 2022
e8dcdad
Reformatted the code
aeswibon Sep 13, 2022
521dfb8
* Added new notification events for health details
aeswibon Sep 13, 2022
86f9d18
Reformatted the code
aeswibon Sep 13, 2022
54ba51d
Updated migrations and pre-commit config
aeswibon Sep 21, 2022
1abf293
Updated health details serializer methods and reformatted the code
aeswibon Sep 21, 2022
935bf6d
Reverted the migrations
aeswibon Sep 21, 2022
c629ab6
Updated Migrations
aeswibon Sep 22, 2022
ce73065
Removed health details create/update notification
aeswibon Oct 3, 2022
2514012
Removed medical history API
aeswibon Oct 3, 2022
26f58bf
Updated the logic of Health Details API
aeswibon Oct 3, 2022
689107e
Updated Vaccination field of health details API
aeswibon Oct 5, 2022
75d5d0c
Updated value of health_details field in consultation API
aeswibon Oct 5, 2022
04a5067
Added migration to resolve conflicting migrations
aeswibon Oct 5, 2022
c824156
Updated health_details and medical_history API
aeswibon Oct 19, 2022
91e0f06
Fix migrations
aeswibon Oct 19, 2022
1485db8
Fixed the bug in health details serializer
aeswibon Oct 19, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Updated the logic of Health Details API
* Switched to serializer to create/update health details record
* Added create/update method in health details serializer
* Added auth while creating health details record
* Switched to validated_data for health details data
* Reformatted the code
  • Loading branch information
aeswibon committed Oct 19, 2022
commit 26f58bf97070d2454396e355fd4c6296234f157b
188 changes: 79 additions & 109 deletions care/facility/api/serializers/patient_consultation.py
Original file line number Diff line number Diff line change
@@ -8,17 +8,12 @@
from care.facility.api.serializers.bed import ConsultationBedSerializer
from care.facility.api.serializers.daily_round import DailyRoundSerializer
from care.facility.api.serializers.facility import FacilityBasicInfoSerializer
from care.facility.models import (
CATEGORY_CHOICES,
COVID_CATEGORY_CHOICES,
Facility,
PatientRegistration,
)
from care.facility.api.serializers.patient_health_details import (
PatientHealthDetailsSerializer,
)
from care.facility.models import (
CATEGORY_CHOICES,
COVID_CATEGORY_CHOICES,
Facility,
PatientRegistration,
)
@@ -44,9 +39,7 @@

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,
@@ -65,14 +58,10 @@ 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
@@ -89,14 +78,21 @@ 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)
last_daily_round = DailyRoundSerializer(read_only=True)
last_health_details = PatientHealthDetailsSerializer(read_only=True)
health_details = ExternalIdSerializerField(
queryset=PatientHealthDetails.objects.all(), required=False
)

health_details_object = PatientHealthDetailsSerializer(
source="last_health_details",
read_only=True,
)

new_health_details = PatientHealthDetailsSerializer(required=False)

current_bed = ConsultationBedSerializer(read_only=True)

@@ -121,9 +117,7 @@ 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(
@@ -139,7 +133,7 @@ class Meta:
"created_by",
"kasp_enabled_date",
)
exclude = ("deleted", "external_id")
exclude = ("deleted", "external_id", "last_health_details")

def validate_bed_number(self, bed_number):
try:
@@ -155,11 +149,7 @@ 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:
@@ -190,52 +180,44 @@ def update(self, instance, validated_data):
validated_data["kasp_enabled_date"] = localtime(now())

_temp = instance.assigned_to
health_details_data = validated_data.pop("new_health_details", None)

consultation = super().update(instance, validated_data)

try:
health_details_data = self.context["request"].data[
"new_health_details"
]
vaccination_history = health_details_data.pop(
"vaccination_history", []
vaccination_history = health_details_data.pop("vaccination_history", [])

serializer = PatientHealthDetailsSerializer(
data={
"patient": consultation.patient.external_id.hex,
"facility": consultation.facility.external_id.hex,
"consultation": consultation.external_id.hex,
**health_details_data,
},
context={
"request": self.context["request"],
},
)
PatientHealthDetails.objects.filter(
id=consultation.last_health_details.id
).update(**self.context["request"].data["new_health_details"])
serializer.is_valid(raise_exception=True)
serializer.save()

consultation.last_health_details = (
PatientHealthDetails.objects.get(
id=consultation.last_health_details.id
)
)
health_details = PatientHealthDetails.objects.filter(
consultation=consultation
).latest("external_id")

consultation.health_details = health_details

vaccines = []
for vaccine in vaccination_history:
vaccines.append(
Vaccine(
health_details=consultation.last_health_details,
**vaccine,
)
)
vaccines.append(Vaccine(health_details=health_details, **vaccine))
if vaccines:
Vaccine.objects.bulk_create(vaccines, ignore_conflicts=True)
aeswibon marked this conversation as resolved.
Show resolved Hide resolved
consultation.save(update_fields=["last_health_details"])

NotificationGenerator(
event=Notification.Event.PATIENT_HEALTH_DETAILS_UPDATED,
caused_by=self.context["request"].user,
caused_object=consultation.last_health_details,
facility=consultation.facility,
).generate()
except KeyError:
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,
@@ -267,16 +249,12 @@ 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
@@ -295,16 +273,15 @@ 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:
if validated_data["is_kasp"]:
validated_data["kasp_enabled_date"] = localtime(now())

bed = validated_data.pop("bed", None)
health_details_data = validated_data.pop("new_health_details", None)

validated_data["facility_id"] = validated_data[
"patient"
@@ -315,47 +292,50 @@ def create(self, validated_data):
consultation.save()

try:
health_details_data = self.context["request"].data[
"new_health_details"
]
vaccination_history = health_details_data.pop(
"vaccination_history", []
vaccination_history = health_details_data.pop("vaccination_history", [])

serializer = PatientHealthDetailsSerializer(
data={
"patient": consultation.patient.external_id.hex,
"facility": consultation.facility.external_id.hex,
"consultation": consultation.external_id.hex,
**health_details_data,
},
context={
"request": self.context["request"],
},
)
serializer.is_valid(raise_exception=True)
serializer.save()

health_details = PatientHealthDetails(
patient=consultation.patient,
facility=consultation.facility,
consultation=consultation,
**health_details_data,
)
health_details.save()
health_details = PatientHealthDetails.objects.filter(
consultation=consultation
).latest("external_id")

consultation.health_details = health_details

vaccines = []
for vaccine in vaccination_history:
vaccines.append(
Vaccine(health_details=health_details, **vaccine)
)
vaccines.append(Vaccine(health_details=health_details, **vaccine))
if vaccines:
Vaccine.objects.bulk_create(vaccines, ignore_conflicts=True)

consultation.last_health_details = health_details
consultation.save(update_fields=["last_health_details"])

NotificationGenerator(
event=Notification.Event.PATIENT_HEALTH_DETAILS_CREATED,
caused_by=self.context["request"].user,
caused_object=health_details,
facility=consultation.facility,
).generate()
except KeyError as error:
if consultation.patient.last_consultation is None:
raise ValidationError(
{
"last_health_details": [
"health_details": [
"Please provide the health details of the patient"
]
}
) from error

else:
consultation.last_health_details = (
consultation.patient.last_consultation.last_health_details
)
consultation.save(update_fields=["last_health_details"])

if bed:
consultation_bed = ConsultationBed(
bed=bed,
@@ -379,9 +359,7 @@ 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(
@@ -410,9 +388,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": [
@@ -445,11 +423,7 @@ 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

@@ -483,12 +457,8 @@ 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
33 changes: 31 additions & 2 deletions care/facility/api/serializers/patient_health_details.py
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@
from care.facility.models.facility import Facility
from care.facility.models.patient import Vaccine
from care.facility.models.patient_base import BLOOD_GROUP_CHOICES, VACCINE_CHOICES
from care.utils.queryset.facility import get_home_facility_queryset
from care.utils.serializer.external_id_field import ExternalIdSerializerField


@@ -30,8 +31,12 @@ class PatientHealthDetailsSerializer(serializers.ModelSerializer):

id = serializers.CharField(source="external_id", read_only=True)

patient = ExternalIdSerializerField(queryset=PatientRegistration.objects.all())
facility = ExternalIdSerializerField(queryset=Facility.objects.all())
patient = ExternalIdSerializerField(
queryset=PatientRegistration.objects.all(), required=False
)
facility = ExternalIdSerializerField(
queryset=Facility.objects.all(), required=False
)
consultation = ExternalIdSerializerField(
queryset=PatientConsultation.objects.all(), required=False
)
@@ -44,3 +49,27 @@ class PatientHealthDetailsSerializer(serializers.ModelSerializer):
class Meta:
model = PatientHealthDetails
exclude = ("deleted", "external_id")

def create(self, validated_data):
consultation = validated_data["consultation"]
allowed_facilities = get_home_facility_queryset(self.context["request"].user)
if not allowed_facilities.filter(
id=self.validated_data["patient"].facility.id
).exists():
raise serializers.ValidationError(
{
"patient": "Patient Health Details creates are only allowed in home facility"
}
)

health_details = super().create(validated_data)
consultation.last_health_details = health_details
consultation.save(update_fields=["last_health_details"])
return health_details

def update(self, instance, validated_data):
consultation = validated_data["consultation"]
health_details = super().update(instance, validated_data)
consultation.last_health_details = health_details
consultation.save(update_fields=["last_health_details"])
return health_details