Beispiel #1
0
 def _get_navigation_wizard(self):
     return NavigationWizard(self.request.user,
                             self.registry_model,
                             self.patient_model,
                             NavigationFormType.CLINICIAN,
                             None,
                             None)
Beispiel #2
0
    def post(self, request, registry_code, patient_id):
        user = request.user
        patient = Patient.objects.get(id=patient_id)
        patient_type = patient.patient_type
        security_check_user_patient(user, patient)

        patient_relatives_forms = None
        actions = []

        if patient.user:
            patient_user = patient.user
        else:
            patient_user = None

        registry_model = Registry.objects.get(code=registry_code)

        context_launcher = RDRFContextLauncherComponent(
            request.user, registry_model, patient)
        patient_info = RDRFPatientInfoComponent(registry_model, patient)

        if registry_model.patient_fields:
            patient_form_class = self._create_registry_specific_patient_form_class(
                user, PatientForm, registry_model, patient)
        else:
            patient_form_class = PatientForm

        patient_form = patient_form_class(request.POST,
                                          request.FILES,
                                          instance=patient,
                                          user=request.user,
                                          registry_model=registry_model)

        country_code = request.POST.get('country_of_birth')
        patient_form.fields['country_of_birth'].choices = [(country_code,
                                                            country_code)]

        kin_country_code = request.POST.get('next_of_kin_country')
        kin_state_code = request.POST.get('next_of_kin_state')
        patient_form.fields['next_of_kin_country'].choices = [
            (kin_country_code, kin_country_code)
        ]
        patient_form.fields['next_of_kin_state'].choices = [(kin_state_code,
                                                             kin_state_code)]

        patient_address_form_set = inlineformset_factory(
            Patient, PatientAddress, form=PatientAddressForm, fields="__all__")
        address_to_save = patient_address_form_set(request.POST,
                                                   instance=patient,
                                                   prefix="patient_address")

        index = 0
        for f in address_to_save.forms:
            country_field_name = 'patient_address-' + str(index) + '-country'
            patient_country_code = request.POST.get(country_field_name)
            state_field_name = 'patient_address-' + str(index) + '-state'
            patient_state_code = request.POST.get(state_field_name)
            index += 1
            f.fields['country'].choices = [(patient_country_code,
                                            patient_country_code)]
            f.fields['state'].choices = [(patient_state_code,
                                          patient_state_code)]

        patient_relatives_forms = None

        if patient.is_index and registry_model.get_metadata_item(
                "family_linkage"):
            patient_relatives_formset = inlineformset_factory(
                Patient,
                PatientRelative,
                fk_name='patient',
                form=PatientRelativeForm,
                extra=0,
                can_delete=True,
                fields="__all__")

            patient_relatives_forms = patient_relatives_formset(
                request.POST, instance=patient, prefix="patient_relative")

            forms = [patient_form, address_to_save, patient_relatives_forms]
        else:
            forms = [patient_form, address_to_save]

        valid_forms = []
        error_messages = []

        for form in forms:
            if not form.is_valid():
                valid_forms.append(False)
                if isinstance(form.errors, list):
                    for error_dict in form.errors:
                        for field in error_dict:
                            error_messages.append("%s: %s" %
                                                  (field, error_dict[field]))
                else:
                    for field in form.errors:
                        for error in form.errors[field]:
                            error_messages.append(error)
            else:
                valid_forms.append(True)

        if registry_model.get_metadata_item("patient_form_doctors"):
            patient_doctor_form_set = inlineformset_factory(
                Patient,
                PatientDoctor,
                form=PatientDoctorForm,
                fields="__all__")
            doctors_to_save = patient_doctor_form_set(request.POST,
                                                      instance=patient,
                                                      prefix="patient_doctor")
            valid_forms.append(doctors_to_save.is_valid())

        if all(valid_forms):
            if registry_model.get_metadata_item("patient_form_doctors"):
                doctors_to_save.save()
            address_to_save.save()
            patient_instance = patient_form.save()
            patient_instance.patient_type = patient_type
            patient_instance.save()

            patient_instance.sync_patient_relative()

            if patient_user and not patient_instance.user:
                patient_instance.user = patient_user
                patient_instance.save()

            registry_specific_fields_handler = RegistrySpecificFieldsHandler(
                registry_model, patient_instance)
            registry_specific_fields_handler.save_registry_specific_data_in_mongo(
                request)

            patient, form_sections = self._get_patient_and_forms_sections(
                patient_id, registry_code, request)

            if patient_relatives_forms:
                self.create_patient_relatives(patient_relatives_forms, patient,
                                              registry_model)

            context = {
                "forms": form_sections,
                "patient": patient,
                "context_launcher": context_launcher.html,
                "message": _("Patient's details saved successfully"),
                "error_messages": [],
            }
        else:
            error_messages = get_error_messages(forms)
            if not registry_model.get_metadata_item("patient_form_doctors"):
                doctors_to_save = None
            patient, form_sections = self._get_patient_and_forms_sections(
                patient_id,
                registry_code,
                request,
                patient_form,
                address_to_save,
                doctors_to_save,
                patient_relatives_forms=patient_relatives_forms)

            context = {
                "forms": form_sections,
                "patient": patient,
                "actions": actions,
                "context_launcher": context_launcher.html,
                "errors": True,
                "error_messages": error_messages,
            }

        wizard = NavigationWizard(request.user, registry_model, patient,
                                  NavigationFormType.DEMOGRAPHICS, None, None)

        family_linkage_panel = FamilyLinkagePanel(request.user, registry_model,
                                                  patient)

        context["next_form_link"] = wizard.next_link
        context["previous_form_link"] = wizard.previous_link
        context["patient_info"] = patient_info.html

        context["registry_code"] = registry_code
        context["patient_id"] = patient.id
        context["location"] = _("Demographics")
        context["form_links"] = []
        context["not_linked"] = not patient.is_linked
        context["family_linkage_panel"] = family_linkage_panel.html
        context["show_archive_button"] = request.user.can_archive
        context["archive_patient_url"] = patient.get_archive_url(
            registry_model) if request.user.can_archive else ""
        context["consent"] = consent_status_for_patient(registry_code, patient)

        section_blacklist = self._check_for_blacklisted_sections(
            registry_model)
        context["section_blacklist"] = section_blacklist

        hidden_sectionlist = self._check_for_hidden_section(
            request.user, registry_model, form_sections)
        context["hidden_sectionlist"] = hidden_sectionlist

        if request.user.is_parent:
            context['parent'] = ParentGuardian.objects.get(user=request.user)

        return render(request, 'rdrf_cdes/patient_edit.html', context)
