Ejemplo n.º 1
0
    def create(self, validated_data):
        with transaction.atomic():
            medical_history = validated_data.pop("medical_history", [])
            meta_info = validated_data.pop("meta_info", {})
            contacted_patients = validated_data.pop("contacted_patients", [])

            if "facility" in validated_data:
                external_id = validated_data.pop("facility")["external_id"]
                if external_id:
                    validated_data["facility_id"] = Facility.objects.get(
                        external_id=external_id
                    ).id

            validated_data["created_by"] = self.context["request"].user
            patient = super().create(validated_data)
            diseases = []

            for disease in medical_history:
                diseases.append(Disease(patient=patient, **disease))
            if diseases:
                Disease.objects.bulk_create(diseases, ignore_conflicts=True)

            if meta_info:
                meta_info_obj = PatientMetaInfo.objects.create(**meta_info)
                patient.meta_info = meta_info_obj
                patient.save()

            if contacted_patients:
                contacted_patient_objs = [
                    PatientContactDetails(**data, patient=patient)
                    for data in contacted_patients
                ]
                PatientContactDetails.objects.bulk_create(contacted_patient_objs)

            patient.last_edited = self.context["request"].user
            patient.save()

            NotificationGenerator(
                event=Notification.Event.PATIENT_CREATED,
                caused_by=self.context["request"].user,
                caused_object=patient,
                facility=patient.facility,
            ).generate()

            return patient
Ejemplo n.º 2
0
    def update(self, instance, validated_data):
        with transaction.atomic():
            medical_history = validated_data.pop("medical_history", [])
            meta_info = validated_data.pop("meta_info", {})
            contacted_patients = validated_data.pop("contacted_patients", [])

            if "facility" in validated_data:
                external_id = validated_data.pop("facility")["external_id"]
                if external_id:
                    validated_data["facility_id"] = Facility.objects.get(
                        external_id=external_id).id

            patient = super().update(instance, validated_data)
            Disease.objects.filter(patient=patient).update(deleted=True)
            diseases = []
            for disease in medical_history:
                diseases.append(Disease(patient=patient, **disease))
            if diseases:
                Disease.objects.bulk_create(diseases, ignore_conflicts=True)

            if meta_info:
                for key, value in meta_info.items():
                    setattr(patient.meta_info, key, value)
                patient.meta_info.save()

            if self.partial is not True:  # clear the list and enter details if PUT
                patient.contacted_patients.all().delete()

            if contacted_patients:
                contacted_patient_objs = [
                    PatientContactDetails(**data, patient=patient)
                    for data in contacted_patients
                ]
                PatientContactDetails.objects.bulk_create(
                    contacted_patient_objs)

            return patient
Ejemplo n.º 3
0
    def test_patient_put(self):
        """
        Test user can update their patient details
            - test status code
            - test response.json()
            - test data from database
        """
        patient = self.clone_object(self.patient)
        patient.created_by = self.user
        patient.contacted_patients.add(
            PatientContactDetails(
                **{
                    "patient_in_contact": self.patient,
                    "relation_with_patient": PatientContactDetails.RelationEnum.FAMILY_MEMBER.value,
                    "mode_of_contact": PatientContactDetails.ModeOfContactEnum.CLEANED_USED_ITEMS.value,
                    "date_of_last_contact": datetime.datetime(2020, 4, 1),
                    "is_primary": True,
                    "condition_of_contact_is_symptomatic": False,
                }
            ),
            bulk=False,
        )
        patient.save()
        patient.refresh_from_db()

        new_disease_status = DiseaseStatusEnum.NEGATIVE
        data = self.get_detail_representation(patient)
        data.update(
            {
                "disease_status": new_disease_status.value,
                "contacted_patients": [
                    {
                        "patient_in_contact": str(self.patient.external_id),
                        "relation_with_patient": PatientContactDetails.RelationEnum.FRIEND.name,
                        "mode_of_contact": PatientContactDetails.ModeOfContactEnum.CO_PASSENGER_AEROPLANE.name,
                        "date_of_last_contact": "2020-04-10",
                        "is_primary": True,
                        "condition_of_contact_is_symptomatic": False,
                    }
                ],
            }
        )
        keys_to_pop = [key for key, value in data.items() if value is mock_equal] + [
            "meta_info",
            "nationality",
            "passport_no",
            # "aadhar_no",
        ]
        for key in keys_to_pop:
            data.pop(key)

        response = self.client.put(self.get_url(str(patient.external_id)), data, format="json")
        self.assertEqual(response.status_code, status.HTTP_200_OK)  # only for verified users

        patient.refresh_from_db()
        self.assertEqual(patient.disease_status, new_disease_status.value)
        self.assertEquals(patient.contacted_patients.count(), 1)

        contacted_patient = patient.contacted_patients.first()
        self.assertEquals(contacted_patient.relation_with_patient, PatientContactDetails.RelationEnum.FRIEND.value)
        self.assertEquals(
            contacted_patient.mode_of_contact, PatientContactDetails.ModeOfContactEnum.CO_PASSENGER_AEROPLANE.value
        )
        self.assertEquals(contacted_patient.date_of_last_contact, datetime.date(2020, 4, 10))