mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-23 08:51:30 -05:00
history-meta's dynamic magic is insufficient for templates, where we need to be able to refer to the specific history table to take advantage of sqlalchemy's relationship management (2/3rds of an ORM). So replace it with a custom made version table. Had to change the version decorator slightly for this
59 lines
1.5 KiB
Python
59 lines
1.5 KiB
Python
import uuid
|
|
|
|
from sqlalchemy import (asc, desc)
|
|
|
|
from app import db
|
|
from app.models import (Template, TemplateHistory)
|
|
from app.dao.dao_utils import (
|
|
transactional,
|
|
version_class
|
|
)
|
|
|
|
|
|
@transactional
|
|
@version_class(Template, TemplateHistory)
|
|
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, TemplateHistory)
|
|
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 TemplateHistory.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 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):
|
|
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):
|
|
return TemplateHistory.query.filter_by(
|
|
service_id=service_id, id=template_id
|
|
).order_by(
|
|
desc(TemplateHistory.version)
|
|
).all()
|