mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-19 05:58:53 -04:00
Updated API to handle pre-compiled pdfs
* added a method to letter/utils.py to get the PDF document from the S3 bucket * added the logic to return the pdf or to produce a png of the pdf
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
|
import boto3
|
||||||
from flask import current_app
|
from flask import current_app
|
||||||
|
|
||||||
from notifications_utils.s3 import s3upload
|
from notifications_utils.s3 import s3upload
|
||||||
@@ -10,6 +11,8 @@ from app.variables import Retention
|
|||||||
LETTERS_PDF_FILE_LOCATION_STRUCTURE = \
|
LETTERS_PDF_FILE_LOCATION_STRUCTURE = \
|
||||||
'{folder}/NOTIFY.{reference}.{duplex}.{letter_class}.{colour}.{crown}.{date}.pdf'
|
'{folder}/NOTIFY.{reference}.{duplex}.{letter_class}.{colour}.{crown}.{date}.pdf'
|
||||||
|
|
||||||
|
PRECOMPILED_BUCKET_PREFIX = '{folder}/NOTIFY.{reference}'
|
||||||
|
|
||||||
|
|
||||||
def get_letter_pdf_filename(reference, crown):
|
def get_letter_pdf_filename(reference, crown):
|
||||||
now = datetime.utcnow()
|
now = datetime.utcnow()
|
||||||
@@ -31,6 +34,15 @@ def get_letter_pdf_filename(reference, crown):
|
|||||||
return upload_file_name
|
return upload_file_name
|
||||||
|
|
||||||
|
|
||||||
|
def get_bucket_prefix_for_notification(notification):
|
||||||
|
upload_file_name = PRECOMPILED_BUCKET_PREFIX.format(
|
||||||
|
folder=notification.created_at.date(),
|
||||||
|
reference=notification.reference
|
||||||
|
).upper()
|
||||||
|
|
||||||
|
return upload_file_name
|
||||||
|
|
||||||
|
|
||||||
def upload_letter_pdf(notification, pdf_data):
|
def upload_letter_pdf(notification, pdf_data):
|
||||||
current_app.logger.info("PDF Letter {} reference {} created at {}, {} bytes".format(
|
current_app.logger.info("PDF Letter {} reference {} created at {}, {} bytes".format(
|
||||||
notification.id, notification.reference, notification.created_at, len(pdf_data)))
|
notification.id, notification.reference, notification.created_at, len(pdf_data)))
|
||||||
@@ -48,3 +60,19 @@ def upload_letter_pdf(notification, pdf_data):
|
|||||||
|
|
||||||
current_app.logger.info("Uploaded letters PDF {} to {} for notification id {}".format(
|
current_app.logger.info("Uploaded letters PDF {} to {} for notification id {}".format(
|
||||||
upload_file_name, current_app.config['LETTERS_PDF_BUCKET_NAME'], notification.id))
|
upload_file_name, current_app.config['LETTERS_PDF_BUCKET_NAME'], notification.id))
|
||||||
|
|
||||||
|
|
||||||
|
def get_letter_pdf(notification):
|
||||||
|
bucket_name = current_app.config['LETTERS_PDF_BUCKET_NAME']
|
||||||
|
|
||||||
|
s3 = boto3.resource('s3')
|
||||||
|
bucket = s3.Bucket(bucket_name)
|
||||||
|
|
||||||
|
for item in bucket.objects.filter(Prefix=get_bucket_prefix_for_notification(notification)):
|
||||||
|
obj = s3.Object(
|
||||||
|
bucket_name=bucket_name,
|
||||||
|
key=item.key
|
||||||
|
)
|
||||||
|
file_content = obj.get()["Body"].read()
|
||||||
|
|
||||||
|
return file_content
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ from app.dao.templates_dao import (
|
|||||||
dao_get_template_by_id)
|
dao_get_template_by_id)
|
||||||
from notifications_utils.template import SMSMessageTemplate
|
from notifications_utils.template import SMSMessageTemplate
|
||||||
from app.dao.services_dao import dao_fetch_service_by_id
|
from app.dao.services_dao import dao_fetch_service_by_id
|
||||||
|
from app.letters.utils import get_letter_pdf
|
||||||
from app.models import SMS_TYPE
|
from app.models import SMS_TYPE
|
||||||
from app.notifications.validators import service_has_permission, check_reply_to
|
from app.notifications.validators import service_has_permission, check_reply_to
|
||||||
from app.schemas import (template_schema, template_history_schema)
|
from app.schemas import (template_schema, template_history_schema)
|
||||||
@@ -185,6 +186,7 @@ def redact_template(template, data):
|
|||||||
|
|
||||||
@template_blueprint.route('/preview/<uuid:notification_id>/<file_type>', methods=['GET'])
|
@template_blueprint.route('/preview/<uuid:notification_id>/<file_type>', methods=['GET'])
|
||||||
def preview_letter_template_by_notification_id(service_id, notification_id, file_type):
|
def preview_letter_template_by_notification_id(service_id, notification_id, file_type):
|
||||||
|
|
||||||
if file_type not in ('pdf', 'png'):
|
if file_type not in ('pdf', 'png'):
|
||||||
raise InvalidRequest({'content': ["file_type must be pdf or png"]}, status_code=400)
|
raise InvalidRequest({'content': ["file_type must be pdf or png"]}, status_code=400)
|
||||||
|
|
||||||
@@ -194,36 +196,65 @@ def preview_letter_template_by_notification_id(service_id, notification_id, file
|
|||||||
|
|
||||||
template = dao_get_template_by_id(notification.template_id)
|
template = dao_get_template_by_id(notification.template_id)
|
||||||
|
|
||||||
template_for_letter_print = {
|
if template.hidden and template.name == 'Pre-compiled PDF':
|
||||||
"id": str(notification.template_id),
|
|
||||||
"subject": template.subject,
|
|
||||||
"content": template.content,
|
|
||||||
"version": str(template.version)
|
|
||||||
}
|
|
||||||
|
|
||||||
service = dao_fetch_service_by_id(service_id)
|
pdf_file = get_letter_pdf(notification)
|
||||||
|
|
||||||
data = {
|
content = base64.b64encode(pdf_file).decode('utf-8')
|
||||||
'letter_contact_block': notification.reply_to_text,
|
|
||||||
'template': template_for_letter_print,
|
|
||||||
'values': notification.personalisation,
|
|
||||||
'dvla_org_id': service.dvla_organisation_id,
|
|
||||||
}
|
|
||||||
|
|
||||||
resp = requests_post(
|
if file_type == 'png':
|
||||||
'{}/preview.{}{}'.format(
|
|
||||||
current_app.config['TEMPLATE_PREVIEW_API_HOST'],
|
|
||||||
file_type,
|
|
||||||
'?page={}'.format(page) if page else ''
|
|
||||||
),
|
|
||||||
json=data,
|
|
||||||
headers={'Authorization': 'Token {}'.format(current_app.config['TEMPLATE_PREVIEW_API_KEY'])}
|
|
||||||
)
|
|
||||||
|
|
||||||
if resp.status_code != 200:
|
url = '{}//precompiled-preview.png{}'.format(
|
||||||
raise InvalidRequest(
|
current_app.config['TEMPLATE_PREVIEW_API_HOST'],
|
||||||
'Error generating preview for {}'.format(notification_id), status_code=500
|
'?page={}'.format(page) if page else ''
|
||||||
|
)
|
||||||
|
|
||||||
|
resp = requests_post(
|
||||||
|
url,
|
||||||
|
data=content,
|
||||||
|
headers={'Authorization': 'Token {}'.format(current_app.config['TEMPLATE_PREVIEW_API_KEY'])}
|
||||||
|
)
|
||||||
|
|
||||||
|
if resp.status_code != 200:
|
||||||
|
raise InvalidRequest(
|
||||||
|
'Error generating preview for {}'.format(notification_id), status_code=500
|
||||||
|
)
|
||||||
|
|
||||||
|
content = base64.b64encode(resp.content).decode('utf-8')
|
||||||
|
|
||||||
|
else:
|
||||||
|
|
||||||
|
template_for_letter_print = {
|
||||||
|
"id": str(notification.template_id),
|
||||||
|
"subject": template.subject,
|
||||||
|
"content": template.content,
|
||||||
|
"version": str(template.version)
|
||||||
|
}
|
||||||
|
|
||||||
|
service = dao_fetch_service_by_id(service_id)
|
||||||
|
|
||||||
|
data = {
|
||||||
|
'letter_contact_block': notification.reply_to_text,
|
||||||
|
'template': template_for_letter_print,
|
||||||
|
'values': notification.personalisation,
|
||||||
|
'dvla_org_id': service.dvla_organisation_id,
|
||||||
|
}
|
||||||
|
|
||||||
|
resp = requests_post(
|
||||||
|
'{}/preview.{}{}'.format(
|
||||||
|
current_app.config['TEMPLATE_PREVIEW_API_HOST'],
|
||||||
|
file_type,
|
||||||
|
'?page={}'.format(page) if page else ''
|
||||||
|
),
|
||||||
|
json=data,
|
||||||
|
headers={'Authorization': 'Token {}'.format(current_app.config['TEMPLATE_PREVIEW_API_KEY'])}
|
||||||
)
|
)
|
||||||
|
|
||||||
content = base64.b64encode(resp.content).decode('utf-8')
|
if resp.status_code != 200:
|
||||||
|
raise InvalidRequest(
|
||||||
|
'Error generating preview for {}'.format(notification_id), status_code=500
|
||||||
|
)
|
||||||
|
|
||||||
|
content = base64.b64encode(resp.content).decode('utf-8')
|
||||||
|
|
||||||
return jsonify({"content": content})
|
return jsonify({"content": content})
|
||||||
|
|||||||
Reference in New Issue
Block a user