def init(self, request, **kwargs): if "good_pk" in kwargs: # coming from the application self.object_pk = str(kwargs["good_pk"]) self.application_id = str(kwargs["pk"]) self.back_link = build_firearm_create_back( reverse("applications:add_good_summary", kwargs={"pk": self.application_id, "good_pk": self.object_pk}) ) else: self.back_link = None self.object_pk = str(kwargs["pk"]) self.draft_pk = str(kwargs.get("draft_pk", "")) self.data = get_good_details(request, self.object_pk)[0]["firearm_details"] self.selected_section = "section" if self.data["firearms_act_section"] == "firearms_act_section1": self.selected_section = "Section 1" elif self.data["firearms_act_section"] == "firearms_act_section2": self.selected_section = "Section 2" elif self.data["firearms_act_section"] == "firearms_act_section5": self.selected_section = "Section 5" self.certificate_filename = "" if not self.data["section_certificate_missing"]: documents, _ = get_application_documents(request, self.application_id, self.object_pk) if documents["documents"]: self.certificate_filename = documents["documents"][0]["name"] self.firearms_data_id = f"edit_{request.session['lite_api_user_id']}_{self.application_id}_{self.object_pk}" self.form = upload_firearms_act_certificate_form( self.selected_section, self.certificate_filename, self.back_link ) self.action = edit_good_firearm_details
def get(self, request, **kwargs): back_link = build_firearm_back_link_create( form_url=reverse("applications:new_good", kwargs={"pk": kwargs["pk"]}), form_data=request.session.get(self.firearms_data_id, {}), ) form = upload_firearms_act_certificate_form( section=self.selected_section, filename="", back_link=back_link) return form_page(request, form)
def post(self, request, **kwargs): self.request.upload_handlers.insert(0, S3FileUploadHandler(request)) certificate_available = request.POST.get("section_certificate_missing", False) is False doc_data = {} doc_error = None new_file_selected = False file_upload_key = f"{self.firearms_data_id}_file" doc_data, doc_error = add_document_data(request) if doc_data: request.session[file_upload_key] = doc_data self.certificate_filename = doc_data["name"] new_file_selected = True else: file_info = request.session.get(file_upload_key) if file_info: doc_data = file_info doc_error = None new_file_selected = True self.certificate_filename = file_info["name"] old_post = request.session.get(self.firearms_data_id, None) if not old_post: return error_page( request, "Firearms data from previous forms is missing in session") copied_request = {k: request.POST.get(k) for k in request.POST} data = {**old_post, **copied_request} back_link = build_firearm_back_link_create( form_url=reverse("applications:new_good", kwargs={"pk": kwargs["pk"]}), form_data=old_post, ) if self.good_pk: response, status_code = post_good_on_application( request, self.draft_pk, data) if status_code != HTTPStatus.CREATED: if doc_error: response["errors"]["file"] = [ "Select certificate file to upload" ] form = upload_firearms_act_certificate_form( section="section", filename=self.certificate_filename, back_link=back_link, ) return form_page(request, form, data=data, errors=response["errors"]) success_url = reverse_lazy("applications:goods", kwargs={"pk": self.draft_pk}) else: response, status_code = post_goods(request, data) if status_code != HTTPStatus.CREATED: if doc_error: response["errors"]["file"] = [ "Select certificate file to upload" ] form = upload_firearms_act_certificate_form( section="section", filename=self.certificate_filename, back_link=back_link, ) return form_page(request, form, data=data, errors=response["errors"]) self.good_pk = response["good"]["id"] success_url = reverse("applications:add_good_summary", kwargs={ "pk": self.draft_pk, "good_pk": self.good_pk }) if certificate_available and new_file_selected: data, status_code = post_application_document( request, self.draft_pk, self.good_pk, doc_data) if status_code != HTTPStatus.CREATED: return error_page(request, data["errors"]["file"]) elif doc_data: delete_application_document_data(request, self.draft_pk, self.good_pk, doc_data) self.certificate_filename = "" del request.session[self.firearms_data_id] del request.session[file_upload_key] request.session.modified = True return redirect(success_url)
def post(self, request, **kwargs): self.request.upload_handlers.insert(0, S3FileUploadHandler(request)) self.init(request, **kwargs) doc_data = {} new_file_selected = False file_upload_key = f"{self.firearms_data_id}_file" json = {k: v for k, v in request.POST.items()} certificate_available = json.get("section_certificate_missing", False) is False doc_data, _ = add_document_data(request) if doc_data: self.request.session[file_upload_key] = doc_data self.certificate_filename = doc_data["name"] new_file_selected = True else: file_info = self.request.session.get(file_upload_key) if file_info: doc_data = file_info new_file_selected = True self.certificate_filename = file_info["name"] # if certificate_available and error and self.certificate_filename == "": if certificate_available and self.certificate_filename == "": form = upload_firearms_act_certificate_form( self.selected_section, self.certificate_filename, self.back_link) return form_page( request, form, data=json, errors={"file": ["Select certificate file to upload"]}) validated_data, _ = edit_good_firearm_details(request, kwargs["good_pk"], json) if "errors" in validated_data: form = upload_firearms_act_certificate_form( self.selected_section, self.certificate_filename, self.back_link) return form_page(request, form, data=json, errors=validated_data["errors"]) if certificate_available and new_file_selected: fetch_and_delete_previous_application_documents( request, kwargs["pk"], kwargs["good_pk"]) data, status_code = post_application_document( request, kwargs["pk"], kwargs["good_pk"], doc_data) if status_code != HTTPStatus.CREATED: return error_page(request, data["errors"]["file"]) if not certificate_available and self.certificate_filename: fetch_and_delete_previous_application_documents( request, kwargs["pk"], kwargs["good_pk"]) if file_upload_key in request.session.keys(): del self.request.session[file_upload_key] self.request.session.modified = True return redirect( reverse("applications:add_good_summary", kwargs={ "pk": kwargs["pk"], "good_pk": kwargs["good_pk"] }))
def post(self, request, **kwargs): self.init(request, **kwargs) doc_data = {} new_file_selected = False file_upload_key = f"{self.firearms_data_id}_file" json = {k: v for k, v in request.POST.items()} certificate_available = json.get("section_certificate_missing", False) is False doc_data, _ = add_document_data(request) if doc_data: self.request.session[file_upload_key] = doc_data self.certificate_filename = doc_data["name"] new_file_selected = True else: file_info = self.request.session.get(file_upload_key) if file_info: doc_data = file_info new_file_selected = True self.certificate_filename = file_info["name"] # if certificate_available and error and self.certificate_filename == "": if certificate_available and self.certificate_filename == "": form = upload_firearms_act_certificate_form( self.selected_section, self.certificate_filename, self.back_link ) return form_page(request, form, data=json, errors={"file": ["Select certificate file to upload"]}) validated_data, _ = edit_good_firearm_details(request, kwargs["good_pk"], json) if "errors" in validated_data: form = upload_firearms_act_certificate_form( self.selected_section, self.certificate_filename, self.back_link ) return form_page(request, form, data=json, errors=validated_data["errors"]) if certificate_available and new_file_selected: fetch_and_delete_previous_application_documents(request, kwargs["pk"], kwargs["good_pk"]) document_types = { "Section 1": "section-one-certificate", "Section 2": "section-two-certificate", "Section 5": "section-five-certificate", } doc_data["document_on_organisation"] = { "expiry_date": format_date(request.POST, "section_certificate_date_of_expiry"), "reference_code": request.POST["section_certificate_number"], "document_type": document_types[self.selected_section], } data, status_code = post_application_document(request, kwargs["pk"], kwargs["good_pk"], doc_data) if status_code != HTTPStatus.CREATED: return error_page(request, data["errors"]["file"]) if not certificate_available and self.certificate_filename: fetch_and_delete_previous_application_documents(request, kwargs["pk"], kwargs["good_pk"]) if file_upload_key in request.session.keys(): del self.request.session[file_upload_key] self.request.session.modified = True return redirect( reverse("applications:add_good_summary", kwargs={"pk": kwargs["pk"], "good_pk": kwargs["good_pk"]}) )
def post(self, request, **kwargs): certificate_available = request.POST.get("section_certificate_missing", False) is False doc_data = {} doc_error = None new_file_selected = False file_upload_key = f"{self.firearms_data_id}_file" doc_data, doc_error = add_document_data(request) if doc_data: request.session[file_upload_key] = doc_data self.certificate_filename = doc_data["name"] new_file_selected = True else: file_info = request.session.get(file_upload_key) if file_info: doc_data = file_info doc_error = None new_file_selected = True self.certificate_filename = file_info["name"] old_post = request.session.get(self.firearms_data_id, None) if not old_post: return error_page( request, "Firearms data from previous forms is missing in session") copied_request = {k: request.POST.get(k) for k in request.POST} data = {**old_post, **copied_request} back_link = build_firearm_back_link_create( form_url=reverse("applications:new_good", kwargs={"pk": kwargs["pk"]}), form_data=old_post, ) errors = validate_expiry_date(request, "section_certificate_date_of_expiry") if errors: form = upload_firearms_act_certificate_form( section="section", filename=self.certificate_filename, back_link=back_link, ) return form_page( request, form, data=data, errors={"section_certificate_date_of_expiry": errors}) if self.good_pk: response, status_code = post_good_on_application( request, self.draft_pk, data) if status_code != HTTPStatus.CREATED: if doc_error: response["errors"]["file"] = [ "Select certificate file to upload" ] form = upload_firearms_act_certificate_form( section="section", filename=self.certificate_filename, back_link=back_link, ) return form_page(request, form, data=data, errors=response["errors"]) success_url = reverse_lazy("applications:goods", kwargs={"pk": self.draft_pk}) else: response, status_code = post_goods(request, data) if status_code != HTTPStatus.CREATED: if doc_error: response["errors"]["file"] = [ "Select certificate file to upload" ] form = upload_firearms_act_certificate_form( section="section", filename=self.certificate_filename, back_link=back_link, ) return form_page(request, form, data=data, errors=response["errors"]) self.good_pk = response["good"]["id"] success_url = reverse("applications:add_good_summary", kwargs={ "pk": self.draft_pk, "good_pk": self.good_pk }) if certificate_available and new_file_selected: document_types = { "Section 1": "section-one-certificate", "Section 2": "section-two-certificate", "Section 5": "section-five-certificate", } doc_data["document_on_organisation"] = { "expiry_date": format_date(request.POST, "section_certificate_date_of_expiry"), "reference_code": request.POST["section_certificate_number"], "document_type": document_types[self.selected_section], } data, status_code = post_application_document( request, self.draft_pk, self.good_pk, doc_data) if status_code != HTTPStatus.CREATED: return error_page(request, data["errors"]["file"]) elif doc_data: delete_application_document_data(request, self.draft_pk, self.good_pk, doc_data) self.certificate_filename = "" del request.session[self.firearms_data_id] request.session.pop(file_upload_key, None) request.session.modified = True return redirect(success_url)