2016-04-25 10:38:37 +01:00
|
|
|
import uuid
|
2021-03-10 13:55:06 +00:00
|
|
|
from datetime import datetime
|
2016-08-02 16:23:14 +01:00
|
|
|
|
2019-09-05 12:07:35 +01:00
|
|
|
from flask import current_app
|
List templates in alphabetical order
Currently templates are ordered by the newest created first. This made
sense when, after creating a new template, you were landed on the page
that listed all the templates. In other words, you needed to see
confirmation of the thing that you’ve just done.
Now (since https://github.com/alphagov/notifications-admin/pull/1195)
you get landed on the page for just that template. So you always see
the template you’ve just created, no matter how the list of templates is
ordered. So we are free to change the order of the templates.
Ordering by time created is not great, because it gives users no control
over which templates appear first. For example, our research reported
this from one team:
> One frustration they have is that when you add a new template it
> automatically goes to the top of the list. To get round this, whenever
> they add a new template they delete all of the existing ones and start
> again because they want to keep their templates in numerical order.
> They'd like to be able to control the order of templates in the list.
We _could_ give people some sort of drag-and-drop template ordering
thing. But this feels like overkill. I think that alphabetical order is
better because:
- it’s easily discoverable – anyone who wants to know how a list is
ordered can quickly tell just by looking at it
- it’s universal – everyone knows how alphabetical ordering works
- it’s familiar – this is how people documents on their computer are
ordered; there’s no new UI to learn
- it’s what users are doing already – from the same service as above:
> They number their templates 1,2a, 2b, 3a etc
So this commit changes the ordering from newest created first to
alphabetical.
Previous changes to template order and navigation:
- https://github.com/alphagov/notifications-admin/pull/1163
- https://github.com/alphagov/notifications-admin/pull/1195
- https://github.com/alphagov/notifications-admin/pull/1330
Implementation notes
---
I refactored some of the tests here. I still don’t think they’re great
tests, but they’re a little more Pythonic now at least.
I also added a sort by template type, so that the order is deterministic
when you have, for example, an email template and a text message
template with the same name. If you have two text message templates with
the same name you’re on your own.
2017-11-23 11:37:35 +00:00
|
|
|
from sqlalchemy import asc, desc
|
2016-01-13 11:04:13 +00:00
|
|
|
|
2016-08-02 16:23:14 +01:00
|
|
|
from app import db
|
2021-03-10 13:55:06 +00:00
|
|
|
from app.dao.dao_utils import VersionOptions, transactional, version_class
|
|
|
|
|
from app.dao.users_dao import get_user_by_id
|
2018-03-07 15:42:59 +00:00
|
|
|
from app.models import (
|
2019-09-05 12:07:35 +01:00
|
|
|
LETTER_TYPE,
|
|
|
|
|
SECOND_CLASS,
|
2018-03-07 15:42:59 +00:00
|
|
|
Template,
|
|
|
|
|
TemplateHistory,
|
2021-03-10 13:55:06 +00:00
|
|
|
TemplateRedacted,
|
2018-03-07 15:42:59 +00:00
|
|
|
)
|
2016-01-13 11:04:13 +00:00
|
|
|
|
2016-04-25 10:38:37 +01:00
|
|
|
|
|
|
|
|
@transactional
|
2019-03-07 17:39:38 +00:00
|
|
|
@version_class(
|
|
|
|
|
VersionOptions(Template, history_class=TemplateHistory)
|
|
|
|
|
)
|
2017-03-08 13:03:44 +00:00
|
|
|
def dao_create_template(template):
|
|
|
|
|
template.id = uuid.uuid4() # must be set now so version history model can use same id
|
2016-05-16 16:16:14 +01:00
|
|
|
template.archived = False
|
2017-06-28 10:26:25 +01:00
|
|
|
|
2019-06-26 18:01:20 +01:00
|
|
|
redacted_dict = {
|
|
|
|
|
"template": template,
|
|
|
|
|
"redact_personalisation": False,
|
|
|
|
|
}
|
|
|
|
|
if template.created_by:
|
|
|
|
|
redacted_dict.update({"updated_by": template.created_by})
|
|
|
|
|
else:
|
|
|
|
|
redacted_dict.update({"updated_by_id": template.created_by_id})
|
|
|
|
|
|
|
|
|
|
template.template_redacted = TemplateRedacted(**redacted_dict)
|
2017-06-28 10:26:25 +01:00
|
|
|
|
2016-02-22 09:46:16 +00:00
|
|
|
db.session.add(template)
|
|
|
|
|
|
|
|
|
|
|
2016-04-25 10:38:37 +01:00
|
|
|
@transactional
|
2019-03-07 17:39:38 +00:00
|
|
|
@version_class(
|
|
|
|
|
VersionOptions(Template, history_class=TemplateHistory)
|
|
|
|
|
)
|
2016-02-22 09:46:16 +00:00
|
|
|
def dao_update_template(template):
|
2018-11-08 11:16:59 +00:00
|
|
|
if template.archived:
|
|
|
|
|
template.folder = None
|
|
|
|
|
|
2016-02-22 09:46:16 +00:00
|
|
|
db.session.add(template)
|
|
|
|
|
|
|
|
|
|
|
2018-01-10 12:40:14 +00:00
|
|
|
@transactional
|
|
|
|
|
def dao_update_template_reply_to(template_id, reply_to):
|
|
|
|
|
Template.query.filter_by(id=template_id).update(
|
|
|
|
|
{"service_letter_contact_id": reply_to,
|
|
|
|
|
"updated_at": datetime.utcnow(),
|
|
|
|
|
"version": Template.version + 1,
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
template = Template.query.filter_by(id=template_id).one()
|
|
|
|
|
|
|
|
|
|
history = TemplateHistory(**
|
|
|
|
|
{
|
|
|
|
|
"id": template.id,
|
|
|
|
|
"name": template.name,
|
|
|
|
|
"template_type": template.template_type,
|
|
|
|
|
"created_at": template.created_at,
|
|
|
|
|
"updated_at": template.updated_at,
|
|
|
|
|
"content": template.content,
|
|
|
|
|
"service_id": template.service_id,
|
|
|
|
|
"subject": template.subject,
|
2019-02-12 10:41:44 +00:00
|
|
|
"postage": template.postage,
|
2018-01-10 12:40:14 +00:00
|
|
|
"created_by_id": template.created_by_id,
|
|
|
|
|
"version": template.version,
|
|
|
|
|
"archived": template.archived,
|
|
|
|
|
"process_type": template.process_type,
|
2020-07-02 12:12:34 +01:00
|
|
|
"service_letter_contact_id": template.service_letter_contact_id,
|
|
|
|
|
"broadcast_data": template.broadcast_data,
|
2018-01-10 12:40:14 +00:00
|
|
|
})
|
|
|
|
|
db.session.add(history)
|
2018-01-10 13:32:54 +00:00
|
|
|
return template
|
2018-01-10 12:40:14 +00:00
|
|
|
|
|
|
|
|
|
2017-06-28 10:26:25 +01:00
|
|
|
@transactional
|
|
|
|
|
def dao_redact_template(template, user_id):
|
|
|
|
|
template.template_redacted.redact_personalisation = True
|
|
|
|
|
template.template_redacted.updated_at = datetime.utcnow()
|
|
|
|
|
template.template_redacted.updated_by_id = user_id
|
|
|
|
|
db.session.add(template.template_redacted)
|
|
|
|
|
|
|
|
|
|
|
2016-05-06 15:42:43 +01:00
|
|
|
def dao_get_template_by_id_and_service_id(template_id, service_id, version=None):
|
|
|
|
|
if version is not None:
|
2016-08-02 16:23:14 +01:00
|
|
|
return TemplateHistory.query.filter_by(
|
2016-05-06 15:42:43 +01:00
|
|
|
id=template_id,
|
2018-02-26 13:18:51 +00:00
|
|
|
hidden=False,
|
2016-05-06 15:42:43 +01:00
|
|
|
service_id=service_id,
|
|
|
|
|
version=version).one()
|
2018-02-26 13:18:51 +00:00
|
|
|
return Template.query.filter_by(id=template_id, hidden=False, service_id=service_id).one()
|
2016-02-22 09:46:16 +00:00
|
|
|
|
|
|
|
|
|
2016-05-06 15:42:43 +01:00
|
|
|
def dao_get_template_by_id(template_id, version=None):
|
|
|
|
|
if version is not None:
|
2016-08-02 16:23:14 +01:00
|
|
|
return TemplateHistory.query.filter_by(
|
2016-05-06 15:42:43 +01:00
|
|
|
id=template_id,
|
|
|
|
|
version=version).one()
|
2016-03-11 15:34:20 +00:00
|
|
|
return Template.query.filter_by(id=template_id).one()
|
2016-02-24 11:51:02 +00:00
|
|
|
|
|
|
|
|
|
2017-04-18 16:19:25 +01:00
|
|
|
def dao_get_all_templates_for_service(service_id, template_type=None):
|
|
|
|
|
if template_type is not None:
|
|
|
|
|
return Template.query.filter_by(
|
|
|
|
|
service_id=service_id,
|
|
|
|
|
template_type=template_type,
|
2018-02-26 13:18:51 +00:00
|
|
|
hidden=False,
|
2017-04-18 16:19:25 +01:00
|
|
|
archived=False
|
|
|
|
|
).order_by(
|
List templates in alphabetical order
Currently templates are ordered by the newest created first. This made
sense when, after creating a new template, you were landed on the page
that listed all the templates. In other words, you needed to see
confirmation of the thing that you’ve just done.
Now (since https://github.com/alphagov/notifications-admin/pull/1195)
you get landed on the page for just that template. So you always see
the template you’ve just created, no matter how the list of templates is
ordered. So we are free to change the order of the templates.
Ordering by time created is not great, because it gives users no control
over which templates appear first. For example, our research reported
this from one team:
> One frustration they have is that when you add a new template it
> automatically goes to the top of the list. To get round this, whenever
> they add a new template they delete all of the existing ones and start
> again because they want to keep their templates in numerical order.
> They'd like to be able to control the order of templates in the list.
We _could_ give people some sort of drag-and-drop template ordering
thing. But this feels like overkill. I think that alphabetical order is
better because:
- it’s easily discoverable – anyone who wants to know how a list is
ordered can quickly tell just by looking at it
- it’s universal – everyone knows how alphabetical ordering works
- it’s familiar – this is how people documents on their computer are
ordered; there’s no new UI to learn
- it’s what users are doing already – from the same service as above:
> They number their templates 1,2a, 2b, 3a etc
So this commit changes the ordering from newest created first to
alphabetical.
Previous changes to template order and navigation:
- https://github.com/alphagov/notifications-admin/pull/1163
- https://github.com/alphagov/notifications-admin/pull/1195
- https://github.com/alphagov/notifications-admin/pull/1330
Implementation notes
---
I refactored some of the tests here. I still don’t think they’re great
tests, but they’re a little more Pythonic now at least.
I also added a sort by template type, so that the order is deterministic
when you have, for example, an email template and a text message
template with the same name. If you have two text message templates with
the same name you’re on your own.
2017-11-23 11:37:35 +00:00
|
|
|
asc(Template.name),
|
|
|
|
|
asc(Template.template_type),
|
2017-04-18 16:19:25 +01:00
|
|
|
).all()
|
|
|
|
|
|
2016-04-11 17:39:49 +01:00
|
|
|
return Template.query.filter_by(
|
2016-05-23 14:00:28 +01:00
|
|
|
service_id=service_id,
|
2018-02-26 13:18:51 +00:00
|
|
|
hidden=False,
|
2016-05-23 14:00:28 +01:00
|
|
|
archived=False
|
2016-04-11 17:39:49 +01:00
|
|
|
).order_by(
|
List templates in alphabetical order
Currently templates are ordered by the newest created first. This made
sense when, after creating a new template, you were landed on the page
that listed all the templates. In other words, you needed to see
confirmation of the thing that you’ve just done.
Now (since https://github.com/alphagov/notifications-admin/pull/1195)
you get landed on the page for just that template. So you always see
the template you’ve just created, no matter how the list of templates is
ordered. So we are free to change the order of the templates.
Ordering by time created is not great, because it gives users no control
over which templates appear first. For example, our research reported
this from one team:
> One frustration they have is that when you add a new template it
> automatically goes to the top of the list. To get round this, whenever
> they add a new template they delete all of the existing ones and start
> again because they want to keep their templates in numerical order.
> They'd like to be able to control the order of templates in the list.
We _could_ give people some sort of drag-and-drop template ordering
thing. But this feels like overkill. I think that alphabetical order is
better because:
- it’s easily discoverable – anyone who wants to know how a list is
ordered can quickly tell just by looking at it
- it’s universal – everyone knows how alphabetical ordering works
- it’s familiar – this is how people documents on their computer are
ordered; there’s no new UI to learn
- it’s what users are doing already – from the same service as above:
> They number their templates 1,2a, 2b, 3a etc
So this commit changes the ordering from newest created first to
alphabetical.
Previous changes to template order and navigation:
- https://github.com/alphagov/notifications-admin/pull/1163
- https://github.com/alphagov/notifications-admin/pull/1195
- https://github.com/alphagov/notifications-admin/pull/1330
Implementation notes
---
I refactored some of the tests here. I still don’t think they’re great
tests, but they’re a little more Pythonic now at least.
I also added a sort by template type, so that the order is deterministic
when you have, for example, an email template and a text message
template with the same name. If you have two text message templates with
the same name you’re on your own.
2017-11-23 11:37:35 +00:00
|
|
|
asc(Template.name),
|
|
|
|
|
asc(Template.template_type),
|
2016-04-11 17:39:49 +01:00
|
|
|
).all()
|
2016-05-09 15:59:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def dao_get_template_versions(service_id, template_id):
|
2016-08-02 16:23:14 +01:00
|
|
|
return TemplateHistory.query.filter_by(
|
2018-02-26 13:18:51 +00:00
|
|
|
service_id=service_id, id=template_id,
|
|
|
|
|
hidden=False,
|
2016-08-02 16:23:14 +01:00
|
|
|
).order_by(
|
|
|
|
|
desc(TemplateHistory.version)
|
|
|
|
|
).all()
|
2019-09-05 12:07:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_precompiled_letter_template(service_id):
|
|
|
|
|
template = Template.query.filter_by(
|
|
|
|
|
service_id=service_id,
|
|
|
|
|
template_type=LETTER_TYPE,
|
|
|
|
|
hidden=True
|
|
|
|
|
).first()
|
|
|
|
|
if template is not None:
|
|
|
|
|
return template
|
|
|
|
|
|
|
|
|
|
template = Template(
|
|
|
|
|
name='Pre-compiled PDF',
|
|
|
|
|
created_by=get_user_by_id(current_app.config['NOTIFY_USER_ID']),
|
|
|
|
|
service_id=service_id,
|
|
|
|
|
template_type=LETTER_TYPE,
|
|
|
|
|
hidden=True,
|
|
|
|
|
subject='Pre-compiled PDF',
|
|
|
|
|
content='',
|
|
|
|
|
postage=SECOND_CLASS
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
dao_create_template(template)
|
|
|
|
|
|
|
|
|
|
return template
|