def update(self, instance, validated_data):

        instance.last_edited_by = self.context["request"].user

        if instance.discharge_date:
            raise ValidationError({
                "consultation":
                ["Discharged Consultation data cannot be updated"]
            })

        if instance.suggestion == SuggestionChoices.OP:
            instance.discharge_date = localtime(now())
            instance.save()

        if "action" in validated_data or "review_time" in validated_data:
            patient = instance.patient

            if "action" in validated_data:
                action = validated_data.pop("action")
                patient.action = action

            if "review_time" in validated_data:
                review_time = validated_data.pop("review_time")
                if review_time >= 0:
                    patient.review_time = localtime(
                        now()) + timedelta(minutes=review_time)
            patient.save()

        validated_data["last_updated_by_telemedicine"] = self.context[
            "request"].user == instance.assigned_to

        if "is_kasp" in validated_data:
            if validated_data["is_kasp"] and (not instance.is_kasp):
                validated_data["kasp_enabled_date"] = localtime(now())

        if "assigned_to" in validated_data:
            if validated_data["assigned_to"] != instance.assigned_to:
                NotificationGenerator(
                    event=Notification.Event.PATIENT_CONSULTATION_ASSIGNMENT,
                    caused_by=self.context["request"].user,
                    caused_object=instance,
                    facility=instance.patient.facility,
                    notification_mediums=[
                        Notification.Medium.SYSTEM,
                        Notification.Medium.WHATSAPP
                    ],
                ).generate()

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

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

        return consultation
Beispiel #2
0
    def create(self, validated_data):

        if "action" in validated_data or "review_time" in validated_data:
            patient = validated_data["consultation"].patient

            if "action" in validated_data:
                action = validated_data.pop("action")
                patient.action = action

            if "review_time" in validated_data:
                review_time = validated_data.pop("review_time")
                if review_time >= 0:
                    patient.review_time = localtime(
                        now()) + timedelta(minutes=review_time)
            patient.save()

        daily_round_obj = super().create(validated_data)

        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()

        return daily_round_obj
Beispiel #3
0
    def update(self, instance, validated_data):

        if instance.consultation.discharge_date:
            raise ValidationError({
                "consultation":
                [f"Discharged Consultation data cannot be updated"]
            })

        if "action" in validated_data or "review_time" in validated_data:
            patient = instance.consultation.patient

            if "action" in validated_data:
                action = validated_data.pop("action")
                patient.action = action

            if "review_time" in validated_data:
                review_time = validated_data.pop("review_time")
                if review_time >= 0:
                    patient.review_time = localtime(
                        now()) + timedelta(minutes=review_time)
            patient.save()

        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)
