def post(self, request, registry_code, patient_id, context_id): registry_model = Registry.objects.get(code=registry_code) context_model = RDRFContext.objects.get(pk=context_id) context_form_group_model = context_model.context_form_group if context_form_group_model: naming_info = context_form_group_model.naming_info else: naming_info = self.get_naming_info(None) context_name = context_model.registry.metadata.get("context_name", "Context") patient_model = Patient.objects.get(id=patient_id) form = ContextForm(request.POST, instance=context_model) if form.is_valid(): content_type = ContentType.objects.get_for_model(patient_model) context_model = form.save(commit=False) context_model.registry = registry_model context_model.content_type = content_type context_model.content_object = patient_model context_model.save() form_links = get_form_links(request.user, context_model.object_id, context_model.registry, context_model) context = {"location": "Edit %s" % context_name, "patient_name": patient_model.display_name, 'patient_link': PatientLocator(registry_model, patient_model).link, "form_links": form_links, "context_launcher": self.get_context_launcher(request.user, registry_model, patient_model), "message": "%s saved successfully" % context_name, "error_messages": [], "registry": registry_model.code, "naming_info": naming_info, "patient_id": patient_id, "form": ContextForm(instance=context_model), } else: error_messages = get_error_messages([form]) context = {"location": "Add %s" % context_name, "errors": True, "error_messages": error_messages, "registry": registry_model.code, "patient_id": patient_id, "form_links": [], 'patient_link': PatientLocator(registry_model, patient_model).link, "context_launcher": self.get_context_launcher(request.user, registry_model, patient_model), "error_messages": error_messages, "naming_info": naming_info, "patient_name": patient_model.display_name, "form": ContextForm(request.POST)} return render(request, "rdrf_cdes/rdrf_context.html", context)
def post(self, request, registry_code): self.request = request self._set_user(request) self._set_registry_model(registry_code) forms = [] patient_form_class = self.get_form_class() patient_form = self.get_form(patient_form_class) 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)] forms.append(patient_form) self.address_formset = self._get_address_formset(request) index = 0 for f in self.address_formset.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)] forms.append(self.address_formset) if self._has_doctors_form(): self.doctor_formset = self._get_doctor_formset(request) forms.append(self.doctor_formset) if self._has_patient_relatives_form(): self.patient_relative_formset = self._get_patient_relatives_formset( request) forms.append(self.patient_relative_formset) if all([form.is_valid() for form in forms]): return self.form_valid(patient_form) else: errors = get_error_messages(forms) return self.form_invalid( patient_form=patient_form, patient_address_formset=self.address_formset, patient_doctor_formset=self.doctor_formset, patient_relative_formset=self.patient_relative_formset, errors=errors)
def get_context_data(self, **kwargs): """ :param kwargs: The kwargs supplied to render to response :return: """ patient_id = self._get_patient_id() patient_address_formset = kwargs.get("patient_address_formset", None) patient_doctor_formset = kwargs.get("patient_doctor_formset", None) patient_relative_formset = kwargs.get("patient_relative_formset", None) patient, forms_sections = self._get_patient_and_forms_sections( patient_id, self.registry_model.code, self.request, self.patient_form, patient_address_form=patient_address_formset, patient_doctor_form=patient_doctor_formset, patient_relative_form=patient_relative_formset) error_messages = get_error_messages( [pair[0] for pair in forms_sections]) num_errors = len(error_messages) kwargs["forms"] = forms_sections kwargs["patient"] = patient # Avoid spurious errors message when we first hit the Add screen: kwargs["errors"] = True if num_errors > 0 and any( [not form[0].is_valid() for form in forms_sections]) else False if "all_errors" in kwargs: kwargs["errors"] = True kwargs["error_messages"] = kwargs["all_errors"] else: kwargs["error_messages"] = error_messages kwargs["registry_code"] = self.registry_model.code kwargs["location"] = _("Demographics") section_blacklist = self._check_for_blacklisted_sections( self.registry_model) kwargs["section_blacklist"] = section_blacklist section_hiddenlist = self._check_for_hidden_section( self.request.user, self.registry_model, forms_sections) kwargs["section_hiddenlist"] = section_hiddenlist if self.request.user.is_parent: kwargs['parent'] = ParentGuardian.objects.get( user=self.request.user) return kwargs
def post(self, request, registry_code, patient_id, context_form_group_id=None): form = ContextForm(request.POST) registry_model = Registry.objects.get(code=registry_code) self.sanity_check(registry_model) patient_model = Patient.objects.get(pk=patient_id) context_form_group_model = self.get_context_form_group(context_form_group_id) naming_info = self.get_naming_info(context_form_group_id) context_name = self.get_context_name(registry_model, context_form_group_model) if form.is_valid(): patient_model = Patient.objects.get(id=patient_id) registry_model = Registry.objects.get(code=registry_code) content_type = ContentType.objects.get_for_model(patient_model) context_model = form.save(commit=False) context_model.registry = registry_model context_model.content_type = content_type context_model.content_object = patient_model if context_form_group_model: context_model.context_form_group = context_form_group_model context_model.save() context_edit = reverse('context_edit', kwargs={"registry_code": registry_model.code, "patient_id": patient_model.pk, "context_id": context_model.pk}) return HttpResponseRedirect(context_edit) else: error_messages = get_error_messages([form]) context = {"location": "Add %s" % context_name, "errors": True, "error_messages": error_messages, "registry": registry_model.code, 'patient_link': PatientLocator(registry_model, patient_model).link, "patient_id": patient_id, "form_links": [], "naming_info": naming_info, "context_launcher": self.get_context_launcher(request.user, registry_model, patient_model), "patient_name": patient_model.display_name, "form": ContextForm(request.POST)} return render(request, "rdrf_cdes/rdrf_context.html", context)
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)