Beispiel #3
0
    def get(self, request, registry_code, patient_id):
        if not request.user.is_authenticated:
            patient_edit_url = reverse('patient_edit',
                                       args=[
                                           registry_code,
                                           patient_id,
                                       ])
            login_url = reverse('two_factor:login')
            return redirect("%s?next=%s" % (login_url, patient_edit_url))

        registry_model = Registry.objects.get(code=registry_code)

        section_blacklist = self._check_for_blacklisted_sections(
            registry_model)

        patient, form_sections = self._get_patient_and_forms_sections(
            patient_id, registry_code, request)

        security_check_user_patient(request.user, patient)

        if registry_model.has_feature("consent_checks"):
            from rdrf.helpers.utils import consent_check
            if not consent_check(registry_model, request.user, patient,
                                 "see_patient"):
                raise PermissionDenied

        context_launcher = RDRFContextLauncherComponent(
            request.user, registry_model, patient)
        patient_info = RDRFPatientInfoComponent(registry_model, patient)

        family_linkage_panel = FamilyLinkagePanel(request.user, registry_model,
                                                  patient)

        context = {
            "location":
            "Demographics",
            "context_launcher":
            context_launcher.html,
            "patient_info":
            patient_info.html,
            "forms":
            form_sections,
            "family_linkage_panel":
            family_linkage_panel.html,
            "patient":
            patient,
            "proms_link":
            self._get_proms_link(registry_model, patient),
            "patient_id":
            patient.id,
            "registry_code":
            registry_code,
            "form_links": [],
            "show_archive_button":
            request.user.can_archive,
            "archive_patient_url":
            patient.get_archive_url(registry_model)
            if request.user.can_archive else "",
            "consent":
            consent_status_for_patient(registry_code, patient),
            "section_blacklist":
            section_blacklist
        }
        if request.GET.get('just_created', False):
            context["message"] = _("Patient added successfully")

        context["not_linked"] = not patient.is_linked

        wizard = NavigationWizard(request.user, registry_model, patient,
                                  NavigationFormType.DEMOGRAPHICS, None, None)

        context["next_form_link"] = wizard.next_link
        context["previous_form_link"] = wizard.previous_link

        if request.user.is_parent:
            context['parent'] = ParentGuardian.objects.get(user=request.user)

        hidden_sectionlist = self._check_for_hidden_section(
            request.user, registry_model, form_sections)
        context["hidden_sectionlist"] = hidden_sectionlist

        return render(request, 'rdrf_cdes/patient_edit.html', context)