Beispiel #4
0
 def update(self, instance, validated_data):
     consultation = super().update(instance, validated_data)
     NotificationGenerator(
         event=Notification.Event.PATIENT_CONSULTATION_UPDATED,
         caused_by=self.context["request"].user,
         caused_object=consultation,
         facility=consultation.patient.facility,
     ).generate()
     return consultation
    def create(self, validated_data):

        action = -1
        review_time = -1
        if "action" in validated_data:
            action = validated_data.pop("action")
        if "review_time" in validated_data:
            review_time = validated_data.pop("review_time")

        if validated_data["patient"].last_consultation:
            if self.context["request"].user == validated_data[
                    "patient"].last_consultation.assigned_to:
                raise ValidationError(
                    {
                        "Permission Denied":
                        "Only Facility Staff can create consultation for a Patient"
                    }, )

        if validated_data["patient"].last_consultation:
            if not validated_data["patient"].last_consultation.discharge_date:
                raise ValidationError({
                    "consultation":
                    "Exists please Edit Existing Consultation"
                })

        if "is_kasp" in validated_data:
            if validated_data["is_kasp"]:
                validated_data["kasp_enabled_date"] = localtime(now())

        consultation = super().create(validated_data)
        consultation.created_by = self.context["request"].user
        consultation.last_edited_by = self.context["request"].user
        consultation.save()

        patient = consultation.patient
        if consultation.suggestion == SuggestionChoices.OP:
            consultation.discharge_date = localtime(now())
            consultation.save()
            patient.is_active = False
            patient.allow_transfer = True
        patient.last_consultation = consultation

        if action != -1:
            patient.action = action
        if review_time > 0:
            patient.review_time = localtime(
                now()) + timedelta(minutes=review_time)

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

        return consultation
    def create(self, request, *args, **kwargs):

        if "investigations" not in request.data:
            return Response({"investigation": "is required"},
                            status=status.HTTP_400_BAD_REQUEST)

        investigations = request.data["investigations"]

        if not isinstance(investigations, list):
            return Response({"error": "Data must be a list"},
                            status=status.HTTP_400_BAD_REQUEST)

        consultation = PatientConsultation.objects.get(
            external_id=kwargs.get("consultation_external_id"))
        consultation_id = consultation.id

        if consultation.discharge_date:
            return Response(
                {
                    "consultation":
                    ["Discharged Consultation data cannot be updated"]
                },
                status=status.HTTP_400_BAD_REQUEST,
            )

        with transaction.atomic():
            session = InvestigationSession()
            session.save()

            for value in investigations:
                value["session"] = session.id
                value["investigation"] = get_investigation_id(
                    value["investigation"])
                value["consultation"] = consultation_id

            serializer = self.get_serializer(data=investigations, many=True)
            serializer.is_valid(raise_exception=True)
            self.perform_create(serializer)

            NotificationGenerator(
                event=Notification.Event.INVESTIGATION_SESSION_CREATED,
                caused_by=request.user,
                caused_object=session,
                facility=consultation.patient.facility,
                extra_data={
                    "consultation": consultation
                },
            ).generate()

        return Response(serializer.data, status=status.HTTP_201_CREATED)
Beispiel #7
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)

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

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

            return patient
Beispiel #8
0
 def notify(self, request, *args, **kwargs):
     user = request.user
     if "facility" not in request.data:
         raise ValidationError({"facility": "is required"})
     if "message" not in request.data:
         raise ValidationError({"mesasge": "is required"})
     facilities = get_facility_queryset(user)
     facility = get_object_or_404(facilities.filter(external_id=request.data["facility"]))
     NotificationGenerator(
         event_type=Notification.EventType.CUSTOM_MESSAGE,
         event=Notification.Event.MESSAGE,
         caused_by=user,
         facility=facility,
         caused_object=user,
         message=request.data["message"],
     ).generate()
     return Response(status=status.HTTP_204_NO_CONTENT)
Beispiel #9
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
    def create(self, validated_data):

        if "action" in validated_data or "review_time" in validated_data:
            patient = validated_data["consultation"].patient

            if "action" in validated_data:
                action = validated_data.pop("action")
                patient.action = action

            if "review_time" in validated_data:
                review_time = validated_data.pop("review_time")
                if review_time >= 0:
                    patient.review_time = localtime(
                        now()) + timedelta(minutes=review_time)
            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 = super().create(validated_data)

        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"])

        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()

        return daily_round_obj
Beispiel #11
0
    def create(self, validated_data):

        action = -1
        review_time = -1
        if "action" in validated_data:
            action = validated_data.pop("action")
        if "review_time" in validated_data:
            review_time = validated_data.pop("review_time")

        consultation = super().create(validated_data)
        consultation.created_by = self.context["request"].user
        consultation.last_edited_by = self.context["request"].user
        consultation.save()

        patient = consultation.patient
        if consultation.suggestion == SuggestionChoices.OP:
            consultation.discharge_date = localtime(now())
            consultation.save()
            patient.is_active = False
            patient.allow_transfer = True
        patient.last_consultation = consultation

        if action != -1:
            patient.action = action
        if review_time > 0:
            patient.review_time = localtime(
                now()) + timedelta(minutes=review_time)

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

        return consultation
