code cleanup

This commit is contained in:
stvnrlly
2022-11-28 15:53:56 -05:00
parent ca135e32e8
commit e12e780a05
16 changed files with 26 additions and 145 deletions

View File

@@ -1,30 +1,20 @@
# -*- coding: utf-8 -*-
import base64
import io
import json
import os
from datetime import datetime
from dateutil import parser
from flask import (
Response,
flash,
jsonify,
redirect,
render_template,
request,
send_file,
stream_with_context,
url_for,
)
from notifications_python_client.errors import APIError, HTTPError
from notifications_utils import LETTER_MAX_PAGE_COUNT
from notifications_utils.letter_timings import (
get_letter_timings,
letter_can_be_cancelled,
)
from notifications_utils.pdf import pdf_page_count
from PyPDF2.errors import PdfReadError
from app import (
current_service,
@@ -43,9 +33,7 @@ from app.utils import (
set_status_filters,
)
from app.utils.csv import generate_notifications_csv
from app.utils.letters import (
get_letter_validation_error,
)
from app.utils.letters import get_letter_validation_error
from app.utils.templates import get_template
from app.utils.user import user_has_permissions
@@ -58,40 +46,20 @@ def view_notification(service_id, notification_id):
personalisation = get_all_personalisation_from_notification(notification)
error_message = None
if notification['template']['is_precompiled_letter']:
try:
file_contents, metadata = get_letter_file_data(
service_id, notification_id, "pdf", with_metadata=True
)
page_count = int(
metadata["page_count"]
) if metadata.get("page_count") else pdf_page_count(io.BytesIO(file_contents))
if notification["status"] == "validation-failed":
invalid_pages = metadata.get("invalid_pages")
invalid_pages = json.loads(invalid_pages) if invalid_pages else invalid_pages
error_message = get_letter_validation_error(
metadata.get("message"), invalid_pages, page_count
)
except PdfReadError:
return render_template(
'views/notifications/invalid_precompiled_letter.html',
created_at=notification['created_at']
)
else:
page_count = get_page_count_for_letter(notification['template'], values=personalisation)
if page_count and page_count > LETTER_MAX_PAGE_COUNT:
# We check page count here to show the right error message for a letter that is too long.
# Another way to do this would be to get the status and error message from letter metadata.
# This would be a significant amount of work though, out of scope for this bug fix.
# This is because currently we do not pull the letter from S3 when showing preview.
# Instead, we generate letter preview based on the letter template and personalisation.
# Additionally, when a templated letter is sent via the api and the personalisation pushes the
# page count over 10 pages, it takes a while for validation status to come through.
# Checking page count here will enable us to show the error message even if the letter is not
# fully processed yet.
error_message = get_letter_validation_error(
"letter-too-long", [1], page_count
)
page_count = get_page_count_for_letter(notification['template'], values=personalisation)
if page_count and page_count > LETTER_MAX_PAGE_COUNT:
# We check page count here to show the right error message for a letter that is too long.
# Another way to do this would be to get the status and error message from letter metadata.
# This would be a significant amount of work though, out of scope for this bug fix.
# This is because currently we do not pull the letter from S3 when showing preview.
# Instead, we generate letter preview based on the letter template and personalisation.
# Additionally, when a templated letter is sent via the api and the personalisation pushes the
# page count over 10 pages, it takes a while for validation status to come through.
# Checking page count here will enable us to show the error message even if the letter is not
# fully processed yet.
error_message = get_letter_validation_error(
"letter-too-long", [1], page_count
)
if notification.get('postage'):
if notification["status"] == "validation-failed":
@@ -101,12 +69,6 @@ def view_notification(service_id, notification_id):
template = get_template(
notification['template'],
current_service,
letter_preview_url=url_for(
'.view_letter_notification_as_preview',
service_id=service_id,
notification_id=notification_id,
filetype='png',
),
page_count=page_count,
show_recipient=True,
redact_missing_personalisation=True,
@@ -119,7 +81,6 @@ def view_notification(service_id, notification_id):
else:
job = None
notification_created = parser.parse(notification['created_at']).replace(tzinfo=None)
show_cancel_button = notification['notification_type'] == 'letter' and \
@@ -185,41 +146,6 @@ def view_notification(service_id, notification_id):
)
@main.route("/services/<uuid:service_id>/notification/<uuid:notification_id>.<letter_file_extension:filetype>")
@user_has_permissions('view_activity', 'send_messages')
def view_letter_notification_as_preview(
service_id, notification_id, filetype, with_metadata=False
):
image_data = get_letter_file_data(service_id, notification_id, filetype, with_metadata)
file = io.BytesIO(image_data)
mimetype = 'image/png' if filetype == 'png' else 'application/pdf'
return send_file(
path_or_file=file,
mimetype=mimetype,
)
def get_letter_file_data(service_id, notification_id, filetype, with_metadata=False):
try:
preview = notification_api_client.get_notification_letter_preview(
service_id,
notification_id,
filetype,
page=request.args.get('page')
)
display_file = base64.b64decode(preview['content'])
except APIError:
display_file = get_preview_error_image()
preview = {"metadata": {}}
if with_metadata:
return display_file, preview['metadata']
return display_file
@main.route("/services/<uuid:service_id>/notification/<uuid:notification_id>.json")
@user_has_permissions('view_activity', 'send_messages')
def view_notification_updates(service_id, notification_id):