mirror of
https://github.com/GSA/notifications-api.git
synced 2026-04-30 14:19:43 -04:00
When you add a new template, it’s probably the one that you want to do subsequent stuff with. But it’s also helpful to see the template in context (with its siblings) to understand that there are multiple templates. So we don’t want to do what we do in https://github.com/alphagov/notifications-admin/pull/648 for adding a new template. But we _can_ make your brand-new template appear first by always ordering by when the template was created. This also removes the confusion caused by having `updated_at` affecting order, and causing the templates to move around all the time.
56 lines
1.5 KiB
Python
56 lines
1.5 KiB
Python
import uuid
|
|
from app import db
|
|
from app.models import (Template, Service)
|
|
from sqlalchemy import (asc, desc)
|
|
|
|
from app.dao.dao_utils import (
|
|
transactional,
|
|
version_class
|
|
)
|
|
|
|
|
|
@transactional
|
|
@version_class(Template)
|
|
def dao_create_template(template):
|
|
template.id = uuid.uuid4() # must be set now so version history model can use same id
|
|
template.archived = False
|
|
db.session.add(template)
|
|
|
|
|
|
@transactional
|
|
@version_class(Template)
|
|
def dao_update_template(template):
|
|
db.session.add(template)
|
|
|
|
|
|
def dao_get_template_by_id_and_service_id(template_id, service_id, version=None):
|
|
if version is not None:
|
|
return Template.get_history_model().query.filter_by(
|
|
id=template_id,
|
|
service_id=service_id,
|
|
version=version).one()
|
|
return Template.query.filter_by(id=template_id, service_id=service_id).one()
|
|
|
|
|
|
def dao_get_template_by_id(template_id, version=None):
|
|
if version is not None:
|
|
return Template.get_history_model().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):
|
|
return Template.query.filter_by(
|
|
service_id=service_id,
|
|
archived=False
|
|
).order_by(
|
|
desc(Template.created_at)
|
|
).all()
|
|
|
|
|
|
def dao_get_template_versions(service_id, template_id):
|
|
history_model = Template.get_history_model()
|
|
return history_model.query.filter_by(service_id=service_id, id=template_id).order_by(
|
|
desc(history_model.version)).all()
|