When requesting a png for a single page, show overlay as appropriate

If the png page is invalid then show the overlay for that page
If the png page is not invalid, even if other pages are invalid, then
don't shown the overlay for that page

These keeps the behaviour were if you get the pdf for a pdf which has
invalid pages then the whole PDF is requested (not just a single page)
and all of the pages include the overlay

Tests have been improved to be more explicit about what we are testing
This commit is contained in:
David McDonald
2020-03-09 13:43:06 +00:00
parent 9eda30f4fd
commit d9243b8b8a
2 changed files with 88 additions and 13 deletions

View File

@@ -245,11 +245,15 @@ def preview_letter_template_by_notification_id(service_id, notification_id, file
status_code=500
)
content = base64.b64encode(pdf_file).decode('utf-8')
overlay = metadata.get("message") == "content-outside-printable-area"
page_number = page if page else "1"
content = base64.b64encode(pdf_file).decode('utf-8')
content_outside_printable_area = metadata.get("message") == "content-outside-printable-area"
if overlay:
show_overlay_for_page = False
if content_outside_printable_area and page_number in metadata.get('invalid_pages', '[]'):
show_overlay_for_page = True
if show_overlay_for_page or (content_outside_printable_area and file_type == "pdf"):
path = '/precompiled/overlay.{}'.format(file_type)
query_string = '?page_number={}'.format(page_number) if file_type == 'png' else ''
content = pdf_file
@@ -262,7 +266,7 @@ def preview_letter_template_by_notification_id(service_id, notification_id, file
if file_type == 'png':
try:
pdf_page = extract_page_from_pdf(BytesIO(pdf_file), int(page_number) - 1)
content = pdf_page if overlay else base64.b64encode(pdf_page).decode('utf-8')
content = pdf_page if show_overlay_for_page else 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(