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
|
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
|
2021-03-10 13:55:06 +00:00
|
|
|
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
|
|
|
|
2021-04-14 07:11:01 +01:00
|
|
|
@autocommit
|
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})
|
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
|
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):
|
|
|
|
|
db.session.add(template)
|
|
|
|
|
|
|
|
|
|
|
2021-04-14 07:11:01 +01:00
|
|
|
@autocommit
|
2018-01-10 12:40:14 +00:00
|
|
|
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,
|
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
|
|
|
|
|
|
|
|
|
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(
|
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(
|
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(
|
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
|