Beispiel #12
0
    def update(self, instance, validated_data):

        LIMITED_RECIEVING_STATUS_ = [
            "DESTINATION APPROVED",
            "DESTINATION REJECTED",
            "COMPLETED",
        ]
        LIMITED_RECIEVING_STATUS = [
            REVERSE_SHIFTING_STATUS_CHOICES[x]
            for x in LIMITED_RECIEVING_STATUS_
        ]
        LIMITED_SHIFTING_STATUS_ = [
            "APPROVED",
            "REJECTED",
            "PATIENT TO BE PICKED UP",
            "TRANSPORTATION TO BE ARRANGED",
            "TRANSFER IN PROGRESS",
            "COMPLETED",
            "PENDING",
            "ON HOLD",
        ]
        LIMITED_SHIFTING_STATUS = [
            REVERSE_SHIFTING_STATUS_CHOICES[x]
            for x in LIMITED_SHIFTING_STATUS_
        ]
        LIMITED_ORGIN_STATUS = []

        RECIEVING_REQUIRED_STATUS_ = [
            "APPROVED",
            "REJECTED",
            "DESTINATION APPROVED",
            "DESTINATION REJECTED",
            "TRANSPORTATION TO BE ARRANGED",
            "PATIENT TO BE PICKED UP",
            "TRANSFER IN PROGRESS",
            "COMPLETED",
        ]
        RECIEVING_REQUIRED_STATUS = [
            REVERSE_SHIFTING_STATUS_CHOICES[x]
            for x in RECIEVING_REQUIRED_STATUS_
        ]

        user = self.context["request"].user

        if "is_kasp" in validated_data:
            if validated_data[
                    "is_kasp"] != instance.is_kasp:  # Check only when changed
                if not has_facility_permission(
                        user, instance.shifting_approving_facility):
                    raise ValidationError({"kasp": ["Permission Denied"]})

        if "breathlessness_level" in validated_data:
            if not has_facility_permission(
                    user, instance.shifting_approving_facility):
                del validated_data["breathlessness_level"]

        if "status" in validated_data:
            if validated_data["status"] in LIMITED_RECIEVING_STATUS:
                if instance.assigned_facility:
                    if not has_facility_permission(user,
                                                   instance.assigned_facility):
                        raise ValidationError(
                            {"status": ["Permission Denied"]})
            elif validated_data["status"] in LIMITED_SHIFTING_STATUS:
                if not has_facility_permission(
                        user, instance.shifting_approving_facility):
                    raise ValidationError({"status": ["Permission Denied"]})

        # Dont allow editing origin or patient
        if "orgin_facility" in validated_data:
            validated_data.pop("orgin_facility")
        if "patient" in validated_data:
            validated_data.pop("patient")

        if "shifting_approving_facility" in validated_data:
            shifting_approving_facility_external_id = validated_data.pop(
                "shifting_approving_facility")["external_id"]
            if shifting_approving_facility_external_id:
                validated_data[
                    "shifting_approving_facility_id"] = Facility.objects.get(
                        external_id=shifting_approving_facility_external_id).id

        if "assigned_facility" in validated_data:
            assigned_facility_external_id = validated_data.pop(
                "assigned_facility")["external_id"]
            if assigned_facility_external_id:
                validated_data["assigned_facility_id"] = Facility.objects.get(
                    external_id=assigned_facility_external_id).id

        if (not instance.assigned_facility) and ("assigned_facility"
                                                 not in validated_data):
            if "status" in validated_data:
                if validated_data["status"] in LIMITED_RECIEVING_STATUS:
                    raise ValidationError({
                        "status": [
                            "Destination Facility is required for moving to this stage."
                        ]
                    })

        instance.last_edited_by = self.context["request"].user

        old_status = instance.status

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

        if validated_data["status"] != old_status:
            if validated_data["status"] == 40:
                NotificationGenerator(
                    event=Notification.Event.SHIFTING_UPDATED,
                    caused_by=self.context["request"].user,
                    caused_object=new_instance,
                    facility=new_instance.shifting_approving_facility,
                    generate_sms=True,
                ).generate()

        return new_instance