mirror of
https://github.com/GSA/notifications-api.git
synced 2026-09-05 15:08:25 -04:00
Add task to resanitise and replace a PDF for precompiled letter
This adds a task which is designed to be used if we want to recreate the PDF for a precompiled letter (either one that has been created using the API or one that has been uploaded through the website). The task takes the `notification_id` of the letter and passes template preview the details it needs in order to sanitise the original file and then replace the version in the letters-pdf bucket with the freshly sanitised version.
This commit is contained in:
@@ -31,6 +31,7 @@ from app.letters.utils import (
|
|||||||
generate_letter_pdf_filename,
|
generate_letter_pdf_filename,
|
||||||
get_billable_units_for_letter_page_count,
|
get_billable_units_for_letter_page_count,
|
||||||
get_file_names_from_error_bucket,
|
get_file_names_from_error_bucket,
|
||||||
|
get_folder_name,
|
||||||
get_reference_from_filename,
|
get_reference_from_filename,
|
||||||
move_error_pdf_to_scan_bucket,
|
move_error_pdf_to_scan_bucket,
|
||||||
move_failed_pdf,
|
move_failed_pdf,
|
||||||
@@ -524,3 +525,37 @@ def replay_letters_in_error(filename=None):
|
|||||||
[filename],
|
[filename],
|
||||||
queue=QueueNames.LETTERS
|
queue=QueueNames.LETTERS
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@notify_celery.task(name='resanitise-pdf')
|
||||||
|
def resanitise_pdf(notification_id):
|
||||||
|
"""
|
||||||
|
`notification_id` is the notification id for a PDF letter which was either uploaded or sent using the API.
|
||||||
|
|
||||||
|
This task calls the `recreate_pdf_for_precompiled_letter` template preview task which recreates the
|
||||||
|
PDF for a letter which is already sanitised and in the letters-pdf bucket. The new file that is generated
|
||||||
|
will then overwrite the existing letter in the letters-pdf bucket.
|
||||||
|
"""
|
||||||
|
notification = get_notification_by_id(notification_id)
|
||||||
|
|
||||||
|
# folder_name is the folder that the letter is in the letters-pdf bucket e.g. '2021-10-10/'
|
||||||
|
folder_name = get_folder_name(notification.created_at)
|
||||||
|
|
||||||
|
filename = generate_letter_pdf_filename(
|
||||||
|
reference=notification.reference,
|
||||||
|
created_at=notification.created_at,
|
||||||
|
ignore_folder=True,
|
||||||
|
postage=notification.postage
|
||||||
|
)
|
||||||
|
|
||||||
|
notify_celery.send_task(
|
||||||
|
name=TaskNames.RECREATE_PDF_FOR_PRECOMPILED_LETTER,
|
||||||
|
kwargs={
|
||||||
|
'notification_id': str(notification.id),
|
||||||
|
'file_location': f'{folder_name}{filename}',
|
||||||
|
'allow_international_letters': notification.service.has_permission(
|
||||||
|
INTERNATIONAL_LETTERS
|
||||||
|
),
|
||||||
|
},
|
||||||
|
queue=QueueNames.SANITISE_LETTERS,
|
||||||
|
)
|
||||||
|
|||||||
@@ -77,6 +77,7 @@ class TaskNames(object):
|
|||||||
SANITISE_LETTER = 'sanitise-and-upload-letter'
|
SANITISE_LETTER = 'sanitise-and-upload-letter'
|
||||||
CREATE_PDF_FOR_TEMPLATED_LETTER = 'create-pdf-for-templated-letter'
|
CREATE_PDF_FOR_TEMPLATED_LETTER = 'create-pdf-for-templated-letter'
|
||||||
PUBLISH_GOVUK_ALERTS = 'publish-govuk-alerts'
|
PUBLISH_GOVUK_ALERTS = 'publish-govuk-alerts'
|
||||||
|
RECREATE_PDF_FOR_PRECOMPILED_LETTER = 'recreate-pdf-for-precompiled-letter'
|
||||||
|
|
||||||
|
|
||||||
class Config(object):
|
class Config(object):
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ from app.celery.letters_pdf_tasks import (
|
|||||||
process_virus_scan_error,
|
process_virus_scan_error,
|
||||||
process_virus_scan_failed,
|
process_virus_scan_failed,
|
||||||
replay_letters_in_error,
|
replay_letters_in_error,
|
||||||
|
resanitise_pdf,
|
||||||
sanitise_letter,
|
sanitise_letter,
|
||||||
send_letters_volume_email_to_dvla,
|
send_letters_volume_email_to_dvla,
|
||||||
update_billable_units_for_letter,
|
update_billable_units_for_letter,
|
||||||
@@ -1098,3 +1099,33 @@ def test_replay_letters_in_error_for_one_file(notify_api, mocker):
|
|||||||
replay_letters_in_error("file_name")
|
replay_letters_in_error("file_name")
|
||||||
mock_move.assert_called_once_with('file_name')
|
mock_move.assert_called_once_with('file_name')
|
||||||
mock_celery.assert_called_once_with(name='scan-file', kwargs={'filename': 'file_name'}, queue='antivirus-tasks')
|
mock_celery.assert_called_once_with(name='scan-file', kwargs={'filename': 'file_name'}, queue='antivirus-tasks')
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('permissions, expected_international_letters_allowed', (
|
||||||
|
([LETTER_TYPE], False),
|
||||||
|
([LETTER_TYPE, INTERNATIONAL_LETTERS], True),
|
||||||
|
))
|
||||||
|
def test_resanitise_pdf_calls_template_preview_with_letter_details(
|
||||||
|
mocker,
|
||||||
|
sample_letter_notification,
|
||||||
|
permissions,
|
||||||
|
expected_international_letters_allowed,
|
||||||
|
):
|
||||||
|
mock_celery = mocker.patch('app.celery.letters_pdf_tasks.notify_celery.send_task')
|
||||||
|
|
||||||
|
sample_letter_notification.created_at = datetime(2021, 2, 7, 12)
|
||||||
|
sample_letter_notification.service = create_service(
|
||||||
|
service_permissions=permissions
|
||||||
|
)
|
||||||
|
|
||||||
|
resanitise_pdf(sample_letter_notification.id)
|
||||||
|
|
||||||
|
mock_celery.assert_called_once_with(
|
||||||
|
name=TaskNames.RECREATE_PDF_FOR_PRECOMPILED_LETTER,
|
||||||
|
kwargs={
|
||||||
|
'notification_id': str(sample_letter_notification.id),
|
||||||
|
'file_location': '2021-02-07/NOTIFY.FOO.D.2.C.20210207120000.PDF',
|
||||||
|
'allow_international_letters': expected_international_letters_allowed,
|
||||||
|
},
|
||||||
|
queue=QueueNames.SANITISE_LETTERS,
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user