mirror of
https://github.com/GSA/notifications-api.git
synced 2026-07-16 19:30:51 -04:00
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:
@@ -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
|
||||
|
||||
@@ -25,7 +25,9 @@ from app.dao.templates_dao import (
|
||||
dao_get_all_templates_for_service,
|
||||
dao_get_template_versions,
|
||||
dao_update_template_reply_to,
|
||||
dao_get_template_by_id)
|
||||
dao_get_template_by_id,
|
||||
get_precompiled_letter_template,
|
||||
)
|
||||
from app.errors import (
|
||||
register_errors,
|
||||
InvalidRequest
|
||||
@@ -138,6 +140,14 @@ def update_template(service_id, template_id):
|
||||
return jsonify(data=template_schema.dump(update_dict).data), 200
|
||||
|
||||
|
||||
@template_blueprint.route('/precompiled', methods=['GET'])
|
||||
def get_precompiled_template_for_service(service_id):
|
||||
template = get_precompiled_letter_template(service_id)
|
||||
template_dict = template_schema.dump(template).data
|
||||
|
||||
return jsonify(template_dict), 200
|
||||
|
||||
|
||||
@template_blueprint.route('', methods=['GET'])
|
||||
def get_all_templates_for_service(service_id):
|
||||
templates = dao_get_all_templates_for_service(service_id=service_id)
|
||||
|
||||
@@ -11,11 +11,9 @@ from app.celery.research_mode_tasks import create_fake_letter_response_file
|
||||
from app.clients.document_download import DocumentDownloadError
|
||||
from app.config import QueueNames, TaskNames
|
||||
from app.dao.notifications_dao import update_notification_status_by_reference
|
||||
from app.dao.templates_dao import dao_create_template
|
||||
from app.dao.users_dao import get_user_by_id
|
||||
from app.dao.templates_dao import get_precompiled_letter_template
|
||||
from app.letters.utils import upload_letter_pdf
|
||||
from app.models import (
|
||||
Template,
|
||||
SMS_TYPE,
|
||||
EMAIL_TYPE,
|
||||
LETTER_TYPE,
|
||||
@@ -27,7 +25,6 @@ from app.models import (
|
||||
NOTIFICATION_SENDING,
|
||||
NOTIFICATION_DELIVERED,
|
||||
NOTIFICATION_PENDING_VIRUS_CHECK,
|
||||
SECOND_CLASS
|
||||
)
|
||||
from app.notifications.process_letter_notifications import (
|
||||
create_letter_notification
|
||||
@@ -342,28 +339,3 @@ def get_reply_to_text(notification_type, form, template):
|
||||
reply_to = template.get_reply_to_text()
|
||||
|
||||
return reply_to
|
||||
|
||||
|
||||
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
|
||||
|
||||
@@ -389,6 +389,50 @@ def test_should_be_able_to_archive_template(client, sample_template):
|
||||
assert Template.query.first().archived
|
||||
|
||||
|
||||
def test_get_precompiled_template_for_service(
|
||||
client,
|
||||
notify_user,
|
||||
sample_service,
|
||||
):
|
||||
assert len(sample_service.templates) == 0
|
||||
|
||||
response = client.get(
|
||||
'/service/{}/template/precompiled'.format(sample_service.id),
|
||||
headers=[create_authorization_header()],
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert len(sample_service.templates) == 1
|
||||
|
||||
data = json.loads(response.get_data(as_text=True))
|
||||
assert data['name'] == 'Pre-compiled PDF'
|
||||
assert data['hidden'] is True
|
||||
|
||||
|
||||
def test_get_precompiled_template_for_service_when_service_has_existing_precompiled_template(
|
||||
client,
|
||||
notify_user,
|
||||
sample_service,
|
||||
):
|
||||
create_template(
|
||||
sample_service,
|
||||
template_name='Exisiting precompiled template',
|
||||
template_type=LETTER_TYPE,
|
||||
hidden=True)
|
||||
assert len(sample_service.templates) == 1
|
||||
|
||||
response = client.get(
|
||||
'/service/{}/template/precompiled'.format(sample_service.id),
|
||||
headers=[create_authorization_header()],
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert len(sample_service.templates) == 1
|
||||
|
||||
data = json.loads(response.get_data(as_text=True))
|
||||
assert data['name'] == 'Exisiting precompiled template'
|
||||
assert data['hidden'] is True
|
||||
|
||||
|
||||
def test_should_be_able_to_get_all_templates_for_a_service(client, sample_user, sample_service):
|
||||
data = {
|
||||
'name': 'my template 1',
|
||||
|
||||
Reference in New Issue
Block a user