def letter_validation_preview(from_platform_admin): message, pages, passed_validation, error_code = None, [], None, None form = PDFUploadForm() view_location = 'views/platform-admin/letter-validation-preview.html' \ if from_platform_admin else'views/letter-validation-preview.html' if form.validate_on_submit(): pdf_file = form.file.data virus_free = antivirus_client.scan(pdf_file) if not virus_free: return render_template( view_location, form=form, message="Document did not pass the virus scan", pages=pages, passed_validation=passed_validation ), 400 try: if len(pdf_file.read()) > (2 * 1024 * 1024): return render_template( view_location, form=form, message="File must be less than 2MB", pages=pages, passed_validation=passed_validation ), 400 pdf_file.seek(0) response = validate_letter(pdf_file) response.raise_for_status() if response.status_code == 200: pages, message = response.json()["pages"], response.json()["message"], passed_validation = response.json()["result"] invalid_pages = response.json().get('invalid_pages') page_count = len(pages) if page_count > LETTER_MAX_PAGE_COUNT: message = "letter-too-long" passed_validation = False if not passed_validation: error_code = message message = get_letter_validation_error( message, invalid_pages=invalid_pages, page_count=page_count ) except RequestException as error: if error.response and error.response.status_code == 400: message = "Something was wrong with the file you tried to upload. Please upload a valid PDF file." return render_template( view_location, form=form, message=message, pages=pages, passed_validation=passed_validation ), 400 else: raise error return render_template( view_location, form=form, message=message, pages=pages, passed_validation=passed_validation, error_code=error_code )
def letter_validation_preview(from_platform_admin): message, pages, result = None, [], None form = PDFUploadForm() view_location = 'views/platform-admin/letter-validation-preview.html' \ if from_platform_admin else'views/letter-validation-preview.html' if form.validate_on_submit(): pdf_file = form.file.data virus_free = antivirus_client.scan(pdf_file) if not virus_free: return render_template( view_location, form=form, message="Document didn't pass the virus scan", pages=pages, result=result), 400 try: if len(pdf_file.read()) > (2 * 1024 * 1024): return render_template(view_location, form=form, message="File must be less than 2MB", pages=pages, result=result), 400 pdf_file.seek(0) response = validate_letter(pdf_file) response.raise_for_status() if response.status_code == 200: pages, message, result = response.json( )["pages"], response.json()["message"], response.json( )["result"] except RequestException as error: if error.response and error.response.status_code == 400: message = "Something was wrong with the file you tried to upload. Please upload a valid PDF file." return render_template(view_location, form=form, message=message, pages=pages, result=result), 400 else: raise error return render_template(view_location, form=form, message=message, pages=pages, result=result)
def invalid_upload_error(error_title, error_detail=None): return render_template('views/uploads/choose-file.html', error={ 'title': error_title, 'detail': error_detail }, form=PDFUploadForm()), 400
def uploaded_letter_preview(service_id, file_id): re_upload_form = PDFUploadForm() try: metadata = get_letter_metadata(service_id, file_id) except ClientError as e: # if the file's not there, it's probably because we've already created the notification and the letter has been # moved to the normal letters-pdf bucket. So lets just bounce out to the notification page if e.response['Error']['Code'] == 'NoSuchKey': return redirect( url_for( '.view_notification', service_id=service_id, notification_id=file_id, )) else: raise original_filename = metadata.get('filename') page_count = metadata.get('page_count') status = metadata.get('status') error_shortcode = metadata.get('message') invalid_pages = metadata.get('invalid_pages') postal_address = PostalAddress(metadata.get('recipient', '')) if invalid_pages: invalid_pages = json.loads(invalid_pages) error_message = get_letter_validation_error(error_shortcode, invalid_pages, page_count) template_dict = service_api_client.get_precompiled_template(service_id) # Override pre compiled letter template postage to none as it has not yet been picked even though # the pre compiled letter template has its postage set as second class as the DB currently requires # a non null value of postage for letter templates template_dict['postage'] = None form = LetterUploadPostageForm(postage_zone=postal_address.postage) template = get_template(template_dict, service_id, letter_preview_url=url_for( '.view_letter_upload_as_preview', service_id=service_id, file_id=file_id), page_count=page_count) return render_template( 'views/uploads/preview.html', original_filename=original_filename, template=template, status=status, file_id=file_id, message=error_message, error_code=error_shortcode, form=form, allowed_file_extensions=Spreadsheet.ALLOWED_FILE_EXTENSIONS, postal_address=postal_address, re_upload_form=re_upload_form)
def uploaded_letter_preview(service_id, file_id): re_upload_form = PDFUploadForm() metadata = get_letter_metadata(service_id, file_id) original_filename = metadata.get('filename') page_count = metadata.get('page_count') status = metadata.get('status') error_shortcode = metadata.get('message') invalid_pages = metadata.get('invalid_pages') recipient = format_recipient(metadata.get('recipient', '')) if invalid_pages: invalid_pages = json.loads(invalid_pages) error_message = get_letter_validation_error(error_shortcode, invalid_pages, page_count) template_dict = service_api_client.get_precompiled_template(service_id) # Override pre compiled letter template postage to none as it has not yet been picked even though # the pre compiled letter template has its postage set as second class as the DB currently requires # a non null value of postage for letter templates template_dict['postage'] = None form = LetterUploadPostageForm() template = get_template( template_dict, service_id, letter_preview_url=url_for( '.view_letter_upload_as_preview', service_id=service_id, file_id=file_id ), page_count=page_count ) return render_template( 'views/uploads/preview.html', original_filename=original_filename, template=template, status=status, file_id=file_id, message=error_message, error_code=error_shortcode, form=form, recipient=recipient, re_upload_form=re_upload_form )
def upload_letter(service_id): form = PDFUploadForm() error = {} if form.validate_on_submit(): pdf_file_bytes = form.file.data.read() original_filename = form.file.data.filename if current_app.config['ANTIVIRUS_ENABLED']: virus_free = antivirus_client.scan(BytesIO(pdf_file_bytes)) if not virus_free: return invalid_upload_error('Your file contains a virus') if len(pdf_file_bytes) > MAX_FILE_UPLOAD_SIZE: return invalid_upload_error('Your file is too big', 'Files must be smaller than 2MB.') try: # TODO: get page count from the sanitise response once template preview handles malformed files nicely page_count = pdf_page_count(BytesIO(pdf_file_bytes)) except PdfReadError: current_app.logger.info( 'Invalid PDF uploaded for service_id: {}'.format(service_id)) return invalid_upload_error( "There’s a problem with your file", 'Notify cannot read this PDF.<br>Save a new copy of your file and try again.' ) upload_id = uuid.uuid4() file_location = get_transient_letter_file_location( service_id, upload_id) try: response = sanitise_letter( BytesIO(pdf_file_bytes), allow_international_letters=current_service.has_permission( 'international_letters'), ) response.raise_for_status() except RequestException as ex: if ex.response is not None and ex.response.status_code == 400: validation_failed_message = response.json().get('message') invalid_pages = response.json().get('invalid_pages') status = 'invalid' upload_letter_to_s3(pdf_file_bytes, file_location=file_location, status=status, page_count=page_count, filename=original_filename, message=validation_failed_message, invalid_pages=invalid_pages) else: raise ex else: response = response.json() recipient = response['recipient_address'] status = 'valid' file_contents = base64.b64decode(response['file'].encode()) upload_letter_to_s3(file_contents, file_location=file_location, status=status, page_count=page_count, filename=original_filename, recipient=recipient) return redirect( url_for( 'main.uploaded_letter_preview', service_id=current_service.id, file_id=upload_id, )) if form.file.errors: error = _get_error_from_upload_form(form.file.errors[0]) return render_template('views/uploads/choose-file.html', error=error, form=form)