2018-02-27 15:57:37 +00:00
|
|
|
import base64
|
2018-03-09 15:50:43 +00:00
|
|
|
from io import BytesIO
|
2018-03-05 14:11:37 +00:00
|
|
|
|
|
|
|
|
import botocore
|
2018-03-09 15:50:43 +00:00
|
|
|
from PyPDF2.utils import PdfReadError
|
2016-02-17 17:04:50 +00:00
|
|
|
from flask import (
|
|
|
|
|
Blueprint,
|
2018-02-26 13:57:41 +00:00
|
|
|
current_app,
|
2016-02-22 12:55:18 +00:00
|
|
|
jsonify,
|
2018-02-27 15:57:37 +00:00
|
|
|
request)
|
2018-08-16 16:25:58 +01:00
|
|
|
from notifications_utils import SMS_CHAR_COUNT_LIMIT
|
2018-03-09 15:50:43 +00:00
|
|
|
from notifications_utils.pdf import extract_page_from_pdf
|
2020-04-13 13:48:23 +01:00
|
|
|
from notifications_utils.template import SMSMessageTemplate
|
2018-02-27 15:57:37 +00:00
|
|
|
from requests import post as requests_post
|
2018-11-07 17:07:04 +00:00
|
|
|
from sqlalchemy.orm.exc import NoResultFound
|
2018-03-05 14:11:37 +00:00
|
|
|
|
2018-02-26 13:57:41 +00:00
|
|
|
from app.dao.notifications_dao import get_notification_by_id
|
2018-03-12 11:05:05 +00:00
|
|
|
from app.dao.services_dao import dao_fetch_service_by_id
|
2018-11-07 16:34:51 +00:00
|
|
|
from app.dao.template_folder_dao import dao_get_template_folder_by_id_and_service_id
|
2016-02-22 12:55:18 +00:00
|
|
|
from app.dao.templates_dao import (
|
|
|
|
|
dao_update_template,
|
|
|
|
|
dao_create_template,
|
2017-06-28 10:26:25 +01:00
|
|
|
dao_redact_template,
|
2016-02-22 12:55:18 +00:00
|
|
|
dao_get_template_by_id_and_service_id,
|
2016-05-09 15:59:34 +01:00
|
|
|
dao_get_all_templates_for_service,
|
2018-01-10 12:40:14 +00:00
|
|
|
dao_get_template_versions,
|
2018-02-26 13:57:41 +00:00
|
|
|
dao_update_template_reply_to,
|
2019-09-05 12:07:35 +01:00
|
|
|
dao_get_template_by_id,
|
|
|
|
|
get_precompiled_letter_template,
|
|
|
|
|
)
|
2016-06-14 15:07:23 +01:00
|
|
|
from app.errors import (
|
|
|
|
|
register_errors,
|
|
|
|
|
InvalidRequest
|
|
|
|
|
)
|
2019-10-29 16:19:50 +00:00
|
|
|
from app.letters.utils import get_letter_pdf_and_metadata
|
2019-01-30 16:26:49 +00:00
|
|
|
from app.models import SMS_TYPE, Template, SECOND_CLASS, LETTER_TYPE
|
2018-03-12 11:05:05 +00:00
|
|
|
from app.notifications.validators import service_has_permission, check_reply_to
|
2018-11-02 16:00:22 +00:00
|
|
|
from app.schema_validation import validate
|
2018-03-12 11:05:05 +00:00
|
|
|
from app.schemas import (template_schema, template_history_schema)
|
2018-11-02 16:00:22 +00:00
|
|
|
from app.template.template_schemas import post_create_template_schema
|
2020-04-06 14:25:43 +01:00
|
|
|
from app.utils import get_public_notify_type_text
|
2016-02-22 12:55:18 +00:00
|
|
|
|
2017-11-28 10:35:16 +00:00
|
|
|
template_blueprint = Blueprint('template', __name__, url_prefix='/service/<uuid:service_id>/template')
|
|
|
|
|
|
2017-03-16 18:15:49 +00:00
|
|
|
register_errors(template_blueprint)
|
2016-02-17 17:04:50 +00:00
|
|
|
|
2016-01-13 11:04:13 +00:00
|
|
|
|
2016-04-29 10:36:59 +01:00
|
|
|
def _content_count_greater_than_limit(content, template_type):
|
2016-12-09 15:56:25 +00:00
|
|
|
if template_type != SMS_TYPE:
|
|
|
|
|
return False
|
|
|
|
|
template = SMSMessageTemplate({'content': content, 'template_type': template_type})
|
2019-11-18 16:16:28 +00:00
|
|
|
return template.is_message_too_long()
|
2016-04-29 10:36:59 +01:00
|
|
|
|
|
|
|
|
|
2018-11-07 12:48:56 +00:00
|
|
|
def validate_parent_folder(template_json):
|
|
|
|
|
if template_json.get("parent_folder_id"):
|
2018-11-07 17:07:04 +00:00
|
|
|
try:
|
|
|
|
|
return dao_get_template_folder_by_id_and_service_id(
|
|
|
|
|
template_folder_id=template_json.pop("parent_folder_id"),
|
|
|
|
|
service_id=template_json['service']
|
|
|
|
|
)
|
|
|
|
|
except NoResultFound:
|
|
|
|
|
raise InvalidRequest("parent_folder_id not found", status_code=400)
|
2018-11-07 12:48:56 +00:00
|
|
|
else:
|
|
|
|
|
return None
|
2018-11-05 10:54:42 +00:00
|
|
|
|
|
|
|
|
|
2017-03-16 18:15:49 +00:00
|
|
|
@template_blueprint.route('', methods=['POST'])
|
2016-02-22 12:55:18 +00:00
|
|
|
def create_template(service_id):
|
|
|
|
|
fetched_service = dao_fetch_service_by_id(service_id=service_id)
|
2018-11-02 16:00:22 +00:00
|
|
|
# permissions needs to be placed here otherwise marshmallow will interfere with versioning
|
2017-06-27 18:56:35 +01:00
|
|
|
permissions = fetched_service.permissions
|
2018-11-05 10:54:42 +00:00
|
|
|
template_json = validate(request.get_json(), post_create_template_schema)
|
2018-11-07 12:48:56 +00:00
|
|
|
folder = validate_parent_folder(template_json=template_json)
|
2018-11-05 10:54:42 +00:00
|
|
|
new_template = Template.from_json(template_json, folder)
|
2017-06-27 18:56:35 +01:00
|
|
|
|
2017-06-30 15:00:44 +01:00
|
|
|
if not service_has_permission(new_template.template_type, permissions):
|
|
|
|
|
message = "Creating {} templates is not allowed".format(
|
|
|
|
|
get_public_notify_type_text(new_template.template_type))
|
|
|
|
|
errors = {'template_type': [message]}
|
|
|
|
|
raise InvalidRequest(errors, 403)
|
2017-06-27 18:56:35 +01:00
|
|
|
|
2019-01-30 16:26:49 +00:00
|
|
|
if not new_template.postage and new_template.template_type == LETTER_TYPE:
|
|
|
|
|
new_template.postage = SECOND_CLASS
|
2018-12-17 10:03:54 +00:00
|
|
|
|
2016-02-22 12:55:18 +00:00
|
|
|
new_template.service = fetched_service
|
2018-11-02 16:00:22 +00:00
|
|
|
|
2016-06-14 15:07:23 +01:00
|
|
|
over_limit = _content_count_greater_than_limit(new_template.content, new_template.template_type)
|
2016-04-29 10:36:59 +01:00
|
|
|
if over_limit:
|
2018-08-16 16:25:58 +01:00
|
|
|
message = 'Content has a character count greater than the limit of {}'.format(SMS_CHAR_COUNT_LIMIT)
|
2016-06-14 15:07:23 +01:00
|
|
|
errors = {'content': [message]}
|
|
|
|
|
raise InvalidRequest(errors, status_code=400)
|
|
|
|
|
|
2017-12-15 17:06:11 +00:00
|
|
|
check_reply_to(service_id, new_template.reply_to, new_template.template_type)
|
|
|
|
|
|
2016-05-18 10:00:09 +01:00
|
|
|
dao_create_template(new_template)
|
2018-11-05 10:54:42 +00:00
|
|
|
|
2016-02-22 12:55:18 +00:00
|
|
|
return jsonify(data=template_schema.dump(new_template).data), 201
|
|
|
|
|
|
|
|
|
|
|
2017-03-16 18:15:49 +00:00
|
|
|
@template_blueprint.route('/<uuid:template_id>', methods=['POST'])
|
2016-02-22 12:55:18 +00:00
|
|
|
def update_template(service_id, template_id):
|
|
|
|
|
fetched_template = dao_get_template_by_id_and_service_id(template_id=template_id, service_id=service_id)
|
|
|
|
|
|
2017-06-30 15:00:44 +01:00
|
|
|
if not service_has_permission(fetched_template.template_type, fetched_template.service.permissions):
|
|
|
|
|
message = "Updating {} templates is not allowed".format(
|
|
|
|
|
get_public_notify_type_text(fetched_template.template_type))
|
|
|
|
|
errors = {'template_type': [message]}
|
|
|
|
|
|
|
|
|
|
raise InvalidRequest(errors, 403)
|
2017-06-27 18:56:35 +01:00
|
|
|
|
2017-06-28 10:26:25 +01:00
|
|
|
data = request.get_json()
|
|
|
|
|
|
|
|
|
|
# if redacting, don't update anything else
|
2017-06-28 17:03:12 +01:00
|
|
|
if data.get('redact_personalisation') is True:
|
|
|
|
|
return redact_template(fetched_template, data)
|
2017-06-28 10:26:25 +01:00
|
|
|
|
2018-01-10 12:40:14 +00:00
|
|
|
if "reply_to" in data:
|
|
|
|
|
check_reply_to(service_id, data.get("reply_to"), fetched_template.template_type)
|
|
|
|
|
updated = dao_update_template_reply_to(template_id=template_id, reply_to=data.get("reply_to"))
|
|
|
|
|
return jsonify(data=template_schema.dump(updated).data), 200
|
|
|
|
|
|
2016-02-22 12:55:18 +00:00
|
|
|
current_data = dict(template_schema.dump(fetched_template).data.items())
|
2016-06-01 12:19:59 +01:00
|
|
|
updated_template = dict(template_schema.dump(fetched_template).data.items())
|
2017-06-28 10:26:25 +01:00
|
|
|
updated_template.update(data)
|
2018-01-09 16:41:58 +00:00
|
|
|
|
2016-06-01 10:53:03 +01:00
|
|
|
# Check if there is a change to make.
|
2016-06-01 12:19:59 +01:00
|
|
|
if _template_has_not_changed(current_data, updated_template):
|
|
|
|
|
return jsonify(data=updated_template), 200
|
2016-06-01 10:53:03 +01:00
|
|
|
|
2016-06-14 15:07:23 +01:00
|
|
|
over_limit = _content_count_greater_than_limit(updated_template['content'], fetched_template.template_type)
|
2016-04-29 10:36:59 +01:00
|
|
|
if over_limit:
|
2018-08-16 16:25:58 +01:00
|
|
|
message = 'Content has a character count greater than the limit of {}'.format(SMS_CHAR_COUNT_LIMIT)
|
2016-06-14 15:07:23 +01:00
|
|
|
errors = {'content': [message]}
|
|
|
|
|
raise InvalidRequest(errors, status_code=400)
|
2017-12-15 17:06:11 +00:00
|
|
|
|
2018-01-09 16:41:58 +00:00
|
|
|
update_dict = template_schema.load(updated_template).data
|
2017-12-15 17:06:11 +00:00
|
|
|
|
2016-02-22 12:55:18 +00:00
|
|
|
dao_update_template(update_dict)
|
|
|
|
|
return jsonify(data=template_schema.dump(update_dict).data), 200
|
|
|
|
|
|
|
|
|
|
|
2019-09-05 12:07:35 +01:00
|
|
|
@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
|
|
|
|
|
|
|
|
|
|
|
2017-03-16 18:15:49 +00:00
|
|
|
@template_blueprint.route('', methods=['GET'])
|
2016-02-22 12:55:18 +00:00
|
|
|
def get_all_templates_for_service(service_id):
|
|
|
|
|
templates = dao_get_all_templates_for_service(service_id=service_id)
|
2016-06-14 15:07:23 +01:00
|
|
|
data = template_schema.dump(templates, many=True).data
|
2016-01-13 11:04:13 +00:00
|
|
|
return jsonify(data=data)
|
2016-02-22 12:55:18 +00:00
|
|
|
|
|
|
|
|
|
2017-03-16 18:15:49 +00:00
|
|
|
@template_blueprint.route('/<uuid:template_id>', methods=['GET'])
|
2016-02-22 12:55:18 +00:00
|
|
|
def get_template_by_id_and_service_id(service_id, template_id):
|
|
|
|
|
fetched_template = dao_get_template_by_id_and_service_id(template_id=template_id, service_id=service_id)
|
2016-06-14 15:07:23 +01:00
|
|
|
data = template_schema.dump(fetched_template).data
|
2016-03-11 12:39:55 +00:00
|
|
|
return jsonify(data=data)
|
2016-03-04 07:03:15 +00:00
|
|
|
|
|
|
|
|
|
2017-03-16 18:15:49 +00:00
|
|
|
@template_blueprint.route('/<uuid:template_id>/preview', methods=['GET'])
|
2016-06-16 13:51:20 +01:00
|
|
|
def preview_template_by_id_and_service_id(service_id, template_id):
|
|
|
|
|
fetched_template = dao_get_template_by_id_and_service_id(template_id=template_id, service_id=service_id)
|
|
|
|
|
data = template_schema.dump(fetched_template).data
|
2020-04-06 14:25:43 +01:00
|
|
|
template_object = fetched_template._as_utils_template_with_personalisation(
|
|
|
|
|
request.args.to_dict()
|
|
|
|
|
)
|
2016-06-16 13:51:20 +01:00
|
|
|
|
|
|
|
|
if template_object.missing_data:
|
|
|
|
|
raise InvalidRequest(
|
|
|
|
|
{'template': [
|
|
|
|
|
'Missing personalisation: {}'.format(", ".join(template_object.missing_data))
|
|
|
|
|
]}, status_code=400
|
|
|
|
|
)
|
|
|
|
|
|
2020-04-06 14:25:43 +01:00
|
|
|
data['subject'] = template_object.subject
|
2020-04-13 13:48:23 +01:00
|
|
|
data['content'] = template_object.content_with_placeholders_filled_in
|
2016-06-16 13:51:20 +01:00
|
|
|
|
2016-06-17 12:57:43 +01:00
|
|
|
return jsonify(data)
|
2016-06-16 13:51:20 +01:00
|
|
|
|
|
|
|
|
|
2017-03-16 18:15:49 +00:00
|
|
|
@template_blueprint.route('/<uuid:template_id>/version/<int:version>')
|
2016-05-06 15:42:43 +01:00
|
|
|
def get_template_version(service_id, template_id, version):
|
2016-06-14 15:07:23 +01:00
|
|
|
data = template_history_schema.dump(
|
2016-05-10 14:55:59 +01:00
|
|
|
dao_get_template_by_id_and_service_id(
|
|
|
|
|
template_id=template_id,
|
|
|
|
|
service_id=service_id,
|
|
|
|
|
version=version
|
|
|
|
|
)
|
2016-06-14 15:07:23 +01:00
|
|
|
).data
|
2016-05-09 15:59:34 +01:00
|
|
|
return jsonify(data=data)
|
|
|
|
|
|
|
|
|
|
|
2017-03-16 18:15:49 +00:00
|
|
|
@template_blueprint.route('/<uuid:template_id>/versions')
|
2016-05-09 15:59:34 +01:00
|
|
|
def get_template_versions(service_id, template_id):
|
2016-06-14 15:07:23 +01:00
|
|
|
data = template_history_schema.dump(
|
2016-05-13 16:25:05 +01:00
|
|
|
dao_get_template_versions(service_id=service_id, template_id=template_id),
|
2016-05-10 14:55:59 +01:00
|
|
|
many=True
|
2016-06-14 15:07:23 +01:00
|
|
|
).data
|
2016-05-06 15:42:43 +01:00
|
|
|
return jsonify(data=data)
|
|
|
|
|
|
|
|
|
|
|
2016-06-01 12:19:59 +01:00
|
|
|
def _template_has_not_changed(current_data, updated_template):
|
2016-06-01 13:55:04 +01:00
|
|
|
return all(
|
|
|
|
|
current_data[key] == updated_template[key]
|
2018-12-18 18:21:03 +00:00
|
|
|
for key in ('name', 'content', 'subject', 'archived', 'process_type', 'postage')
|
2016-06-01 13:55:04 +01:00
|
|
|
)
|
2017-06-28 17:03:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def redact_template(template, data):
|
|
|
|
|
# we also don't need to check what was passed in redact_personalisation - its presence in the dict is enough.
|
2017-06-29 12:39:02 +01:00
|
|
|
if 'created_by' not in data:
|
2017-06-28 17:03:12 +01:00
|
|
|
message = 'Field is required'
|
2017-06-29 12:39:02 +01:00
|
|
|
errors = {'created_by': [message]}
|
2017-06-28 17:03:12 +01:00
|
|
|
raise InvalidRequest(errors, status_code=400)
|
|
|
|
|
|
|
|
|
|
# if it's already redacted, then just return 200 straight away.
|
|
|
|
|
if not template.redact_personalisation:
|
2017-06-29 12:39:02 +01:00
|
|
|
dao_redact_template(template, data['created_by'])
|
2017-06-28 17:03:12 +01:00
|
|
|
return 'null', 200
|
2018-02-26 13:57:41 +00:00
|
|
|
|
|
|
|
|
|
2018-02-27 15:57:37 +00:00
|
|
|
@template_blueprint.route('/preview/<uuid:notification_id>/<file_type>', methods=['GET'])
|
|
|
|
|
def preview_letter_template_by_notification_id(service_id, notification_id, file_type):
|
2018-02-26 13:57:41 +00:00
|
|
|
if file_type not in ('pdf', 'png'):
|
2018-02-27 15:57:37 +00:00
|
|
|
raise InvalidRequest({'content': ["file_type must be pdf or png"]}, status_code=400)
|
2018-02-26 13:57:41 +00:00
|
|
|
|
|
|
|
|
page = request.args.get('page')
|
|
|
|
|
|
2018-02-27 15:57:37 +00:00
|
|
|
notification = get_notification_by_id(notification_id)
|
|
|
|
|
|
|
|
|
|
template = dao_get_template_by_id(notification.template_id)
|
2019-10-29 16:19:50 +00:00
|
|
|
metadata = {}
|
2018-02-26 13:57:41 +00:00
|
|
|
|
2018-03-07 15:42:59 +00:00
|
|
|
if template.is_precompiled_letter:
|
2018-03-05 14:11:37 +00:00
|
|
|
try:
|
2018-02-26 13:57:41 +00:00
|
|
|
|
2019-10-29 16:19:50 +00:00
|
|
|
pdf_file, metadata = get_letter_pdf_and_metadata(notification)
|
2018-03-05 14:11:37 +00:00
|
|
|
|
2018-03-12 11:05:05 +00:00
|
|
|
except botocore.exceptions.ClientError as e:
|
|
|
|
|
raise InvalidRequest(
|
|
|
|
|
'Error extracting requested page from PDF file for notification_id {} type {} {}'.format(
|
|
|
|
|
notification_id, type(e), e),
|
|
|
|
|
status_code=500
|
|
|
|
|
)
|
2018-03-02 14:54:28 +00:00
|
|
|
|
2019-04-10 16:08:08 +01:00
|
|
|
page_number = page if page else "1"
|
2020-03-09 13:43:06 +00:00
|
|
|
content = base64.b64encode(pdf_file).decode('utf-8')
|
|
|
|
|
content_outside_printable_area = metadata.get("message") == "content-outside-printable-area"
|
2020-03-10 13:38:20 +00:00
|
|
|
page_is_in_invalid_pages = page_number in metadata.get('invalid_pages', '[]')
|
2020-03-09 13:43:06 +00:00
|
|
|
|
2020-03-10 13:38:20 +00:00
|
|
|
if content_outside_printable_area and (file_type == "pdf" or page_is_in_invalid_pages):
|
2019-04-10 16:08:08 +01:00
|
|
|
path = '/precompiled/overlay.{}'.format(file_type)
|
2019-04-24 18:29:01 +01:00
|
|
|
query_string = '?page_number={}'.format(page_number) if file_type == 'png' else ''
|
2019-04-10 16:08:08 +01:00
|
|
|
content = pdf_file
|
|
|
|
|
elif file_type == 'png':
|
|
|
|
|
query_string = '?hide_notify=true' if page_number == '1' else ''
|
2019-04-24 18:29:01 +01:00
|
|
|
path = '/precompiled-preview.png'
|
2019-04-10 16:08:08 +01:00
|
|
|
else:
|
|
|
|
|
path = None
|
2018-03-02 14:54:28 +00:00
|
|
|
|
|
|
|
|
if file_type == 'png':
|
2018-03-09 15:50:43 +00:00
|
|
|
try:
|
|
|
|
|
pdf_page = extract_page_from_pdf(BytesIO(pdf_file), int(page_number) - 1)
|
2020-03-11 14:10:48 +00:00
|
|
|
if content_outside_printable_area and page_is_in_invalid_pages:
|
|
|
|
|
content = pdf_page
|
|
|
|
|
else:
|
|
|
|
|
content = base64.b64encode(pdf_page).decode('utf-8')
|
2018-03-12 11:05:05 +00:00
|
|
|
except PdfReadError as e:
|
2018-03-09 15:50:43 +00:00
|
|
|
raise InvalidRequest(
|
2018-03-12 11:05:05 +00:00
|
|
|
'Error extracting requested page from PDF file for notification_id {} type {} {}'.format(
|
|
|
|
|
notification_id, type(e), e),
|
2018-03-09 15:50:43 +00:00
|
|
|
status_code=500
|
|
|
|
|
)
|
|
|
|
|
|
2019-04-10 16:08:08 +01:00
|
|
|
if path:
|
2019-04-24 18:29:01 +01:00
|
|
|
url = current_app.config['TEMPLATE_PREVIEW_API_HOST'] + path + query_string
|
2019-04-10 16:08:08 +01:00
|
|
|
response_content = _get_png_preview_or_overlaid_pdf(url, content, notification.id, json=False)
|
|
|
|
|
else:
|
|
|
|
|
response_content = content
|
2018-03-02 14:54:28 +00:00
|
|
|
else:
|
|
|
|
|
|
|
|
|
|
template_for_letter_print = {
|
|
|
|
|
"id": str(notification.template_id),
|
|
|
|
|
"subject": template.subject,
|
|
|
|
|
"content": template.content,
|
2020-04-20 15:22:49 +01:00
|
|
|
"version": str(template.version),
|
|
|
|
|
"template_type": template.template_type
|
2018-03-02 14:54:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
service = dao_fetch_service_by_id(service_id)
|
2019-02-12 15:47:50 +00:00
|
|
|
letter_logo_filename = service.letter_branding and service.letter_branding.filename
|
2018-03-02 14:54:28 +00:00
|
|
|
data = {
|
|
|
|
|
'letter_contact_block': notification.reply_to_text,
|
|
|
|
|
'template': template_for_letter_print,
|
|
|
|
|
'values': notification.personalisation,
|
2018-04-30 15:47:49 +01:00
|
|
|
'date': notification.created_at.isoformat(),
|
2019-01-23 12:51:09 +00:00
|
|
|
'filename': letter_logo_filename,
|
2018-03-02 14:54:28 +00:00
|
|
|
}
|
|
|
|
|
|
2018-03-05 14:54:18 +00:00
|
|
|
url = '{}/preview.{}{}'.format(
|
|
|
|
|
current_app.config['TEMPLATE_PREVIEW_API_HOST'],
|
|
|
|
|
file_type,
|
|
|
|
|
'?page={}'.format(page) if page else ''
|
2018-02-27 15:57:37 +00:00
|
|
|
)
|
2019-04-10 16:08:08 +01:00
|
|
|
response_content = _get_png_preview_or_overlaid_pdf(url, data, notification.id, json=True)
|
2018-03-02 14:54:28 +00:00
|
|
|
|
2019-10-29 16:19:50 +00:00
|
|
|
return jsonify({"content": response_content, "metadata": metadata})
|
2018-03-05 14:54:18 +00:00
|
|
|
|
|
|
|
|
|
2019-04-04 18:38:31 +01:00
|
|
|
def _get_png_preview_or_overlaid_pdf(url, data, notification_id, json=True):
|
2018-03-06 15:35:00 +00:00
|
|
|
if json:
|
|
|
|
|
resp = requests_post(
|
|
|
|
|
url,
|
|
|
|
|
json=data,
|
|
|
|
|
headers={'Authorization': 'Token {}'.format(current_app.config['TEMPLATE_PREVIEW_API_KEY'])}
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
resp = requests_post(
|
|
|
|
|
url,
|
|
|
|
|
data=data,
|
|
|
|
|
headers={'Authorization': 'Token {}'.format(current_app.config['TEMPLATE_PREVIEW_API_KEY'])}
|
|
|
|
|
)
|
2018-03-05 14:54:18 +00:00
|
|
|
|
|
|
|
|
if resp.status_code != 200:
|
|
|
|
|
raise InvalidRequest(
|
2018-03-12 11:05:05 +00:00
|
|
|
'Error generating preview letter for {} Status code: {} {}'.format(
|
2018-03-06 14:24:30 +00:00
|
|
|
notification_id,
|
|
|
|
|
resp.status_code,
|
|
|
|
|
resp.content
|
|
|
|
|
), status_code=500
|
2018-03-05 14:54:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return base64.b64encode(resp.content).decode('utf-8')
|