Respect template’s redaction preference

If a template has the `redact_personalisation` flag set, then this
commit removes the personalisation from the notification before
rehydrating the template.

We’re doing this because we have a need to not show things like one time
passwords or two factor codes when we show the content of messages.

By passing through empty personalisation, and the `redact_missing` flag,
the `Template` instance will make use of the work done in:
- [x] https://github.com/alphagov/notifications-utils/pull/171
This commit is contained in:
Chris Hill-Scott
2017-06-24 17:29:28 +01:00
parent 1d65c19c4e
commit bc880017e5
6 changed files with 129 additions and 18 deletions

View File

@@ -390,12 +390,29 @@ def get_job_partials(job):
def add_preview_of_content_to_notifications(notifications):
for notification in notifications:
yield dict(
preview_of_content=(
str(Template(notification['template'], notification['personalisation']))
if notification['template']['template_type'] == 'sms' else
WithSubjectTemplate(notification['template'], notification['personalisation']).subject
),
**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:
yield dict(
preview_of_content=(
WithSubjectTemplate(
notification['template'],
notification['personalisation'],
redact_missing_personalisation=True,
).subject
),
**notification
)

View File

@@ -56,6 +56,7 @@ def view_notification(service_id, notification_id):
filetype='png',
),
show_recipient=True,
redact_missing_personalisation=True,
)
template.values = get_all_personalisation_from_notification(notification)
if notification['job']:
@@ -109,15 +110,21 @@ def get_single_notification_partials(notification):
def get_all_personalisation_from_notification(notification):
if notification['template'].get('redact_personalisation'):
notification['personalisation'] = {}
if notification['template']['template_type'] == 'email':
return dict(
email_address=notification['to'],
**notification['personalisation']
)
if notification['template']['template_type'] == 'sms':
return dict(
phone_number=notification['to'],
**notification['personalisation']
)
if notification['template']['template_type'] == 'letter':
return notification['personalisation']