mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-24 16:24:08 -04:00
Merge pull request #1930 from alphagov/revert-1918-letter_preview_use_api_not_template_preview
Revert "Letter preview use api not template preview"
This commit is contained in:
Binary file not shown.
|
Before Width: | Height: | Size: 16 KiB |
@@ -1,6 +1,4 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
import base64
|
|
||||||
import os
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from flask import (
|
from flask import (
|
||||||
@@ -13,7 +11,6 @@ from flask import (
|
|||||||
url_for,
|
url_for,
|
||||||
)
|
)
|
||||||
from flask_login import login_required
|
from flask_login import login_required
|
||||||
from notifications_python_client.errors import APIError
|
|
||||||
|
|
||||||
from app import (
|
from app import (
|
||||||
current_service,
|
current_service,
|
||||||
@@ -22,7 +19,7 @@ from app import (
|
|||||||
notification_api_client,
|
notification_api_client,
|
||||||
)
|
)
|
||||||
from app.main import main
|
from app.main import main
|
||||||
from app.template_previews import get_page_count_for_letter
|
from app.template_previews import TemplatePreview, get_page_count_for_letter
|
||||||
from app.utils import (
|
from app.utils import (
|
||||||
DELIVERED_STATUSES,
|
DELIVERED_STATUSES,
|
||||||
FAILURE_STATUSES,
|
FAILURE_STATUSES,
|
||||||
@@ -86,12 +83,6 @@ 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>")
|
@main.route("/services/<service_id>/notification/<uuid:notification_id>.<filetype>")
|
||||||
@login_required
|
@login_required
|
||||||
@user_has_permissions('view_activity')
|
@user_has_permissions('view_activity')
|
||||||
@@ -100,19 +91,23 @@ def view_letter_notification_as_preview(service_id, notification_id, filetype):
|
|||||||
if filetype not in ('pdf', 'png'):
|
if filetype not in ('pdf', 'png'):
|
||||||
abort(404)
|
abort(404)
|
||||||
|
|
||||||
try:
|
notification = notification_api_client.get_notification(service_id, notification_id)
|
||||||
preview = notification_api_client.get_notification_letter_preview(
|
notification['template'].update({'reply_to_text': notification['reply_to_text']})
|
||||||
service_id,
|
|
||||||
notification_id,
|
|
||||||
filetype,
|
|
||||||
page=request.args.get('page')
|
|
||||||
)
|
|
||||||
|
|
||||||
display_file = base64.b64decode(preview['content'])
|
template = get_template(
|
||||||
except APIError:
|
notification['template'],
|
||||||
display_file = get_preview_error_image()
|
current_service,
|
||||||
|
letter_preview_url=url_for(
|
||||||
|
'.view_letter_notification_as_preview',
|
||||||
|
service_id=service_id,
|
||||||
|
notification_id=notification_id,
|
||||||
|
filetype='png',
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
return display_file
|
template.values = notification['personalisation']
|
||||||
|
|
||||||
|
return TemplatePreview.from_utils_template(template, filetype, page=request.args.get('page'))
|
||||||
|
|
||||||
|
|
||||||
@main.route("/services/<service_id>/notification/<notification_id>.json")
|
@main.route("/services/<service_id>/notification/<notification_id>.json")
|
||||||
|
|||||||
@@ -73,14 +73,3 @@ class NotificationApiClient(NotifyAdminAPIClient):
|
|||||||
if notification['notification_type'] == 'letter' and notification['status'] in ('created', 'sending'):
|
if notification['notification_type'] == 'letter' and notification['status'] in ('created', 'sending'):
|
||||||
notification['status'] = 'accepted'
|
notification['status'] = 'accepted'
|
||||||
return notifications
|
return notifications
|
||||||
|
|
||||||
def get_notification_letter_preview(self, service_id, notification_id, file_type, page=None):
|
|
||||||
|
|
||||||
get_url = '/service/{}/template/preview/{}/{}{}'.format(
|
|
||||||
service_id,
|
|
||||||
notification_id,
|
|
||||||
file_type,
|
|
||||||
'?page={}'.format(page) if page else ''
|
|
||||||
)
|
|
||||||
|
|
||||||
return self.get(url=get_url)
|
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
import base64
|
|
||||||
from functools import partial
|
from functools import partial
|
||||||
from unittest.mock import mock_open
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from flask import url_for
|
from flask import url_for
|
||||||
from freezegun import freeze_time
|
from freezegun import freeze_time
|
||||||
from notifications_python_client.errors import APIError
|
from notifications_utils.template import LetterImageTemplate
|
||||||
from tests.conftest import (
|
from tests.conftest import (
|
||||||
SERVICE_ONE_ID,
|
SERVICE_ONE_ID,
|
||||||
mock_get_notification,
|
mock_get_notification,
|
||||||
@@ -165,11 +163,9 @@ def test_should_show_image_of_letter_notification(
|
|||||||
|
|
||||||
mock_get_notification(mocker, fake_uuid, template_type='letter')
|
mock_get_notification(mocker, fake_uuid, template_type='letter')
|
||||||
|
|
||||||
mocker.patch(
|
mocked_preview = mocker.patch(
|
||||||
'app.notify_client.notification_api_client.NotificationApiClient.get',
|
'app.main.views.templates.TemplatePreview.from_utils_template',
|
||||||
return_value={
|
return_value='foo'
|
||||||
'content': base64.b64encode(b'foo').decode('utf-8')
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
response = logged_in_client.get(url_for(
|
response = logged_in_client.get(url_for(
|
||||||
@@ -181,32 +177,8 @@ def test_should_show_image_of_letter_notification(
|
|||||||
|
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert response.get_data(as_text=True) == 'foo'
|
assert response.get_data(as_text=True) == 'foo'
|
||||||
|
assert isinstance(mocked_preview.call_args[0][0], LetterImageTemplate)
|
||||||
|
assert mocked_preview.call_args[0][1] == filetype
|
||||||
def test_should_show_preview_error_image_letter_notification_on_preview_error(
|
|
||||||
logged_in_client,
|
|
||||||
fake_uuid,
|
|
||||||
mocker,
|
|
||||||
):
|
|
||||||
|
|
||||||
mock_get_notification(mocker, fake_uuid, template_type='letter')
|
|
||||||
|
|
||||||
mocker.patch(
|
|
||||||
'app.notify_client.notification_api_client.NotificationApiClient.get',
|
|
||||||
side_effect=APIError
|
|
||||||
)
|
|
||||||
|
|
||||||
mocker.patch("builtins.open", mock_open(read_data="preview error image"))
|
|
||||||
|
|
||||||
response = logged_in_client.get(url_for(
|
|
||||||
'main.view_letter_notification_as_preview',
|
|
||||||
service_id=SERVICE_ONE_ID,
|
|
||||||
notification_id=fake_uuid,
|
|
||||||
filetype='png'
|
|
||||||
))
|
|
||||||
|
|
||||||
assert response.status_code == 200
|
|
||||||
assert response.get_data(as_text=True) == 'preview error image'
|
|
||||||
|
|
||||||
|
|
||||||
def test_should_404_for_unknown_extension(
|
def test_should_404_for_unknown_extension(
|
||||||
|
|||||||
Reference in New Issue
Block a user