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
|
|
|
|
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-04-14 07:11:01 +01:00
|
|
|
from app.dao.dao_utils import VersionOptions, autocommit, version_class
|
2023-03-02 20:20:31 -05:00
|
|
|
from app.models import Template, TemplateHistory, TemplateRedacted
|
2016-01-13 11:04:13 +00:00
|
|
|
|
2016-04-25 10:38:37 +01:00
|
|
|
|
2021-04-14 07:11:01 +01:00
|
|
|
@autocommit
|
2023-08-29 14:54:30 -07:00
|
|
|
@version_class(VersionOptions(Template, history_class=TemplateHistory))
|
2017-03-08 13:03:44 +00:00
|
|
|
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
|
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})
|
2018-11-02 16:00:22 +00:00
|
|
|
|
2019-06-26 18:01:20 +01:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
2021-04-14 07:11:01 +01:00
|
|
|
@autocommit
|
2023-08-29 14:54:30 -07:00
|
|
|
@version_class(VersionOptions(Template, history_class=TemplateHistory))
|
2016-02-22 09:46:16 +00:00
|
|
|
def dao_update_template(template):
|
|
|
|
|
db.session.add(template)
|
|
|
|
|
|
|
|
|
|
|
2021-04-14 07:11:01 +01:00
|
|
|
@autocommit
|
2017-06-28 10:26:25 +01:00
|
|
|
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(
|
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()
|
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:
|
2023-08-29 14:54:30 -07:00
|
|
|
return TemplateHistory.query.filter_by(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:
|
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(
|
2017-11-23 11:37:35 +00:00
|
|
|
asc(Template.name),
|
|
|
|
|
asc(Template.template_type),
|
2023-08-29 14:54:30 -07:00
|
|
|
)
|
|
|
|
|
.all()
|
|
|
|
|
)
|
2016-05-09 15:59:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
)
|