Show all pages of a letter in the app

In research we’ve seen two problems with the click-to-see-PDF thing:

- it’s not very intuitive that the letter is clickable, or what you
  can expect when clicking the letter
- people get lost of stuck in the PDF view because it opens in the same
  tab, or they open it in a new tab and then get find their way back, or
  …

So this commit changes the show template page to show the entire
contents of the letter, same as we do for emails and text messages.

Right now it only does it on the view template page. I think we’ll have
to work out a way of showing some kind of truncated version on the _Send
yourself a test_ and _Preview_ pages. But that’s for later.
This commit is contained in:
Chris Hill-Scott
2017-04-20 10:40:15 +01:00
parent 38fbc3c47d
commit 36db0ad598
5 changed files with 60 additions and 16 deletions

View File

@@ -8,6 +8,7 @@ from flask import (
url_for,
flash,
abort,
json,
)
from flask_login import login_required, current_user
from dateutil.parser import parse
@@ -72,15 +73,21 @@ def choose_template(service_id):
admin_override=True, any_=True
)
def view_template(service_id, template_id):
template = service_api_client.get_service_template(service_id, template_id)['data']
page_count = None
if template['template_type'] == 'letter':
page_count, _, _ = TemplatePreview.from_database_object(template, filetype='json')
page_count = json.loads(page_count.decode('utf-8'))['count']
return render_template(
'views/templates/template.html',
template=get_template(
service_api_client.get_service_template(service_id, template_id)['data'],
template,
current_service,
expand_emails=True,
letter_preview_url=url_for('.view_template', service_id=service_id, template_id=template_id),
show_recipient=True,
)
page_count=page_count,
),
)
@@ -92,7 +99,7 @@ def view_letter_template_preview(service_id, template_id, filetype):
abort(404)
db_template = service_api_client.get_service_template(service_id, template_id)['data']
return TemplatePreview.from_database_object(db_template, filetype)
return TemplatePreview.from_database_object(db_template, filetype, page=request.args.get('page'))
def _view_template_version(service_id, template_id, version, letters_as_pdf=False):

View File

@@ -6,23 +6,28 @@ from app import current_service
class TemplatePreview:
@classmethod
def from_database_object(cls, template, filetype, values=None):
def from_database_object(cls, template, filetype, values=None, page=None):
data = {
'letter_contact_block': current_service['letter_contact_block'],
'template': template,
'values': values
}
resp = requests.post(
'{}/preview.{}'.format(current_app.config['TEMPLATE_PREVIEW_API_HOST'], filetype),
'{}/preview.{}{}'.format(
current_app.config['TEMPLATE_PREVIEW_API_HOST'],
filetype,
'?page={}'.format(page) if page else '',
),
json=data,
headers={'Authorization': 'Token {}'.format(current_app.config['TEMPLATE_PREVIEW_API_KEY'])}
)
return (resp.content, resp.status_code, resp.headers.items())
@classmethod
def from_utils_template(cls, template, filetype):
def from_utils_template(cls, template, filetype, page=None):
return cls.from_database_object(
template._template,
filetype,
template.values
template.values,
page=page,
)

View File

@@ -295,6 +295,7 @@ def get_template(
show_recipient=False,
expand_emails=False,
letter_preview_url=None,
page_count=1,
):
if 'email' == template['template_type']:
return EmailPreviewTemplate(
@@ -316,6 +317,7 @@ def get_template(
return LetterPDFLinkTemplate(
template,
preview_url=letter_preview_url,
page_count=int(page_count),
)
else:
return LetterPreviewTemplate(

View File

@@ -28,5 +28,4 @@ notifications-python-client>=3.1,<3.2
awscli>=1.11,<1.12
awscli-cwlogs>=1.4,<1.5
git+https://github.com/alphagov/notifications-utils.git@15.0.4#egg=notifications-utils==15.0.4
git+https://github.com/alphagov/notifications-utils.git@16.0.0#egg=notifications-utils==16.0.0

View File

@@ -1,30 +1,61 @@
import pytest
from functools import partial
from unittest.mock import Mock
from notifications_utils.template import LetterPreviewTemplate
from app.template_previews import TemplatePreview
def test_from_utils_template_calls_through(mocker, mock_get_service_letter_template):
@pytest.mark.parametrize('partial_call, expected_page_argument', [
(partial(TemplatePreview.from_utils_template), None),
(partial(TemplatePreview.from_utils_template, page=99), 99),
])
def test_from_utils_template_calls_through(
mocker,
mock_get_service_letter_template,
partial_call,
expected_page_argument,
):
mock_from_db = mocker.patch('app.template_previews.TemplatePreview.from_database_object')
template = LetterPreviewTemplate(mock_get_service_letter_template(None, None)['data'])
ret = TemplatePreview.from_utils_template(template, 'foo')
ret = partial_call(template, 'foo')
assert ret == mock_from_db.return_value
mock_from_db.assert_called_once_with(template._template, 'foo', template.values)
mock_from_db.assert_called_once_with(template._template, 'foo', template.values, page=expected_page_argument)
def test_from_database_object_makes_request(mocker, client):
@pytest.mark.parametrize('partial_call, expected_url', [
(
partial(TemplatePreview.from_database_object, filetype='bar'),
'http://localhost:6013/preview.bar',
),
(
partial(TemplatePreview.from_database_object, filetype='baz'),
'http://localhost:6013/preview.baz',
),
(
partial(TemplatePreview.from_database_object, filetype='bar', page=99),
'http://localhost:6013/preview.bar?page=99',
),
])
def test_from_database_object_makes_request(
mocker,
client,
partial_call,
expected_url,
):
resp = Mock(content='a', status_code='b', headers={'c': 'd'})
request_mock = mocker.patch('app.template_previews.requests.post', return_value=resp)
mocker.patch('app.template_previews.current_service', __getitem__=Mock(return_value='123'))
ret = TemplatePreview.from_database_object(template='foo', filetype='bar')
ret = partial_call(template='foo')
assert ret[0] == 'a'
assert ret[1] == 'b'
assert list(ret[2]) == [('c', 'd')]
url = 'http://localhost:6013/preview.bar'
data = {
'letter_contact_block': '123',
'template': 'foo',
@@ -32,4 +63,4 @@ def test_from_database_object_makes_request(mocker, client):
}
headers = {'Authorization': 'Token my-secret-key'}
request_mock.assert_called_once_with(url, json=data, headers=headers)
request_mock.assert_called_once_with(expected_url, json=data, headers=headers)