def put_organisation(request, pk, json): if "status" in json: del json["status"] json = nest_data(json) data = client.put(request, f"/organisations/{pk}/", json) return data.json(), data.status_code
def get_next_form_page(self, form_pk, action, request, post_errors): form = copy.deepcopy(next(form for form in self.get_forms().forms if str(form.pk) == form_pk)) # Add form fields to validated_data if they dont exist for component in get_all_form_components(form): if component.name not in self._validated_data and component.name[:-2] not in self._validated_data: self._validated_data[component.name] = "" if action == Actions.SUBMIT or action == Actions.RETURN: validated_data = validate_data_unknown( self.get_object_pk(), self.get_action(), request, nest_data(self.get_validated_data()) ) validated_data["errors"] = validated_data.get("errors", {}) errors = validated_data["errors"] if post_errors: errors.update(post_errors) if errors: errors = flatten_data(validated_data["errors"]) errors = remove_unused_errors(errors, form) if errors: insert_hidden_fields(self.get_validated_data(), form) if action == Actions.RETURN: form = convert_form_to_summary_list_instance(form) return form_page( request, form, data=self.get_validated_data(), errors=errors, extra_data={"form_pk": form.pk, **self.additional_context}, ) if action != Actions.RETURN: next_form = get_next_form(form.pk, self.get_forms()) if next_form: insert_hidden_fields(self.get_validated_data(), next_form) return form_page( request, next_form, data=self.get_validated_data(), extra_data={"form_pk": next_form.pk, **self.additional_context}, ) elif action == Actions.CHANGE: insert_hidden_fields(self.get_validated_data(), form) return form_page( request, convert_form_to_summary_list_instance(form), data=self.get_validated_data(), extra_data={"form_pk": form.pk, **self.additional_context}, ) if self.validate_only_until_final_submission: return self.generate_summary_list() return redirect(request.path)
def _prepare_data(request, inject_data): data = request.POST.copy() # Handle lists (such as checkboxes) data = handle_lists(data) if data and inject_data: for key, value in data.items(): inject_data[key] = value data = QueryDict("", mutable=True) data.update(inject_data) # Remove form_pk and CSRF from POST data as the new form will replace them if "form_pk" in data: del data["form_pk"] if "csrfmiddlewaretoken" in data: del data["csrfmiddlewaretoken"] # Post the data to the validator and check for errors return data, nest_data(data)
def test_nest_data(self): value = { "reference": "conversation_16", "organisation.name": "Live on coffee and flowers inc.", "organisation.site.address.city": "London", "organisation.site.name": "Lemonworld", "user.first_name": "Matthew", } data = nest_data(value) self.assertEqual( data, { "reference": "conversation_16", "organisation": { "name": "Live on coffee and flowers inc.", "site": {"address": {"city": "London",}, "name": "Lemonworld",}, }, "user": {"first_name": "Matthew",}, }, )
def _post(self, request, **kwargs): self.init(request, **kwargs) self._validated_data = request.POST.copy() self._validated_data = handle_lists(self._validated_data) action = self.get_validated_data()[ACTION] form_pk = str(self.get_validated_data().get("form_pk", "")) post_errors = None post_function = getattr(self, f"post_form_{form_pk}", None) if post_function: post_errors = post_function(request, **kwargs) if self.validate_only_until_final_submission: self._validated_data[VALIDATE_ONLY] = True if form_pk: return self.get_next_form_page(form_pk, action, request, post_errors) elif action == Actions.FINISH: self._validated_data = nest_data(self.get_validated_data()) self._validated_data[VALIDATE_ONLY] = False validated_data = validate_data_unknown(self.get_object_pk(), self.get_action(), request, self.get_validated_data()) if "errors" not in validated_data: if self.success_message: messages.success(self.request, self.success_message) return redirect(self.get_success_url()) if self.validate_only_until_final_submission: return self.generate_summary_list() return redirect(request.path)