def post(self, request, notice_of_application_to_vary_order_id): html = request.data['html'] json_data = request.data['json_data'] uid = request.user.id notice_of_application_to_vary_order = self.get_notice_of_application_to_vary_order_for_user( notice_of_application_to_vary_order_id, uid) if not notice_of_application_to_vary_order: return HttpResponseNotFound(no_record_found) name = request.query_params.get("name") pdf_type = request.query_params.get("pdf_type") version = request.query_params.get("version") if None in [name, pdf_type, version]: return HttpResponseBadRequest("Missing parameters.") try: pdf_result = self.get_pdf_by_notice_of_application_to_vary_order_id_and_type( notice_of_application_to_vary_order_id, pdf_type) pdf_content = render_pdf(html) (pdf_key_id, pdf_content_enc) = settings.ENCRYPTOR.encrypt(pdf_content) (pdf_key_id, json_enc) = settings.ENCRYPTOR.encrypt( json.dumps(json_data).encode("utf-8")) if pdf_result: pdf_result.data = pdf_content_enc pdf_result.json_data = json_enc pdf_result.key_id = pdf_key_id pdf_result.pdf_type = pdf_type pdf_result.version = version else: pdf_result = FormPdf( notice_of_application_to_vary_order= notice_of_application_to_vary_order, data=pdf_content_enc, json_data=json_enc, key_id=pdf_key_id, pdf_type=pdf_type, version=version, ) pdf_result.save() notice_of_application_to_vary_order.pdf_types = pdf_type notice_of_application_to_vary_order.save() except Exception as ex: LOGGER.error("ERROR: Pdf generation failed %s", ex) raise if request.query_params.get("noDownload"): return HttpResponse(status=204) else: return create_download_response(pdf_content)
def get(self, request, notice_of_application_to_vary_order_id=None): uid = request.user.id pdf_type = request.query_params.get("pdf_type") if pdf_type is None: return HttpResponseBadRequest("Missing pdf_type parameters.") notice_of_application_to_vary_order_ids = request.query_params.getlist( "id") if not notice_of_application_to_vary_order_ids: notice_of_application_to_vary_order = self.get_notice_of_application_to_vary_order_for_user( notice_of_application_to_vary_order_id, uid) if not notice_of_application_to_vary_order: return HttpResponseNotFound(no_record_found) prepared_pdf = self.get_pdf_by_notice_of_application_to_vary_order_id_and_type( notice_of_application_to_vary_order_id, pdf_type) if prepared_pdf is None: return HttpResponseNotFound(no_record_found) pdf_content = settings.ENCRYPTOR.decrypt(prepared_pdf.key_id, prepared_pdf.data) return create_download_response(pdf_content) else: pdf_contents = list() for notice_of_application_to_vary_orderId in notice_of_application_to_vary_order_ids: notice_of_application_to_vary_order = self.get_notice_of_application_to_vary_order_for_user( notice_of_application_to_vary_orderId, uid) if not notice_of_application_to_vary_order: continue prepared_pdf = self.get_pdf_by_notice_of_application_to_vary_order_id_and_type( notice_of_application_to_vary_orderId, pdf_type) if prepared_pdf is None: continue pdf_contents.append({ "id": notice_of_application_to_vary_orderId, "type": pdf_type, "pdf": settings.ENCRYPTOR.decrypt(prepared_pdf.key_id, prepared_pdf.data) }) if not pdf_contents: return HttpResponseNotFound(no_record_found) return create_zip_download_response(pdf_contents)
def get(self, request, objection_withdraw_id=None): uid = request.user.id pdf_type = request.query_params.get("pdf_type") if pdf_type is None: return HttpResponseBadRequest("Missing pdf_type parameters.") objection_withdraw_ids = request.query_params.getlist("id") if not objection_withdraw_ids: objection_withdraw = self.get_objection_withdraw_for_user( objection_withdraw_id, uid) if not objection_withdraw: return HttpResponseNotFound(no_record_found) prepared_pdf = self.get_pdf_by_objection_withdraw_id_and_type( objection_withdraw_id, pdf_type) if prepared_pdf is None: return HttpResponseNotFound(no_record_found) pdf_content = settings.ENCRYPTOR.decrypt(prepared_pdf.key_id, prepared_pdf.data) return create_download_response(pdf_content) else: pdf_contents = list() for objection_withdrawId in objection_withdraw_ids: objection_withdraw = self.get_objection_withdraw_for_user( objection_withdrawId, uid) if not objection_withdraw: continue prepared_pdf = self.get_pdf_by_objection_withdraw_id_and_type( objection_withdrawId, pdf_type) if prepared_pdf is None: continue pdf_contents.append({ "id": objection_withdrawId, "type": pdf_type, "pdf": settings.ENCRYPTOR.decrypt(prepared_pdf.key_id, prepared_pdf.data) }) if not pdf_contents: return HttpResponseNotFound(no_record_found) return create_zip_download_response(pdf_contents)
def get(self, request, change_of_address_id = None): uid = request.user.id pdf_type = request.query_params.get("pdf_type") if pdf_type is None: return HttpResponseBadRequest("Missing pdf_type parameters.") change_of_address_ids = request.query_params.getlist("id") if not change_of_address_ids: change_of_address = self.get_change_of_address_for_user(change_of_address_id, uid) if not change_of_address: return HttpResponseNotFound(no_record_found) prepared_pdf = self.get_pdf_by_change_of_address_id_and_type(change_of_address_id, pdf_type) if prepared_pdf is None: return HttpResponseNotFound(no_record_found) pdf_content = settings.ENCRYPTOR.decrypt(prepared_pdf.key_id, prepared_pdf.data) return create_download_response(pdf_content) else : pdf_contents = list() for change_of_addressId in change_of_address_ids: change_of_address = self.get_change_of_address_for_user(change_of_addressId, uid) if not change_of_address: continue prepared_pdf = self.get_pdf_by_change_of_address_id_and_type(change_of_addressId, pdf_type) if prepared_pdf is None: continue pdf_contents.append({"id":change_of_addressId, "type":pdf_type, "pdf": settings.ENCRYPTOR.decrypt(prepared_pdf.key_id, prepared_pdf.data)}) if not pdf_contents: return HttpResponseNotFound(no_record_found) return create_zip_download_response(pdf_contents)