Merge pull request #1756 from alphagov/rc_send_template_preview_one_page

Updated the notification template endpoint to extract the pdf page
This commit is contained in:
Richard Chapman
2018-03-12 14:46:38 +00:00
committed by GitHub
3 changed files with 92 additions and 23 deletions

View File

@@ -1,14 +1,19 @@
import base64
from io import BytesIO
import botocore
from PyPDF2.utils import PdfReadError
from flask import (
Blueprint,
current_app,
jsonify,
request)
from notifications_utils.pdf import extract_page_from_pdf
from notifications_utils.template import SMSMessageTemplate
from requests import post as requests_post
from app.dao.notifications_dao import get_notification_by_id
from app.dao.services_dao import dao_fetch_service_by_id
from app.dao.templates_dao import (
dao_update_template,
dao_create_template,
@@ -18,16 +23,14 @@ from app.dao.templates_dao import (
dao_get_template_versions,
dao_update_template_reply_to,
dao_get_template_by_id)
from notifications_utils.template import SMSMessageTemplate
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.notifications.validators import service_has_permission, check_reply_to
from app.schemas import (template_schema, template_history_schema)
from app.errors import (
register_errors,
InvalidRequest
)
from app.letters.utils import get_letter_pdf
from app.models import SMS_TYPE
from app.notifications.validators import service_has_permission, check_reply_to
from app.schemas import (template_schema, template_history_schema)
from app.utils import get_template_instance, get_public_notify_type_text
template_blueprint = Blueprint('template', __name__, url_prefix='/service/<uuid:service_id>/template')
@@ -202,18 +205,30 @@ def preview_letter_template_by_notification_id(service_id, notification_id, file
pdf_file = get_letter_pdf(notification)
except botocore.exceptions.ClientError:
current_app.logger.exception(
'Error getting letter file from S3 notification id {}'.format(notification_id))
raise InvalidRequest('Error getting letter file from S3 notification id {}'.format(notification_id),
status_code=500)
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
)
content = base64.b64encode(pdf_file).decode('utf-8')
if file_type == 'png':
url = '{}/precompiled-preview.png{}'.format(
current_app.config['TEMPLATE_PREVIEW_API_HOST'],
'?page={}'.format(page) if page else ''
try:
page_number = page if page else "0"
pdf_page = extract_page_from_pdf(BytesIO(pdf_file), int(page_number) - 1)
content = base64.b64encode(pdf_page).decode('utf-8')
except PdfReadError as e:
raise InvalidRequest(
'Error extracting requested page from PDF file for notification_id {} type {} {}'.format(
notification_id, type(e), e),
status_code=500
)
url = '{}/precompiled-preview.png'.format(
current_app.config['TEMPLATE_PREVIEW_API_HOST']
)
content = _get_png_preview(url, content, notification.id, json=False)
@@ -262,14 +277,8 @@ def _get_png_preview(url, data, notification_id, json=True):
)
if resp.status_code != 200:
current_app.logger.exception(
'Error generating preview letter for {} \nStatus code: {}\n{}'.format(
notification_id,
resp.status_code,
resp.content
))
raise InvalidRequest(
'Error generating preview letter for {}\nStatus code: {}\n{}'.format(
'Error generating preview letter for {} Status code: {} {}'.format(
notification_id,
resp.status_code,
resp.content