Add endpoint for getting / creating a hidden template

We want a way of getting the hidden precompiled template from admin,
so this adds an endpoint which gets the template or creates it if it doesn't
exist. The function to get or create the hidden template already existed
but has been moved to the template DAO now that it is used in more
places.
This commit is contained in:
Katie Smith
2019-09-05 12:07:35 +01:00
parent 5cdce44e42
commit 0c1fa3852f
4 changed files with 85 additions and 30 deletions

View File

@@ -1,10 +1,13 @@
from datetime import datetime
import uuid
from flask import current_app
from sqlalchemy import asc, desc
from app import db
from app.models import (
LETTER_TYPE,
SECOND_CLASS,
Template,
TemplateHistory,
TemplateRedacted
@@ -14,6 +17,7 @@ from app.dao.dao_utils import (
version_class,
VersionOptions,
)
from app.dao.users_dao import get_user_by_id
@transactional
@@ -135,3 +139,28 @@ def dao_get_template_versions(service_id, template_id):
).order_by(
desc(TemplateHistory.version)
).all()
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