Files
notifications-api/app/dao/templates_dao.py

99 lines
2.8 KiB
Python
Raw Normal View History

import uuid
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
from app import db
from app.dao.dao_utils import VersionOptions, autocommit, version_class
from app.models import Template, TemplateHistory, TemplateRedacted
2024-05-23 13:59:51 -07:00
from app.utils import utc_now
2016-01-13 11:04:13 +00:00
@autocommit
2023-08-29 14:54:30 -07:00
@version_class(VersionOptions(Template, history_class=TemplateHistory))
def dao_create_template(template):
2023-08-29 14:54:30 -07:00
template.id = (
uuid.uuid4()
) # must be set now so version history model can use same id
template.archived = False
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)
db.session.add(template)
@autocommit
2023-08-29 14:54:30 -07:00
@version_class(VersionOptions(Template, history_class=TemplateHistory))
def dao_update_template(template):
db.session.add(template)
@autocommit
def dao_redact_template(template, user_id):
template.template_redacted.redact_personalisation = True
2024-05-23 13:59:51 -07:00
template.template_redacted.updated_at = utc_now()
template.template_redacted.updated_by_id = user_id
db.session.add(template.template_redacted)
def dao_get_template_by_id_and_service_id(template_id, service_id, version=None):
if version is not None:
return TemplateHistory.query.filter_by(
2023-08-29 14:54:30 -07:00
id=template_id, hidden=False, service_id=service_id, version=version
).one()
return Template.query.filter_by(
id=template_id, hidden=False, service_id=service_id
).one()
def dao_get_template_by_id(template_id, version=None):
if version is not None:
2023-08-29 14:54:30 -07:00
return TemplateHistory.query.filter_by(id=template_id, version=version).one()
return Template.query.filter_by(id=template_id).one()
def dao_get_all_templates_for_service(service_id, template_type=None):
if template_type is not None:
2023-08-29 14:54:30 -07:00
return (
Template.query.filter_by(
service_id=service_id,
template_type=template_type,
hidden=False,
archived=False,
)
.order_by(
asc(Template.name),
asc(Template.template_type),
)
.all()
)
return (
Template.query.filter_by(service_id=service_id, hidden=False, 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),
2023-08-29 14:54:30 -07:00
)
.all()
)
def dao_get_template_versions(service_id, template_id):
2023-08-29 14:54:30 -07:00
return (
TemplateHistory.query.filter_by(
service_id=service_id,
id=template_id,
hidden=False,
)
.order_by(desc(TemplateHistory.version))
.all()
)