Refactor to handle API errors for preview

This commit is contained in:
Ken Tsang
2018-03-01 14:58:51 +00:00
parent b34b0e2870
commit ff457b8407
5 changed files with 42 additions and 29 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 95 KiB

View File

@@ -13,8 +13,8 @@ from flask import (
url_for,
)
from flask_login import login_required
from notifications_python_client.errors import APIError
from notifications_python_client.errors import APIError
from app import (
current_service,
format_date_numeric,
@@ -57,7 +57,6 @@ def view_notification(service_id, notification_id):
show_recipient=True,
redact_missing_personalisation=True,
)
template.values = get_all_personalisation_from_notification(notification)
if notification['job']:
job = job_api_client.get_job(service_id, notification['job']['id'])['data']
@@ -87,6 +86,12 @@ def view_notification(service_id, notification_id):
)
def get_preview_error_image():
path = os.path.join(os.path.dirname(__file__), "..", "..", "assets", "images", "preview_error.png")
with open(path, "rb") as file:
return file.read()
@main.route("/services/<service_id>/notification/<uuid:notification_id>.<filetype>")
@login_required
@user_has_permissions('view_activity', admin_override=True)
@@ -95,22 +100,6 @@ def view_letter_notification_as_preview(service_id, notification_id, filetype):
if filetype not in ('pdf', 'png'):
abort(404)
# notification = notification_api_client.get_notification(service_id, notification_id)
# notification['template'].update({'reply_to_text': notification['reply_to_text']})
# 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',
# ),
# )
# template.values = notification['personalisation']
try:
preview = notification_api_client.get_notification_letter_preview(
service_id,
@@ -121,8 +110,7 @@ def view_letter_notification_as_preview(service_id, notification_id, filetype):
display_file = base64.b64decode(preview['content'])
except APIError:
with open(os.path.join(os.path.dirname(__file__), "../../assets/images/preview_error.png"), "rb") as file:
display_file = file.read()
display_file = get_preview_error_image()
return display_file

View File

@@ -35,8 +35,8 @@
Estimated delivery date: {{ estimated_letter_delivery_date|string|format_date_short }}
</p>
<p class="bottom-gutter">
<a href="{{ url_for('main.view_letter_notification_as_preview', service_id=current_service.id, notification_id=notification_id, filetype='pdf') }}" download>Download as a PDF</a>
</p>
<a href="{{ url_for('main.view_letter_notification_as_preview', service_id=current_service.id, notification_id=notification_id, filetype='pdf') }}" download>Download as a PDF</a>
</p>
{% endif %}
{{ template|string }}