From c4f0b4d35d1b62555d82fc88974fcfa646585927 Mon Sep 17 00:00:00 2001 From: Richard Chapman Date: Tue, 27 Feb 2018 15:08:39 +0000 Subject: [PATCH] Moved the notifications code to go to admin to get the the template preview document rather than go to template preview. This will remove the logic from admin and place it in api so it is easier to expand on later when there are precompiled PDFs --- app/main/views/notifications.py | 14 +++++++++++++- app/notify_client/notification_api_client.py | 12 ++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/app/main/views/notifications.py b/app/main/views/notifications.py index e880d6cec..c88c3c13e 100644 --- a/app/main/views/notifications.py +++ b/app/main/views/notifications.py @@ -1,4 +1,7 @@ # -*- coding: utf-8 -*- +import base64 +import json +from collections import ItemsView from datetime import datetime from flask import ( @@ -88,6 +91,8 @@ def view_notification(service_id, notification_id): @user_has_permissions('view_activity', admin_override=True) def view_letter_notification_as_preview(service_id, notification_id, filetype): + print("\n\nview_letter_notification_as_preview\n\n") + if filetype not in ('pdf', 'png'): abort(404) @@ -107,8 +112,15 @@ def view_letter_notification_as_preview(service_id, notification_id, filetype): template.values = notification['personalisation'] - return TemplatePreview.from_utils_template(template, filetype, page=request.args.get('page')) + preview = notification_api_client.get_notification_letter_preview( + service_id, + template.id, + notification_id, + filetype, + page=request.args.get('page') + ) + return base64.b64decode(preview['content']), preview['status'], ItemsView(dict(preview['headers'])) @main.route("/services//notification/.json") @user_has_permissions('view_activity', admin_override=True) diff --git a/app/notify_client/notification_api_client.py b/app/notify_client/notification_api_client.py index bb4140066..b3ddf51fd 100644 --- a/app/notify_client/notification_api_client.py +++ b/app/notify_client/notification_api_client.py @@ -78,3 +78,15 @@ class NotificationApiClient(NotifyAdminAPIClient): if notification['notification_type'] == 'letter' and notification['status'] in ('created', 'sending'): notification['status'] = 'accepted' return notifications + + def get_notification_letter_preview(self, service_id, template_id, notification_id, file_type, page=None): + + get_url = '/service/{}/template/{}/pdf-preview/{}/{}{}'.format( + service_id, + template_id, + notification_id, + file_type, + '?page={}'.format(page) if page else '' + ) + + return self.get(url=get_url)