2016-06-06 11:09:46 +01:00
|
|
|
from datetime import datetime, timedelta
|
2016-05-31 14:18:35 +01:00
|
|
|
from string import ascii_uppercase
|
2016-05-27 16:21:29 +01:00
|
|
|
|
2017-03-27 17:59:06 +01:00
|
|
|
from flask import (
|
|
|
|
|
request,
|
|
|
|
|
render_template,
|
|
|
|
|
redirect,
|
|
|
|
|
url_for,
|
|
|
|
|
flash,
|
|
|
|
|
abort,
|
2017-04-20 10:40:15 +01:00
|
|
|
json,
|
2017-03-27 17:59:06 +01:00
|
|
|
)
|
2017-01-18 15:11:34 +00:00
|
|
|
from flask_login import login_required, current_user
|
2016-06-06 11:42:40 +01:00
|
|
|
from dateutil.parser import parse
|
2015-12-20 00:00:01 +00:00
|
|
|
|
2017-03-27 16:39:46 +01:00
|
|
|
from notifications_utils.formatters import escape_html
|
2016-11-10 13:39:05 +00:00
|
|
|
from notifications_utils.recipients import first_column_headings
|
2016-08-24 12:12:19 +01:00
|
|
|
from notifications_python_client.errors import HTTPError
|
2016-04-12 09:55:51 +01:00
|
|
|
|
2015-12-20 00:00:01 +00:00
|
|
|
from app.main import main
|
2017-04-10 17:25:08 +01:00
|
|
|
from app.utils import user_has_permissions, get_template
|
2017-05-11 12:01:51 +01:00
|
|
|
from app.template_previews import TemplatePreview, get_page_count_for_letter
|
2017-03-14 10:46:38 +00:00
|
|
|
from app.main.forms import (
|
|
|
|
|
ChooseTemplateType,
|
|
|
|
|
SMSTemplateForm,
|
|
|
|
|
EmailTemplateForm,
|
|
|
|
|
LetterTemplateForm,
|
|
|
|
|
SearchTemplatesForm,
|
|
|
|
|
)
|
2016-05-27 16:21:29 +01:00
|
|
|
from app.main.views.send import get_example_csv_rows
|
2016-06-06 11:09:46 +01:00
|
|
|
from app import service_api_client, current_service, template_statistics_client
|
2016-01-13 16:21:29 +00:00
|
|
|
|
2015-12-20 00:00:01 +00:00
|
|
|
|
2016-02-22 14:45:13 +00:00
|
|
|
form_objects = {
|
|
|
|
|
'email': EmailTemplateForm,
|
2016-11-08 13:12:07 +00:00
|
|
|
'sms': SMSTemplateForm,
|
|
|
|
|
'letter': LetterTemplateForm
|
2016-02-22 14:45:13 +00:00
|
|
|
}
|
2015-12-20 00:00:01 +00:00
|
|
|
|
2016-03-11 07:45:10 +00:00
|
|
|
page_headings = {
|
|
|
|
|
'email': 'email',
|
|
|
|
|
'sms': 'text message'
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-20 00:00:01 +00:00
|
|
|
|
2017-06-13 15:28:33 +01:00
|
|
|
@main.route("/services/<service_id>/templates/<uuid:template_id>")
|
2016-04-12 09:55:51 +01:00
|
|
|
@login_required
|
|
|
|
|
@user_has_permissions(
|
|
|
|
|
'view_activity',
|
|
|
|
|
'send_texts',
|
|
|
|
|
'send_emails',
|
|
|
|
|
'manage_templates',
|
|
|
|
|
'manage_api_keys',
|
|
|
|
|
admin_override=True, any_=True
|
|
|
|
|
)
|
|
|
|
|
def view_template(service_id, template_id):
|
2017-06-13 15:28:33 +01:00
|
|
|
template = service_api_client.get_service_template(service_id, str(template_id))['data']
|
2016-04-12 09:55:51 +01:00
|
|
|
return render_template(
|
|
|
|
|
'views/templates/template.html',
|
2016-12-08 11:50:59 +00:00
|
|
|
template=get_template(
|
2017-04-20 10:40:15 +01:00
|
|
|
template,
|
2016-12-08 11:50:59 +00:00
|
|
|
current_service,
|
2016-12-20 14:38:34 +00:00
|
|
|
expand_emails=True,
|
2017-04-28 16:04:52 +01:00
|
|
|
letter_preview_url=url_for(
|
|
|
|
|
'.view_letter_template_preview',
|
|
|
|
|
service_id=service_id,
|
|
|
|
|
template_id=template_id,
|
|
|
|
|
filetype='png',
|
|
|
|
|
),
|
2017-03-10 17:39:38 +00:00
|
|
|
show_recipient=True,
|
2017-05-11 12:01:51 +01:00
|
|
|
page_count=get_page_count_for_letter(template),
|
2017-04-20 10:40:15 +01:00
|
|
|
),
|
2016-04-12 09:55:51 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2017-06-13 15:28:33 +01:00
|
|
|
@main.route("/services/<service_id>/templates")
|
|
|
|
|
@main.route("/services/<service_id>/templates/<template_type>")
|
|
|
|
|
@login_required
|
|
|
|
|
@user_has_permissions(
|
|
|
|
|
'view_activity',
|
|
|
|
|
'send_texts',
|
|
|
|
|
'send_emails',
|
|
|
|
|
'manage_templates',
|
|
|
|
|
'manage_api_keys',
|
|
|
|
|
admin_override=True,
|
|
|
|
|
any_=True,
|
|
|
|
|
)
|
|
|
|
|
def choose_template(service_id, template_type='all'):
|
|
|
|
|
templates = service_api_client.get_service_templates(service_id)['data']
|
|
|
|
|
|
2017-06-13 15:27:29 +01:00
|
|
|
has_multiple_template_types = len({
|
|
|
|
|
template['template_type'] for template in templates
|
|
|
|
|
}) > 1
|
|
|
|
|
|
2017-06-13 15:28:33 +01:00
|
|
|
template_nav_items = [
|
|
|
|
|
(label, key, url_for('.choose_template', service_id=current_service['id'], template_type=key), '')
|
|
|
|
|
for label, key in filter(None, [
|
|
|
|
|
('All', 'all'),
|
|
|
|
|
('Text message', 'sms'),
|
|
|
|
|
('Email', 'email'),
|
|
|
|
|
('Letter', 'letter') if current_service['can_send_letters'] else None,
|
|
|
|
|
])
|
|
|
|
|
]
|
|
|
|
|
|
2017-06-23 13:51:22 +01:00
|
|
|
templates_on_page = [
|
|
|
|
|
template for template in templates
|
|
|
|
|
if template_type in ['all', template['template_type']]
|
|
|
|
|
]
|
|
|
|
|
|
2017-06-13 15:28:33 +01:00
|
|
|
return render_template(
|
|
|
|
|
'views/templates/choose.html',
|
2017-06-23 13:51:22 +01:00
|
|
|
templates=templates_on_page,
|
|
|
|
|
show_search_box=(len(templates_on_page) > 7),
|
|
|
|
|
show_template_nav=has_multiple_template_types and (len(templates) > 2),
|
2017-06-13 15:28:33 +01:00
|
|
|
template_nav_items=template_nav_items,
|
|
|
|
|
template_type=template_type,
|
|
|
|
|
search_form=SearchTemplatesForm(),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2017-04-10 17:25:08 +01:00
|
|
|
@main.route("/services/<service_id>/templates/<template_id>.<filetype>")
|
2016-11-27 18:10:18 +00:00
|
|
|
@login_required
|
|
|
|
|
@user_has_permissions('view_activity', admin_override=True)
|
2017-04-10 18:12:00 +01:00
|
|
|
def view_letter_template_preview(service_id, template_id, filetype):
|
2017-04-12 12:12:11 +01:00
|
|
|
if filetype not in ('pdf', 'png'):
|
|
|
|
|
abort(404)
|
2016-11-27 18:10:18 +00:00
|
|
|
|
2017-04-10 17:25:08 +01:00
|
|
|
db_template = service_api_client.get_service_template(service_id, template_id)['data']
|
2017-04-20 10:40:15 +01:00
|
|
|
return TemplatePreview.from_database_object(db_template, filetype, page=request.args.get('page'))
|
2016-11-28 11:01:39 +00:00
|
|
|
|
|
|
|
|
|
2016-12-20 11:56:22 +00:00
|
|
|
def _view_template_version(service_id, template_id, version, letters_as_pdf=False):
|
|
|
|
|
return dict(template=get_template(
|
2016-12-20 14:38:34 +00:00
|
|
|
service_api_client.get_service_template(service_id, template_id, version=version)['data'],
|
2016-12-20 11:56:22 +00:00
|
|
|
current_service,
|
|
|
|
|
expand_emails=True,
|
2016-12-20 14:38:34 +00:00
|
|
|
letter_preview_url=url_for(
|
2017-04-28 16:04:52 +01:00
|
|
|
'.view_template_version_preview',
|
2016-12-20 14:38:34 +00:00
|
|
|
service_id=service_id,
|
|
|
|
|
template_id=template_id,
|
|
|
|
|
version=version,
|
2017-04-28 16:04:52 +01:00
|
|
|
filetype='png',
|
2016-12-20 14:38:34 +00:00
|
|
|
) if not letters_as_pdf else None
|
2016-12-20 11:56:22 +00:00
|
|
|
))
|
|
|
|
|
|
|
|
|
|
|
2016-05-11 11:20:45 +01:00
|
|
|
@main.route("/services/<service_id>/templates/<template_id>/version/<int:version>")
|
|
|
|
|
@login_required
|
|
|
|
|
@user_has_permissions(
|
|
|
|
|
'view_activity',
|
|
|
|
|
'send_texts',
|
|
|
|
|
'send_emails',
|
|
|
|
|
'manage_templates',
|
|
|
|
|
'manage_api_keys',
|
|
|
|
|
admin_override=True,
|
|
|
|
|
any_=True
|
|
|
|
|
)
|
|
|
|
|
def view_template_version(service_id, template_id, version):
|
|
|
|
|
return render_template(
|
|
|
|
|
'views/templates/template_history.html',
|
2016-12-20 11:56:22 +00:00
|
|
|
**_view_template_version(service_id=service_id, template_id=template_id, version=version)
|
2016-05-11 11:20:45 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2017-04-10 17:25:08 +01:00
|
|
|
@main.route("/services/<service_id>/templates/<template_id>/version/<int:version>.<filetype>")
|
2016-12-20 14:38:34 +00:00
|
|
|
@login_required
|
|
|
|
|
@user_has_permissions(
|
|
|
|
|
'view_activity',
|
|
|
|
|
'send_texts',
|
|
|
|
|
'send_emails',
|
|
|
|
|
'manage_templates',
|
|
|
|
|
'manage_api_keys',
|
|
|
|
|
admin_override=True,
|
|
|
|
|
any_=True
|
|
|
|
|
)
|
2017-04-10 17:25:08 +01:00
|
|
|
def view_template_version_preview(service_id, template_id, version, filetype):
|
|
|
|
|
db_template = service_api_client.get_service_template(service_id, template_id, version=version)['data']
|
|
|
|
|
return TemplatePreview.from_database_object(db_template, filetype)
|
2016-12-20 14:38:34 +00:00
|
|
|
|
|
|
|
|
|
Merge email, text message + letter templates pages
Right now we have separate pages for email and text message templates.
In the future we will also have a separate page for letter templates.
This commit changes Notify to only have one page for all templates.
What is the problem?
---
The left-hand navigation is getting quite crowded, at 8 items for a
service that can send letters. Research suggests that the number of
objects an average human can hold in working memory is 7 ± 2 [1]. So
we’re at the limit of how many items the navigation should have.
In the future we will need to search/sort/filter templates by attributes
other than type, for example:
- show me the ‘confirmation’ templates
- show me the most recently used templates
- show me all templates containing the placeholder `((ref_no))`
These are hypothetical for now, but these needs (or others) may become
real in the future. At this point pre-filtering the list of templates
by type would restrict what searches a user could do. So by making this
change now we’re in a better position to iterate the design in the
future.
What’s the change?
---
This commit replaces the ‘Email templates’, ‘Text message templates’ and
‘Letter templates’ pages with one page called ‘Templates’.
This new templates page shows all the templates for the service, sorted
by most recently created first (as before).
To add a new template there is a new page with a form asking you what
kind of template you want to create. This is necessary because in the
past we knew what kind of template you wanted to create based on the
kind you were looking at.
What’s the impact of this change on new users?
---
This change alters the onboarding process slightly. We still want to
take people through the empty templates page from the call-to-action on
the dashboard because it helps them understand that to send a message
using Notify you need a template. But because we don’t have separate
pages for emails/text messages we will have to send users through the
extra step of choosing what kind of template to create. This is a bit
clunkier on first use but:
- it still gets the point across
- it takes them through the actual flow they will be using to create new
templates in the future (ie they’re learning how to use Notify, not
just being taken through a special onboarding route)
I’m not too worried about this change in terms of the experience for new
users. Furthermore, by making it now we get to validate whether it’s
causing any problems in the lab research booked for next week.
What’s the impact of this change on current services?
---
Looking at the top 15 services by number of templates[2], most are using
either text messages or emails. So this change would not have a
significant impact on these services because the page will not get any
longer. In other words we wouldn’t be making it worse for them.
Those services who do use both are not using as many templates. The
worst-case scenario is SSCS, who have 16 templates, evenly split between
email and text messages. So they would go from having 8 templates per
page to 16, which is still less than half the number that HMPO or
Digital Marketplace are managing.
References
---
1. https://en.wikipedia.org/wiki/The_Magical_Number_Seven,_Plus_or_Minus_Two
2. Template usage by service
Service name | Template count | Template types
---------------------------------------|----------------|---------------
Her Majesty's Passport Office | 40 | sms
Digital Marketplace | 40 | email
GovWifi-Staging | 19 | sms
GovWifi | 18 | sms
Digital Apprenticeship Service | 16 | email
SSCS | 16 | both
Crown Commercial Service MI Collection | 15 | email
Help with Prison Visits | 12 | both
Digital Future | 12 | email
Export Licensing Service | 11 | email
Civil Money Claims | 9 | both
DVLA Drivers Medical Service | 9 | sms
GOV.UK Notify | 8 | both
Manage your benefit overpayments | 8 | both
Tax Renewals | 8 | both
2017-02-28 12:16:35 +00:00
|
|
|
@main.route("/services/<service_id>/templates/add", methods=['GET', 'POST'])
|
|
|
|
|
@login_required
|
|
|
|
|
@user_has_permissions('manage_templates', admin_override=True)
|
|
|
|
|
def add_template_by_type(service_id):
|
|
|
|
|
|
|
|
|
|
form = ChooseTemplateType(
|
2017-06-23 13:35:08 +01:00
|
|
|
include_letters='letter' in current_service['permissions']
|
Merge email, text message + letter templates pages
Right now we have separate pages for email and text message templates.
In the future we will also have a separate page for letter templates.
This commit changes Notify to only have one page for all templates.
What is the problem?
---
The left-hand navigation is getting quite crowded, at 8 items for a
service that can send letters. Research suggests that the number of
objects an average human can hold in working memory is 7 ± 2 [1]. So
we’re at the limit of how many items the navigation should have.
In the future we will need to search/sort/filter templates by attributes
other than type, for example:
- show me the ‘confirmation’ templates
- show me the most recently used templates
- show me all templates containing the placeholder `((ref_no))`
These are hypothetical for now, but these needs (or others) may become
real in the future. At this point pre-filtering the list of templates
by type would restrict what searches a user could do. So by making this
change now we’re in a better position to iterate the design in the
future.
What’s the change?
---
This commit replaces the ‘Email templates’, ‘Text message templates’ and
‘Letter templates’ pages with one page called ‘Templates’.
This new templates page shows all the templates for the service, sorted
by most recently created first (as before).
To add a new template there is a new page with a form asking you what
kind of template you want to create. This is necessary because in the
past we knew what kind of template you wanted to create based on the
kind you were looking at.
What’s the impact of this change on new users?
---
This change alters the onboarding process slightly. We still want to
take people through the empty templates page from the call-to-action on
the dashboard because it helps them understand that to send a message
using Notify you need a template. But because we don’t have separate
pages for emails/text messages we will have to send users through the
extra step of choosing what kind of template to create. This is a bit
clunkier on first use but:
- it still gets the point across
- it takes them through the actual flow they will be using to create new
templates in the future (ie they’re learning how to use Notify, not
just being taken through a special onboarding route)
I’m not too worried about this change in terms of the experience for new
users. Furthermore, by making it now we get to validate whether it’s
causing any problems in the lab research booked for next week.
What’s the impact of this change on current services?
---
Looking at the top 15 services by number of templates[2], most are using
either text messages or emails. So this change would not have a
significant impact on these services because the page will not get any
longer. In other words we wouldn’t be making it worse for them.
Those services who do use both are not using as many templates. The
worst-case scenario is SSCS, who have 16 templates, evenly split between
email and text messages. So they would go from having 8 templates per
page to 16, which is still less than half the number that HMPO or
Digital Marketplace are managing.
References
---
1. https://en.wikipedia.org/wiki/The_Magical_Number_Seven,_Plus_or_Minus_Two
2. Template usage by service
Service name | Template count | Template types
---------------------------------------|----------------|---------------
Her Majesty's Passport Office | 40 | sms
Digital Marketplace | 40 | email
GovWifi-Staging | 19 | sms
GovWifi | 18 | sms
Digital Apprenticeship Service | 16 | email
SSCS | 16 | both
Crown Commercial Service MI Collection | 15 | email
Help with Prison Visits | 12 | both
Digital Future | 12 | email
Export Licensing Service | 11 | email
Civil Money Claims | 9 | both
DVLA Drivers Medical Service | 9 | sms
GOV.UK Notify | 8 | both
Manage your benefit overpayments | 8 | both
Tax Renewals | 8 | both
2017-02-28 12:16:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if form.validate_on_submit():
|
2017-03-15 10:53:24 +00:00
|
|
|
|
|
|
|
|
if form.template_type.data == 'letter':
|
|
|
|
|
blank_letter = service_api_client.create_service_template(
|
|
|
|
|
'Untitled',
|
|
|
|
|
'letter',
|
2017-05-10 11:27:45 +01:00
|
|
|
'Body',
|
2017-03-15 10:53:24 +00:00
|
|
|
service_id,
|
2017-05-10 11:27:45 +01:00
|
|
|
'Main heading',
|
2017-03-15 10:53:24 +00:00
|
|
|
'normal',
|
|
|
|
|
)
|
|
|
|
|
return redirect(url_for(
|
|
|
|
|
'.view_template',
|
|
|
|
|
service_id=service_id,
|
|
|
|
|
template_id=blank_letter['data']['id'],
|
|
|
|
|
))
|
|
|
|
|
|
Merge email, text message + letter templates pages
Right now we have separate pages for email and text message templates.
In the future we will also have a separate page for letter templates.
This commit changes Notify to only have one page for all templates.
What is the problem?
---
The left-hand navigation is getting quite crowded, at 8 items for a
service that can send letters. Research suggests that the number of
objects an average human can hold in working memory is 7 ± 2 [1]. So
we’re at the limit of how many items the navigation should have.
In the future we will need to search/sort/filter templates by attributes
other than type, for example:
- show me the ‘confirmation’ templates
- show me the most recently used templates
- show me all templates containing the placeholder `((ref_no))`
These are hypothetical for now, but these needs (or others) may become
real in the future. At this point pre-filtering the list of templates
by type would restrict what searches a user could do. So by making this
change now we’re in a better position to iterate the design in the
future.
What’s the change?
---
This commit replaces the ‘Email templates’, ‘Text message templates’ and
‘Letter templates’ pages with one page called ‘Templates’.
This new templates page shows all the templates for the service, sorted
by most recently created first (as before).
To add a new template there is a new page with a form asking you what
kind of template you want to create. This is necessary because in the
past we knew what kind of template you wanted to create based on the
kind you were looking at.
What’s the impact of this change on new users?
---
This change alters the onboarding process slightly. We still want to
take people through the empty templates page from the call-to-action on
the dashboard because it helps them understand that to send a message
using Notify you need a template. But because we don’t have separate
pages for emails/text messages we will have to send users through the
extra step of choosing what kind of template to create. This is a bit
clunkier on first use but:
- it still gets the point across
- it takes them through the actual flow they will be using to create new
templates in the future (ie they’re learning how to use Notify, not
just being taken through a special onboarding route)
I’m not too worried about this change in terms of the experience for new
users. Furthermore, by making it now we get to validate whether it’s
causing any problems in the lab research booked for next week.
What’s the impact of this change on current services?
---
Looking at the top 15 services by number of templates[2], most are using
either text messages or emails. So this change would not have a
significant impact on these services because the page will not get any
longer. In other words we wouldn’t be making it worse for them.
Those services who do use both are not using as many templates. The
worst-case scenario is SSCS, who have 16 templates, evenly split between
email and text messages. So they would go from having 8 templates per
page to 16, which is still less than half the number that HMPO or
Digital Marketplace are managing.
References
---
1. https://en.wikipedia.org/wiki/The_Magical_Number_Seven,_Plus_or_Minus_Two
2. Template usage by service
Service name | Template count | Template types
---------------------------------------|----------------|---------------
Her Majesty's Passport Office | 40 | sms
Digital Marketplace | 40 | email
GovWifi-Staging | 19 | sms
GovWifi | 18 | sms
Digital Apprenticeship Service | 16 | email
SSCS | 16 | both
Crown Commercial Service MI Collection | 15 | email
Help with Prison Visits | 12 | both
Digital Future | 12 | email
Export Licensing Service | 11 | email
Civil Money Claims | 9 | both
DVLA Drivers Medical Service | 9 | sms
GOV.UK Notify | 8 | both
Manage your benefit overpayments | 8 | both
Tax Renewals | 8 | both
2017-02-28 12:16:35 +00:00
|
|
|
return redirect(url_for(
|
|
|
|
|
'.add_service_template',
|
|
|
|
|
service_id=service_id,
|
|
|
|
|
template_type=form.template_type.data,
|
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
return render_template('views/templates/add.html', form=form)
|
|
|
|
|
|
|
|
|
|
|
2016-02-22 14:45:13 +00:00
|
|
|
@main.route("/services/<service_id>/templates/add-<template_type>", methods=['GET', 'POST'])
|
2016-01-11 16:03:41 +00:00
|
|
|
@login_required
|
2016-03-17 10:46:47 +00:00
|
|
|
@user_has_permissions('manage_templates', admin_override=True)
|
2016-02-22 14:45:13 +00:00
|
|
|
def add_service_template(service_id, template_type):
|
2017-01-18 15:11:34 +00:00
|
|
|
|
2016-11-08 13:12:07 +00:00
|
|
|
if template_type not in ['sms', 'email', 'letter']:
|
2016-02-22 14:45:13 +00:00
|
|
|
abort(404)
|
2017-06-23 13:35:08 +01:00
|
|
|
if 'letter' not in current_service['permissions'] and template_type == 'letter':
|
2016-11-08 13:12:07 +00:00
|
|
|
abort(403)
|
2016-02-22 14:45:13 +00:00
|
|
|
|
|
|
|
|
form = form_objects[template_type]()
|
2016-01-19 15:54:12 +00:00
|
|
|
if form.validate_on_submit():
|
2017-01-18 15:11:34 +00:00
|
|
|
if form.process_type.data == 'priority':
|
|
|
|
|
abort_403_if_not_admin_user()
|
2016-04-29 09:38:59 +01:00
|
|
|
try:
|
2017-03-14 13:39:21 +00:00
|
|
|
new_template = service_api_client.create_service_template(
|
2016-04-29 09:38:59 +01:00
|
|
|
form.name.data,
|
|
|
|
|
template_type,
|
|
|
|
|
form.template_content.data,
|
|
|
|
|
service_id,
|
2017-01-18 15:11:34 +00:00
|
|
|
form.subject.data if hasattr(form, 'subject') else None,
|
|
|
|
|
form.process_type.data
|
2016-04-29 09:38:59 +01:00
|
|
|
)
|
|
|
|
|
except HTTPError as e:
|
2017-02-15 15:06:47 +00:00
|
|
|
if (
|
|
|
|
|
e.status_code == 400 and
|
|
|
|
|
'content' in e.message and
|
|
|
|
|
any(['character count greater than' in x for x in e.message['content']])
|
|
|
|
|
):
|
|
|
|
|
form.template_content.errors.extend(e.message['content'])
|
2016-04-29 09:38:59 +01:00
|
|
|
else:
|
|
|
|
|
raise e
|
|
|
|
|
else:
|
|
|
|
|
return redirect(
|
2017-03-14 13:39:21 +00:00
|
|
|
url_for('.view_template', service_id=service_id, template_id=new_template['data']['id'])
|
2016-04-29 09:38:59 +01:00
|
|
|
)
|
2016-02-22 14:45:13 +00:00
|
|
|
|
2016-01-19 15:54:12 +00:00
|
|
|
return render_template(
|
2016-02-22 14:45:13 +00:00
|
|
|
'views/edit-{}-template.html'.format(template_type),
|
2016-01-19 15:54:12 +00:00
|
|
|
form=form,
|
2016-02-22 14:45:13 +00:00
|
|
|
template_type=template_type,
|
2016-03-11 07:45:10 +00:00
|
|
|
heading_action='Add'
|
2016-02-22 14:45:13 +00:00
|
|
|
)
|
2015-12-20 00:00:01 +00:00
|
|
|
|
|
|
|
|
|
2017-01-18 15:11:34 +00:00
|
|
|
def abort_403_if_not_admin_user():
|
|
|
|
|
if not current_user.has_permissions([], admin_override=True):
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
|
|
2016-04-12 09:55:51 +01:00
|
|
|
@main.route("/services/<service_id>/templates/<template_id>/edit", methods=['GET', 'POST'])
|
2016-01-11 16:03:41 +00:00
|
|
|
@login_required
|
2016-03-17 10:46:47 +00:00
|
|
|
@user_has_permissions('manage_templates', admin_override=True)
|
2016-01-19 15:54:12 +00:00
|
|
|
def edit_service_template(service_id, template_id):
|
2016-03-30 10:23:39 +01:00
|
|
|
template = service_api_client.get_service_template(service_id, template_id)['data']
|
2016-01-22 12:15:47 +00:00
|
|
|
template['template_content'] = template['content']
|
2016-02-22 14:45:13 +00:00
|
|
|
form = form_objects[template['template_type']](**template)
|
2016-01-11 13:15:10 +00:00
|
|
|
|
2016-01-19 15:54:12 +00:00
|
|
|
if form.validate_on_submit():
|
2017-01-18 15:11:34 +00:00
|
|
|
if form.process_type.data != template['process_type']:
|
|
|
|
|
abort_403_if_not_admin_user()
|
|
|
|
|
|
2017-04-14 08:52:02 +01:00
|
|
|
subject = form.subject.data if hasattr(form, 'subject') else None
|
2016-12-08 11:50:59 +00:00
|
|
|
new_template = get_template({
|
2016-05-27 16:21:29 +01:00
|
|
|
'name': form.name.data,
|
|
|
|
|
'content': form.template_content.data,
|
|
|
|
|
'subject': subject,
|
|
|
|
|
'template_type': template['template_type'],
|
2017-01-18 15:11:34 +00:00
|
|
|
'id': template['id'],
|
|
|
|
|
'process_type': form.process_type.data
|
2016-12-08 11:50:59 +00:00
|
|
|
}, current_service)
|
|
|
|
|
template_change = get_template(template, current_service).compare_to(new_template)
|
2017-05-17 13:05:18 +01:00
|
|
|
if template_change.placeholders_added and not request.form.get('confirm'):
|
2017-04-06 10:22:09 +01:00
|
|
|
example_column_headings = (
|
|
|
|
|
first_column_headings[new_template.template_type] +
|
|
|
|
|
list(new_template.placeholders)
|
|
|
|
|
)
|
2016-05-27 16:21:29 +01:00
|
|
|
return render_template(
|
|
|
|
|
'views/templates/breaking-change.html',
|
|
|
|
|
template_change=template_change,
|
2017-03-06 13:08:27 +00:00
|
|
|
new_template=new_template,
|
2017-04-06 10:22:09 +01:00
|
|
|
column_headings=list(ascii_uppercase[:len(example_column_headings)]),
|
2016-05-27 16:21:29 +01:00
|
|
|
example_rows=[
|
2017-04-06 10:22:09 +01:00
|
|
|
example_column_headings,
|
2016-05-27 16:21:29 +01:00
|
|
|
get_example_csv_rows(new_template),
|
|
|
|
|
get_example_csv_rows(new_template)
|
|
|
|
|
],
|
|
|
|
|
form=form
|
|
|
|
|
)
|
2016-04-29 09:38:59 +01:00
|
|
|
try:
|
|
|
|
|
service_api_client.update_service_template(
|
|
|
|
|
template_id,
|
|
|
|
|
form.name.data,
|
|
|
|
|
template['template_type'],
|
|
|
|
|
form.template_content.data,
|
|
|
|
|
service_id,
|
2017-01-18 15:11:34 +00:00
|
|
|
subject,
|
|
|
|
|
form.process_type.data
|
2016-04-29 09:38:59 +01:00
|
|
|
)
|
|
|
|
|
except HTTPError as e:
|
|
|
|
|
if e.status_code == 400:
|
|
|
|
|
if 'content' in e.message and any(['character count greater than' in x for x in e.message['content']]):
|
|
|
|
|
form.template_content.errors.extend(e.message['content'])
|
|
|
|
|
else:
|
|
|
|
|
raise e
|
|
|
|
|
else:
|
|
|
|
|
raise e
|
|
|
|
|
else:
|
|
|
|
|
return redirect(url_for(
|
2016-06-06 10:12:21 +01:00
|
|
|
'.view_template',
|
2016-04-29 09:38:59 +01:00
|
|
|
service_id=service_id,
|
2016-06-06 10:12:21 +01:00
|
|
|
template_id=template_id
|
2016-04-29 09:38:59 +01:00
|
|
|
))
|
2016-01-19 15:54:12 +00:00
|
|
|
return render_template(
|
2016-02-22 14:45:13 +00:00
|
|
|
'views/edit-{}-template.html'.format(template['template_type']),
|
2016-01-19 15:54:12 +00:00
|
|
|
form=form,
|
2016-02-22 14:45:13 +00:00
|
|
|
template_id=template_id,
|
2016-03-11 07:45:10 +00:00
|
|
|
template_type=template['template_type'],
|
|
|
|
|
heading_action='Edit'
|
2016-02-22 14:45:13 +00:00
|
|
|
)
|
2016-01-14 09:44:06 +00:00
|
|
|
|
|
|
|
|
|
2016-04-12 14:19:51 +01:00
|
|
|
@main.route("/services/<service_id>/templates/<template_id>/delete", methods=['GET', 'POST'])
|
2016-01-14 09:44:06 +00:00
|
|
|
@login_required
|
2016-03-17 10:46:47 +00:00
|
|
|
@user_has_permissions('manage_templates', admin_override=True)
|
2016-01-19 15:54:12 +00:00
|
|
|
def delete_service_template(service_id, template_id):
|
2016-03-30 10:23:39 +01:00
|
|
|
template = service_api_client.get_service_template(service_id, template_id)['data']
|
2016-01-14 09:44:06 +00:00
|
|
|
|
2016-01-22 11:36:49 +00:00
|
|
|
if request.method == 'POST':
|
2016-03-30 10:23:39 +01:00
|
|
|
service_api_client.delete_service_template(service_id, template_id)
|
2016-02-22 14:45:13 +00:00
|
|
|
return redirect(url_for(
|
|
|
|
|
'.choose_template',
|
|
|
|
|
service_id=service_id,
|
|
|
|
|
))
|
2016-01-14 09:44:06 +00:00
|
|
|
|
2016-08-22 16:25:35 +01:00
|
|
|
try:
|
|
|
|
|
last_used_notification = template_statistics_client.get_template_statistics_for_template(
|
|
|
|
|
service_id, template['id']
|
|
|
|
|
)
|
|
|
|
|
message = '{} was last used {} ago'.format(
|
|
|
|
|
last_used_notification['template']['name'],
|
|
|
|
|
get_human_readable_delta(
|
|
|
|
|
parse(last_used_notification['created_at']).replace(tzinfo=None),
|
|
|
|
|
datetime.utcnow())
|
|
|
|
|
)
|
|
|
|
|
except HTTPError as e:
|
|
|
|
|
if e.status_code == 404:
|
|
|
|
|
message = '{} has never been used'.format(template['name'])
|
2016-08-24 11:26:34 +01:00
|
|
|
else:
|
|
|
|
|
raise e
|
2016-08-22 16:25:35 +01:00
|
|
|
|
|
|
|
|
flash('{}. Are you sure you want to delete it?'.format(message), 'delete')
|
2017-04-18 13:01:58 +01:00
|
|
|
|
2016-01-19 15:54:12 +00:00
|
|
|
return render_template(
|
2017-04-18 13:01:58 +01:00
|
|
|
'views/templates/template.html',
|
|
|
|
|
template=get_template(
|
|
|
|
|
template,
|
|
|
|
|
current_service,
|
|
|
|
|
expand_emails=True,
|
2017-04-28 16:04:52 +01:00
|
|
|
letter_preview_url=url_for(
|
|
|
|
|
'.view_letter_template_preview',
|
|
|
|
|
service_id=service_id,
|
|
|
|
|
template_id=template['id'],
|
|
|
|
|
filetype='png',
|
|
|
|
|
),
|
2017-04-18 13:01:58 +01:00
|
|
|
show_recipient=True,
|
|
|
|
|
)
|
|
|
|
|
)
|
2016-05-11 11:20:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@main.route('/services/<service_id>/templates/<template_id>/versions')
|
|
|
|
|
@login_required
|
|
|
|
|
@user_has_permissions(
|
|
|
|
|
'view_activity',
|
|
|
|
|
'send_texts',
|
|
|
|
|
'send_emails',
|
|
|
|
|
'manage_templates',
|
|
|
|
|
'manage_api_keys',
|
|
|
|
|
admin_override=True,
|
|
|
|
|
any_=True
|
|
|
|
|
)
|
|
|
|
|
def view_template_versions(service_id, template_id):
|
|
|
|
|
return render_template(
|
|
|
|
|
'views/templates/choose_history.html',
|
2016-12-08 11:50:59 +00:00
|
|
|
versions=[
|
2016-12-20 14:38:34 +00:00
|
|
|
get_template(
|
|
|
|
|
template,
|
|
|
|
|
current_service,
|
|
|
|
|
expand_emails=True,
|
|
|
|
|
letter_preview_url=url_for(
|
2017-04-28 16:04:52 +01:00
|
|
|
'.view_template_version_preview',
|
2016-12-20 14:38:34 +00:00
|
|
|
service_id=service_id,
|
|
|
|
|
template_id=template_id,
|
|
|
|
|
version=template['version'],
|
2017-04-28 16:04:52 +01:00
|
|
|
filetype='png',
|
2016-12-20 14:38:34 +00:00
|
|
|
)
|
|
|
|
|
)
|
2016-12-08 11:50:59 +00:00
|
|
|
for template in service_api_client.get_service_template_versions(service_id, template_id)['data']
|
|
|
|
|
]
|
2016-05-11 11:20:45 +01:00
|
|
|
)
|
2016-06-06 11:09:46 +01:00
|
|
|
|
|
|
|
|
|
2016-06-07 14:28:02 +01:00
|
|
|
def get_last_use_message(template_name, template_statistics):
|
2016-06-06 11:09:46 +01:00
|
|
|
try:
|
|
|
|
|
most_recent_use = max(
|
2016-06-06 11:42:40 +01:00
|
|
|
parse(template_stats['updated_at']).replace(tzinfo=None)
|
2016-06-06 11:09:46 +01:00
|
|
|
for template_stats in template_statistics
|
|
|
|
|
)
|
|
|
|
|
except ValueError:
|
2016-06-06 11:42:40 +01:00
|
|
|
return '{} has never been used'.format(template_name)
|
2016-06-06 11:09:46 +01:00
|
|
|
|
|
|
|
|
return '{} was last used {} ago'.format(
|
2016-06-06 11:42:40 +01:00
|
|
|
template_name,
|
|
|
|
|
get_human_readable_delta(most_recent_use, datetime.utcnow())
|
2016-06-06 11:09:46 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_human_readable_delta(from_time, until_time):
|
|
|
|
|
delta = until_time - from_time
|
|
|
|
|
if delta < timedelta(seconds=60):
|
|
|
|
|
return 'under a minute'
|
|
|
|
|
elif delta < timedelta(hours=1):
|
|
|
|
|
minutes = int(delta.seconds / 60)
|
2016-06-06 11:42:40 +01:00
|
|
|
return '{} minute{}'.format(minutes, '' if minutes == 1 else 's')
|
2016-06-06 11:09:46 +01:00
|
|
|
elif delta < timedelta(days=1):
|
|
|
|
|
hours = int(delta.seconds / 3600)
|
2016-06-06 11:42:40 +01:00
|
|
|
return '{} hour{}'.format(hours, '' if hours == 1 else 's')
|
2016-06-06 11:09:46 +01:00
|
|
|
else:
|
|
|
|
|
days = delta.days
|
2016-06-06 11:42:40 +01:00
|
|
|
return '{} day{}'.format(days, '' if days == 1 else 's')
|
Merge email, text message + letter templates pages
Right now we have separate pages for email and text message templates.
In the future we will also have a separate page for letter templates.
This commit changes Notify to only have one page for all templates.
What is the problem?
---
The left-hand navigation is getting quite crowded, at 8 items for a
service that can send letters. Research suggests that the number of
objects an average human can hold in working memory is 7 ± 2 [1]. So
we’re at the limit of how many items the navigation should have.
In the future we will need to search/sort/filter templates by attributes
other than type, for example:
- show me the ‘confirmation’ templates
- show me the most recently used templates
- show me all templates containing the placeholder `((ref_no))`
These are hypothetical for now, but these needs (or others) may become
real in the future. At this point pre-filtering the list of templates
by type would restrict what searches a user could do. So by making this
change now we’re in a better position to iterate the design in the
future.
What’s the change?
---
This commit replaces the ‘Email templates’, ‘Text message templates’ and
‘Letter templates’ pages with one page called ‘Templates’.
This new templates page shows all the templates for the service, sorted
by most recently created first (as before).
To add a new template there is a new page with a form asking you what
kind of template you want to create. This is necessary because in the
past we knew what kind of template you wanted to create based on the
kind you were looking at.
What’s the impact of this change on new users?
---
This change alters the onboarding process slightly. We still want to
take people through the empty templates page from the call-to-action on
the dashboard because it helps them understand that to send a message
using Notify you need a template. But because we don’t have separate
pages for emails/text messages we will have to send users through the
extra step of choosing what kind of template to create. This is a bit
clunkier on first use but:
- it still gets the point across
- it takes them through the actual flow they will be using to create new
templates in the future (ie they’re learning how to use Notify, not
just being taken through a special onboarding route)
I’m not too worried about this change in terms of the experience for new
users. Furthermore, by making it now we get to validate whether it’s
causing any problems in the lab research booked for next week.
What’s the impact of this change on current services?
---
Looking at the top 15 services by number of templates[2], most are using
either text messages or emails. So this change would not have a
significant impact on these services because the page will not get any
longer. In other words we wouldn’t be making it worse for them.
Those services who do use both are not using as many templates. The
worst-case scenario is SSCS, who have 16 templates, evenly split between
email and text messages. So they would go from having 8 templates per
page to 16, which is still less than half the number that HMPO or
Digital Marketplace are managing.
References
---
1. https://en.wikipedia.org/wiki/The_Magical_Number_Seven,_Plus_or_Minus_Two
2. Template usage by service
Service name | Template count | Template types
---------------------------------------|----------------|---------------
Her Majesty's Passport Office | 40 | sms
Digital Marketplace | 40 | email
GovWifi-Staging | 19 | sms
GovWifi | 18 | sms
Digital Apprenticeship Service | 16 | email
SSCS | 16 | both
Crown Commercial Service MI Collection | 15 | email
Help with Prison Visits | 12 | both
Digital Future | 12 | email
Export Licensing Service | 11 | email
Civil Money Claims | 9 | both
DVLA Drivers Medical Service | 9 | sms
GOV.UK Notify | 8 | both
Manage your benefit overpayments | 8 | both
Tax Renewals | 8 | both
2017-02-28 12:16:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def should_show_template(template_type):
|
|
|
|
|
return (
|
|
|
|
|
template_type != 'letter' or
|
2017-06-23 13:35:08 +01:00
|
|
|
'letter' in current_service['permissions']
|
Merge email, text message + letter templates pages
Right now we have separate pages for email and text message templates.
In the future we will also have a separate page for letter templates.
This commit changes Notify to only have one page for all templates.
What is the problem?
---
The left-hand navigation is getting quite crowded, at 8 items for a
service that can send letters. Research suggests that the number of
objects an average human can hold in working memory is 7 ± 2 [1]. So
we’re at the limit of how many items the navigation should have.
In the future we will need to search/sort/filter templates by attributes
other than type, for example:
- show me the ‘confirmation’ templates
- show me the most recently used templates
- show me all templates containing the placeholder `((ref_no))`
These are hypothetical for now, but these needs (or others) may become
real in the future. At this point pre-filtering the list of templates
by type would restrict what searches a user could do. So by making this
change now we’re in a better position to iterate the design in the
future.
What’s the change?
---
This commit replaces the ‘Email templates’, ‘Text message templates’ and
‘Letter templates’ pages with one page called ‘Templates’.
This new templates page shows all the templates for the service, sorted
by most recently created first (as before).
To add a new template there is a new page with a form asking you what
kind of template you want to create. This is necessary because in the
past we knew what kind of template you wanted to create based on the
kind you were looking at.
What’s the impact of this change on new users?
---
This change alters the onboarding process slightly. We still want to
take people through the empty templates page from the call-to-action on
the dashboard because it helps them understand that to send a message
using Notify you need a template. But because we don’t have separate
pages for emails/text messages we will have to send users through the
extra step of choosing what kind of template to create. This is a bit
clunkier on first use but:
- it still gets the point across
- it takes them through the actual flow they will be using to create new
templates in the future (ie they’re learning how to use Notify, not
just being taken through a special onboarding route)
I’m not too worried about this change in terms of the experience for new
users. Furthermore, by making it now we get to validate whether it’s
causing any problems in the lab research booked for next week.
What’s the impact of this change on current services?
---
Looking at the top 15 services by number of templates[2], most are using
either text messages or emails. So this change would not have a
significant impact on these services because the page will not get any
longer. In other words we wouldn’t be making it worse for them.
Those services who do use both are not using as many templates. The
worst-case scenario is SSCS, who have 16 templates, evenly split between
email and text messages. So they would go from having 8 templates per
page to 16, which is still less than half the number that HMPO or
Digital Marketplace are managing.
References
---
1. https://en.wikipedia.org/wiki/The_Magical_Number_Seven,_Plus_or_Minus_Two
2. Template usage by service
Service name | Template count | Template types
---------------------------------------|----------------|---------------
Her Majesty's Passport Office | 40 | sms
Digital Marketplace | 40 | email
GovWifi-Staging | 19 | sms
GovWifi | 18 | sms
Digital Apprenticeship Service | 16 | email
SSCS | 16 | both
Crown Commercial Service MI Collection | 15 | email
Help with Prison Visits | 12 | both
Digital Future | 12 | email
Export Licensing Service | 11 | email
Civil Money Claims | 9 | both
DVLA Drivers Medical Service | 9 | sms
GOV.UK Notify | 8 | both
Manage your benefit overpayments | 8 | both
Tax Renewals | 8 | both
2017-02-28 12:16:35 +00:00
|
|
|
)
|