def add_section_certificate_details(firearm_details, json): if "section_certificate_step" in json: firearm_details[ "is_covered_by_firearm_act_section_one_two_or_five"] = json.get( "is_covered_by_firearm_act_section_one_two_or_five", "") firearm_details["firearms_act_section"] = json.get( "firearms_act_section", "") if "firearms_certificate_uploaded" in json: certificate_missing = json.get("section_certificate_missing", False) if not certificate_missing: firearm_details["section_certificate_number"] = json.get( "section_certificate_number") formatted_section_certificate_date = format_date( json, "section_certificate_date_of_expiry") firearm_details["section_certificate_date_of_expiry"] = ( formatted_section_certificate_date if formatted_section_certificate_date != "--" else None) firearm_details["section_certificate_missing"] = False firearm_details["section_certificate_missing_reason"] = "" else: firearm_details["section_certificate_missing"] = True firearm_details["section_certificate_missing_reason"] = json.get( "section_certificate_missing_reason", "") firearm_details["section_certificate_number"] = "" return firearm_details
def post(self, request, **kwargs): organisation_id = str(request.session["organisation"]) data = { "expiry_date": format_date(request.POST, "expiry_date_"), "reference_code": self.request.POST["reference_code"], "document_type": self.document_type, } errors = validate_expiry_date(request, "expiry_date_") if errors: form = self.form_function(back_url=reverse("organisation:details")) return form_page(request, form, data=request.POST, errors={"expiry_date": errors}) file = request.FILES.get("file") if file: data["document"] = { "name": getattr(file, "original_name", file.name), "s3_key": file.name, "size": int(file.size // 1024) if file.size else 0, # in kilobytes } post_document_on_organisation(request=request, organisation_id=organisation_id, data=data) return redirect(reverse("organisation:details"))
def edit_good_pv_grading(request, pk, json): data = {"is_pv_graded": json["is_pv_graded"]} if json["is_pv_graded"] == "yes": data["pv_grading_details"] = { "grading": json["grading"], "custom_grading": json["custom_grading"], "prefix": json["prefix"], "suffix": json["suffix"], "issuing_authority": json["issuing_authority"], "reference": json["reference"], "date_of_issue": format_date(json, "date_of_issue"), } return edit_good(request, pk, data)
def serialize_good_on_app_data(json): if json.get("good_on_app_value") or json.get("good_on_app_value") == "": post_data = remove_prefix(json, "good_on_app_") else: post_data = json for key in {"value", "quantity"} & set(post_data.keys()): if "," in post_data[key]: post_data[key] = post_data[key].replace(",", "") if json.get("date_of_deactivationday"): post_data["date_of_deactivation"] = format_date(post_data, "date_of_deactivation") post_data = services.add_firearm_details_to_data(post_data) return post_data
def cache_rfd_certificate_details(self): file = self.request.FILES.get("file") if not file: return self.request.session[self.SESSION_KEY_RFD_CERTIFICATE] = { "name": getattr(file, "original_name", file.name), "s3_key": file.name, "size": int(file.size // 1024) if file.size else 0, # in kilobytes "document_on_organisation": { "expiry_date": format_date(self.request.POST, "expiry_date_"), "reference_code": self.request.POST["reference_code"], "document_type": "rfd-certificate", }, }
def validate_expiry_date(request, field_name): """ Validate that the section certificate expiry date is not in the past or > 5y in the future. """ iso_date = format_date(request.POST, field_name) # Absence of ISO date is dealt with by the API if not iso_date: return [ "Enter the certificate expiry date and include a day, month and year" ] expiry_date = date.fromisoformat(iso_date) today = timezone.now().date() # Date of expiry has to be in the future if expiry_date < today: return ["Expiry date must be in the future"] else: if relativedelta(expiry_date, today).years >= 5: return ["Expiry date is too far in the future"]
def post_goods(request, json): json["item_category"] = json.get("item_category", "group2_firearms") if "is_pv_graded" in json and json["is_pv_graded"] == "yes": if "reference" in json: json["pv_grading_details"] = { "grading": json["grading"], "custom_grading": json["custom_grading"], "prefix": json["prefix"], "suffix": json["suffix"], "issuing_authority": json["issuing_authority"], "reference": json["reference"], "date_of_issue": format_date(json, "date_of_issue"), } if "item_category" in json and json["item_category"] == "group2_firearms": add_firearm_details_to_data(json) data = client.post(request, "/goods/", json) if data.status_code == HTTPStatus.OK: data.json().get("good"), data.status_code return data.json(), data.status_code
def serialize_good_on_app_data(json, good=None, preexisting=False): if json.get("good_on_app_value") or json.get("good_on_app_value") == "": post_data = remove_prefix(json, "good_on_app_") else: post_data = json for key in {"value", "quantity"} & set(post_data.keys()): if "," in post_data[key]: post_data[key] = post_data[key].replace(",", "") if json.get("date_of_deactivationday"): post_data["date_of_deactivation"] = format_date(post_data, "date_of_deactivation") post_data = services.add_firearm_details_to_data(post_data) # Adding new good to the application firearm_details = post_data.get("firearm_details") if firearm_details: if not preexisting and good: firearm_details["number_of_items"] = good["firearm_details"]["number_of_items"] if good["firearm_details"]["has_identification_markings"] is True: firearm_details["serial_numbers"] = good["firearm_details"]["serial_numbers"] else: firearm_details["serial_numbers"] = list() if good["firearm_details"]["type"]["key"] in FIREARM_AMMUNITION_COMPONENT_TYPES: post_data["quantity"] = good["firearm_details"]["number_of_items"] post_data["unit"] = "NAR" # number of articles else: firearm_details["number_of_items"] = post_data["quantity"] if preexisting and good: if good["firearm_details"]["type"]["key"] in FIREARM_AMMUNITION_COMPONENT_TYPES: post_data["quantity"] = firearm_details.get("number_of_items", 0) post_data["unit"] = "NAR" # number of articles return post_data
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)