mirror of
https://github.com/GSA/notifications-api.git
synced 2026-05-05 08:40:29 -04:00
Merge pull request #2647 from alphagov/tell-users-why-letter-failed-validation
Grab metadata when getting pdf letter preview from S3
This commit is contained in:
@@ -164,7 +164,7 @@ def get_file_names_from_error_bucket():
|
||||
return bucket.objects.filter(Prefix="ERROR")
|
||||
|
||||
|
||||
def get_letter_pdf(notification):
|
||||
def get_letter_pdf_and_metadata(notification):
|
||||
bucket_name, prefix = get_bucket_name_and_prefix_for_notification(notification)
|
||||
|
||||
s3 = boto3.resource('s3')
|
||||
@@ -174,8 +174,8 @@ def get_letter_pdf(notification):
|
||||
obj = s3.Object(
|
||||
bucket_name=bucket_name,
|
||||
key=item.key
|
||||
)
|
||||
return obj.get()["Body"].read()
|
||||
).get()
|
||||
return obj["Body"].read(), obj["Metadata"]
|
||||
|
||||
|
||||
def _move_s3_object(source_bucket, source_filename, target_bucket, target_filename, metadata=None):
|
||||
|
||||
@@ -32,7 +32,7 @@ from app.errors import (
|
||||
register_errors,
|
||||
InvalidRequest
|
||||
)
|
||||
from app.letters.utils import get_letter_pdf
|
||||
from app.letters.utils import get_letter_pdf_and_metadata
|
||||
from app.models import SMS_TYPE, Template, SECOND_CLASS, LETTER_TYPE
|
||||
from app.notifications.validators import service_has_permission, check_reply_to
|
||||
from app.schema_validation import validate
|
||||
@@ -231,11 +231,12 @@ def preview_letter_template_by_notification_id(service_id, notification_id, file
|
||||
notification = get_notification_by_id(notification_id)
|
||||
|
||||
template = dao_get_template_by_id(notification.template_id)
|
||||
metadata = {}
|
||||
|
||||
if template.is_precompiled_letter:
|
||||
try:
|
||||
|
||||
pdf_file = get_letter_pdf(notification)
|
||||
pdf_file, metadata = get_letter_pdf_and_metadata(notification)
|
||||
|
||||
except botocore.exceptions.ClientError as e:
|
||||
raise InvalidRequest(
|
||||
@@ -245,7 +246,7 @@ def preview_letter_template_by_notification_id(service_id, notification_id, file
|
||||
)
|
||||
|
||||
content = base64.b64encode(pdf_file).decode('utf-8')
|
||||
overlay = request.args.get('overlay')
|
||||
overlay = metadata.get("message") == "content-outside-printable-area"
|
||||
page_number = page if page else "1"
|
||||
|
||||
if overlay:
|
||||
@@ -300,7 +301,7 @@ def preview_letter_template_by_notification_id(service_id, notification_id, file
|
||||
)
|
||||
response_content = _get_png_preview_or_overlaid_pdf(url, data, notification.id, json=True)
|
||||
|
||||
return jsonify({"content": response_content})
|
||||
return jsonify({"content": response_content, "metadata": metadata})
|
||||
|
||||
|
||||
def _get_png_preview_or_overlaid_pdf(url, data, notification_id, json=True):
|
||||
|
||||
@@ -4,7 +4,7 @@ from flask import jsonify, request, url_for, current_app, send_file
|
||||
|
||||
from app import api_user, authenticated_service
|
||||
from app.dao import notifications_dao
|
||||
from app.letters.utils import get_letter_pdf
|
||||
from app.letters.utils import get_letter_pdf_and_metadata
|
||||
from app.schema_validation import validate
|
||||
from app.v2.errors import BadRequestError, PDFNotReadyError
|
||||
from app.v2.notifications import v2_notification_blueprint
|
||||
@@ -48,7 +48,7 @@ def get_pdf_for_notification(notification_id):
|
||||
raise PDFNotReadyError()
|
||||
|
||||
try:
|
||||
pdf_data = get_letter_pdf(notification)
|
||||
pdf_data, metadata = get_letter_pdf_and_metadata(notification)
|
||||
except Exception:
|
||||
raise PDFNotReadyError()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user