mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 23:55:58 -05:00
Merge pull request #1813 from alphagov/ken-hide-notify-tag-on-first-page
Hide notify tag on first page
This commit is contained in:
@@ -200,7 +200,6 @@ 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)
|
||||||
|
|
||||||
if template.is_precompiled_letter:
|
if template.is_precompiled_letter:
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
pdf_file = get_letter_pdf(notification)
|
pdf_file = get_letter_pdf(notification)
|
||||||
@@ -215,9 +214,9 @@ def preview_letter_template_by_notification_id(service_id, notification_id, file
|
|||||||
content = base64.b64encode(pdf_file).decode('utf-8')
|
content = base64.b64encode(pdf_file).decode('utf-8')
|
||||||
|
|
||||||
if file_type == 'png':
|
if file_type == 'png':
|
||||||
|
|
||||||
try:
|
try:
|
||||||
page_number = page if page else "0"
|
page_number = page if page else "1"
|
||||||
|
|
||||||
pdf_page = extract_page_from_pdf(BytesIO(pdf_file), int(page_number) - 1)
|
pdf_page = extract_page_from_pdf(BytesIO(pdf_file), int(page_number) - 1)
|
||||||
content = base64.b64encode(pdf_page).decode('utf-8')
|
content = base64.b64encode(pdf_page).decode('utf-8')
|
||||||
except PdfReadError as e:
|
except PdfReadError as e:
|
||||||
@@ -227,12 +226,12 @@ def preview_letter_template_by_notification_id(service_id, notification_id, file
|
|||||||
status_code=500
|
status_code=500
|
||||||
)
|
)
|
||||||
|
|
||||||
url = '{}/precompiled-preview.png'.format(
|
url = '{}/precompiled-preview.png{}'.format(
|
||||||
current_app.config['TEMPLATE_PREVIEW_API_HOST']
|
current_app.config['TEMPLATE_PREVIEW_API_HOST'],
|
||||||
|
'?hide_notify=true' if page_number == '1' else ''
|
||||||
)
|
)
|
||||||
|
|
||||||
content = _get_png_preview(url, content, notification.id, json=False)
|
content = _get_png_preview(url, content, notification.id, json=False)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
||||||
template_for_letter_print = {
|
template_for_letter_print = {
|
||||||
@@ -256,7 +255,6 @@ def preview_letter_template_by_notification_id(service_id, notification_id, file
|
|||||||
file_type,
|
file_type,
|
||||||
'?page={}'.format(page) if page else ''
|
'?page={}'.format(page) if page else ''
|
||||||
)
|
)
|
||||||
|
|
||||||
content = _get_png_preview(url, data, notification.id, json=True)
|
content = _get_png_preview(url, data, notification.id, json=True)
|
||||||
|
|
||||||
return jsonify({"content": content})
|
return jsonify({"content": content})
|
||||||
|
|||||||
@@ -169,37 +169,33 @@ def test_move_scanned_letter_pdf_to_processing_bucket(
|
|||||||
@freeze_time(FROZEN_DATE_TIME)
|
@freeze_time(FROZEN_DATE_TIME)
|
||||||
def test_move_failed_pdf_error(notify_api):
|
def test_move_failed_pdf_error(notify_api):
|
||||||
filename = 'test.pdf'
|
filename = 'test.pdf'
|
||||||
source_bucket_name = current_app.config['LETTERS_SCAN_BUCKET_NAME']
|
bucket_name = current_app.config['LETTERS_SCAN_BUCKET_NAME']
|
||||||
target_bucket_name = current_app.config['LETTERS_SCAN_BUCKET_NAME']
|
|
||||||
|
|
||||||
conn = boto3.resource('s3', region_name='eu-west-1')
|
conn = boto3.resource('s3', region_name='eu-west-1')
|
||||||
source_bucket = conn.create_bucket(Bucket=source_bucket_name)
|
bucket = conn.create_bucket(Bucket=bucket_name)
|
||||||
target_bucket = conn.create_bucket(Bucket=target_bucket_name)
|
|
||||||
|
|
||||||
s3 = boto3.client('s3', region_name='eu-west-1')
|
s3 = boto3.client('s3', region_name='eu-west-1')
|
||||||
s3.put_object(Bucket=source_bucket_name, Key=filename, Body=b'pdf_content')
|
s3.put_object(Bucket=bucket_name, Key=filename, Body=b'pdf_content')
|
||||||
|
|
||||||
move_failed_pdf(filename, ScanErrorType.ERROR)
|
move_failed_pdf(filename, ScanErrorType.ERROR)
|
||||||
|
|
||||||
assert 'ERROR/' + filename in [o.key for o in target_bucket.objects.all()]
|
assert 'ERROR/' + filename in [o.key for o in bucket.objects.all()]
|
||||||
assert filename not in [o.key for o in source_bucket.objects.all()]
|
assert filename not in [o.key for o in bucket.objects.all()]
|
||||||
|
|
||||||
|
|
||||||
@mock_s3
|
@mock_s3
|
||||||
@freeze_time(FROZEN_DATE_TIME)
|
@freeze_time(FROZEN_DATE_TIME)
|
||||||
def test_move_failed_pdf_scan_failed(notify_api):
|
def test_move_failed_pdf_scan_failed(notify_api):
|
||||||
filename = 'test.pdf'
|
filename = 'test.pdf'
|
||||||
source_bucket_name = current_app.config['LETTERS_SCAN_BUCKET_NAME']
|
bucket_name = current_app.config['LETTERS_SCAN_BUCKET_NAME']
|
||||||
target_bucket_name = current_app.config['LETTERS_SCAN_BUCKET_NAME']
|
|
||||||
|
|
||||||
conn = boto3.resource('s3', region_name='eu-west-1')
|
conn = boto3.resource('s3', region_name='eu-west-1')
|
||||||
source_bucket = conn.create_bucket(Bucket=source_bucket_name)
|
bucket = conn.create_bucket(Bucket=bucket_name)
|
||||||
target_bucket = conn.create_bucket(Bucket=target_bucket_name)
|
|
||||||
|
|
||||||
s3 = boto3.client('s3', region_name='eu-west-1')
|
s3 = boto3.client('s3', region_name='eu-west-1')
|
||||||
s3.put_object(Bucket=source_bucket_name, Key=filename, Body=b'pdf_content')
|
s3.put_object(Bucket=bucket_name, Key=filename, Body=b'pdf_content')
|
||||||
|
|
||||||
move_failed_pdf(filename, ScanErrorType.FAILURE)
|
move_failed_pdf(filename, ScanErrorType.FAILURE)
|
||||||
|
|
||||||
assert 'FAILURE/' + filename in [o.key for o in target_bucket.objects.all()]
|
assert 'FAILURE/' + filename in [o.key for o in bucket.objects.all()]
|
||||||
assert filename not in [o.key for o in source_bucket.objects.all()]
|
assert filename not in [o.key for o in bucket.objects.all()]
|
||||||
|
|||||||
@@ -1008,6 +1008,54 @@ def test_preview_letter_template_precompiled_png_file_type(
|
|||||||
assert base64.b64decode(resp['content']) == png_content
|
assert base64.b64decode(resp['content']) == png_content
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('page_number,expect_preview_url', [
|
||||||
|
('', 'http://localhost/notifications-template-preview/precompiled-preview.png?hide_notify=true'),
|
||||||
|
('1', 'http://localhost/notifications-template-preview/precompiled-preview.png?hide_notify=true'),
|
||||||
|
('2', 'http://localhost/notifications-template-preview/precompiled-preview.png')
|
||||||
|
])
|
||||||
|
def test_preview_letter_template_precompiled_png_file_type_hide_notify_tag_only_on_first_page(
|
||||||
|
notify_api,
|
||||||
|
client,
|
||||||
|
admin_request,
|
||||||
|
sample_service,
|
||||||
|
mocker,
|
||||||
|
page_number,
|
||||||
|
expect_preview_url
|
||||||
|
):
|
||||||
|
|
||||||
|
template = create_template(sample_service,
|
||||||
|
template_type='letter',
|
||||||
|
template_name='Pre-compiled PDF',
|
||||||
|
subject='Pre-compiled PDF',
|
||||||
|
hidden=True)
|
||||||
|
|
||||||
|
notification = create_notification(template)
|
||||||
|
|
||||||
|
with set_config_values(notify_api, {
|
||||||
|
'TEMPLATE_PREVIEW_API_HOST': 'http://localhost/notifications-template-preview',
|
||||||
|
'TEMPLATE_PREVIEW_API_KEY': 'test-key'
|
||||||
|
}):
|
||||||
|
pdf_content = b'\x00\x01'
|
||||||
|
png_content = b'\x00\x02'
|
||||||
|
encoded = base64.b64encode(png_content).decode('utf-8')
|
||||||
|
|
||||||
|
mocker.patch('app.template.rest.get_letter_pdf', return_value=pdf_content)
|
||||||
|
mocker.patch('app.template.rest.extract_page_from_pdf', return_value=png_content)
|
||||||
|
mock_get_png_preview = mocker.patch('app.template.rest._get_png_preview', return_value=encoded)
|
||||||
|
|
||||||
|
admin_request.get(
|
||||||
|
'template.preview_letter_template_by_notification_id',
|
||||||
|
service_id=notification.service_id,
|
||||||
|
notification_id=notification.id,
|
||||||
|
file_type='png',
|
||||||
|
page=page_number
|
||||||
|
)
|
||||||
|
|
||||||
|
mock_get_png_preview.assert_called_once_with(
|
||||||
|
expect_preview_url, encoded, notification.id, json=False
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_preview_letter_template_precompiled_png_template_preview_500_error(
|
def test_preview_letter_template_precompiled_png_template_preview_500_error(
|
||||||
notify_api,
|
notify_api,
|
||||||
client,
|
client,
|
||||||
|
|||||||
Reference in New Issue
Block a user