From 88734bb74fd71b5a57ced6ba183633743f97afa2 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Wed, 15 Apr 2020 17:58:57 +0100 Subject: [PATCH] Bump utils to bring in recent changes to Template classes The main things we have to account for are: - `WithSubjectTemplate` has gone - every template dictionary must have `template_type` set --- app/main/views/conversation.py | 1 + app/main/views/index.py | 3 +- app/main/views/jobs.py | 62 ++++++++++++++++++++-------------- app/main/views/styleguide.py | 4 +-- requirements-app.txt | 2 +- requirements.txt | 2 +- 6 files changed, 44 insertions(+), 30 deletions(-) diff --git a/app/main/views/conversation.py b/app/main/views/conversation.py index e33c04029..a858688e7 100644 --- a/app/main/views/conversation.py +++ b/app/main/views/conversation.py @@ -117,6 +117,7 @@ def get_sms_thread(service_id, user_number): 'inbound': is_inbound, 'content': SMSPreviewTemplate( { + 'template_type': 'sms', 'content': ( notification['content'] if is_inbound else notification['template']['content'] diff --git a/app/main/views/index.py b/app/main/views/index.py index 062230eb2..4e4713af4 100644 --- a/app/main/views/index.py +++ b/app/main/views/index.py @@ -128,6 +128,7 @@ def email_template(): brand_name = email_branding['name'] template = { + 'template_type': 'email', 'subject': 'foo', 'content': ( 'Lorem Ipsum is simply dummy text of the printing and typesetting ' @@ -198,7 +199,7 @@ def letter_template(): else: filename = 'no-branding' - template = {'subject': '', 'content': ''} + template = {'subject': '', 'content': '', 'template_type': 'letter'} image_url = url_for('no_cookie.letter_branding_preview_image', filename=filename) template_image = str(LetterImageTemplate( diff --git a/app/main/views/jobs.py b/app/main/views/jobs.py index 25bf8a884..94bd5cf19 100644 --- a/app/main/views/jobs.py +++ b/app/main/views/jobs.py @@ -13,7 +13,11 @@ from flask import ( ) from flask_login import current_user from notifications_python_client.errors import HTTPError -from notifications_utils.template import Template, WithSubjectTemplate +from notifications_utils.template import ( + EmailPreviewTemplate, + LetterPreviewTemplate, + SMSPreviewTemplate, +) from app import ( current_service, @@ -402,29 +406,37 @@ def get_job_partials(job): def add_preview_of_content_to_notifications(notifications): for notification in notifications: + yield(dict( + preview_of_content=get_preview_of_content(notification), + **notification + )) - if notification['template'].get('redact_personalisation'): - notification['personalisation'] = {} - if notification['template']['template_type'] == 'sms': - yield dict( - preview_of_content=str(Template( - notification['template'], - notification['personalisation'], - redact_missing_personalisation=True, - )), - **notification - ) - else: - if notification['template']['is_precompiled_letter']: - notification['template']['subject'] = notification['client_reference'] - yield dict( - preview_of_content=( - WithSubjectTemplate( - notification['template'], - notification['personalisation'], - redact_missing_personalisation=True, - ).subject - ), - **notification - ) +def get_preview_of_content(notification): + + if notification['template'].get('redact_personalisation'): + notification['personalisation'] = {} + + if notification['template']['is_precompiled_letter']: + return notification['client_reference'] + + if notification['template']['template_type'] == 'sms': + return str(SMSPreviewTemplate( + notification['template'], + notification['personalisation'], + redact_missing_personalisation=True, + show_prefix=False, + )) + + if notification['template']['template_type'] == 'email': + return EmailPreviewTemplate( + notification['template'], + notification['personalisation'], + redact_missing_personalisation=True, + ).subject + + if notification['template']['template_type'] == 'letter': + return LetterPreviewTemplate( + notification['template'], + notification['personalisation'], + ).subject diff --git a/app/main/views/styleguide.py b/app/main/views/styleguide.py index 6218a1749..c8bc29cc4 100644 --- a/app/main/views/styleguide.py +++ b/app/main/views/styleguide.py @@ -1,6 +1,6 @@ from flask import abort, current_app, render_template from flask_wtf import FlaskForm as Form -from notifications_utils.template import Template +from notifications_utils.template import SMSPreviewTemplate from wtforms import ( FileField, PasswordField, @@ -31,7 +31,7 @@ def styleguide(): form.message.data = sms form.validate() - template = Template({'content': sms}) + template = SMSPreviewTemplate({'content': sms, 'template_type': 'sms'}) return render_template( 'views/styleguide.html', diff --git a/requirements-app.txt b/requirements-app.txt index e796a1e1c..5f5346ed4 100644 --- a/requirements-app.txt +++ b/requirements-app.txt @@ -22,5 +22,5 @@ notifications-python-client==5.5.1 awscli-cwlogs>=1.4,<1.5 itsdangerous==1.1.0 -git+https://github.com/alphagov/notifications-utils.git@36.12.2#egg=notifications-utils==36.12.2 +git+https://github.com/alphagov/notifications-utils.git@37.0.0#egg=notifications-utils==37.0.0 git+https://github.com/alphagov/govuk-frontend-jinja.git@v0.5.1-alpha#egg=govuk-frontend-jinja==0.5.1-alpha diff --git a/requirements.txt b/requirements.txt index e4a81f8f8..7e81cb2f0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -24,7 +24,7 @@ notifications-python-client==5.5.1 awscli-cwlogs>=1.4,<1.5 itsdangerous==1.1.0 -git+https://github.com/alphagov/notifications-utils.git@36.12.2#egg=notifications-utils==36.12.2 +git+https://github.com/alphagov/notifications-utils.git@37.0.0#egg=notifications-utils==37.0.0 git+https://github.com/alphagov/govuk-frontend-jinja.git@v0.5.1-alpha#egg=govuk-frontend-jinja==0.5.1-alpha ## The following requirements were added by pip freeze: