mirror of
https://github.com/GSA/notifications-api.git
synced 2026-07-31 19:58:46 -04:00
Merge pull request #2171 from alphagov/update-page-count-after-antivirus-scan
Update page count after antivirus scan
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
from uuid import UUID
|
||||
import io
|
||||
import math
|
||||
from datetime import datetime
|
||||
from uuid import UUID
|
||||
|
||||
from PyPDF2.utils import PdfReadError
|
||||
from botocore.exceptions import ClientError as BotoClientError
|
||||
from flask import current_app
|
||||
from notifications_utils.pdf import pdf_page_count
|
||||
from requests import (
|
||||
post as requests_post,
|
||||
RequestException
|
||||
@@ -38,9 +41,9 @@ from app.models import (
|
||||
KEY_TYPE_TEST,
|
||||
NOTIFICATION_CREATED,
|
||||
NOTIFICATION_DELIVERED,
|
||||
NOTIFICATION_VIRUS_SCAN_FAILED,
|
||||
NOTIFICATION_TECHNICAL_FAILURE,
|
||||
NOTIFICATION_VALIDATION_FAILED
|
||||
NOTIFICATION_VALIDATION_FAILED,
|
||||
NOTIFICATION_VIRUS_SCAN_FAILED,
|
||||
)
|
||||
|
||||
|
||||
@@ -185,6 +188,7 @@ def process_virus_scan_passed(self, filename):
|
||||
scan_pdf_object = s3.get_s3_object(current_app.config['LETTERS_SCAN_BUCKET_NAME'], filename)
|
||||
old_pdf = scan_pdf_object.get()['Body'].read()
|
||||
|
||||
billable_units = _get_page_count(notification, old_pdf)
|
||||
new_pdf = _sanitise_precompiled_pdf(self, notification, old_pdf)
|
||||
|
||||
# TODO: Remove this once CYSP update their template to not cross over the margins
|
||||
@@ -213,13 +217,30 @@ def process_virus_scan_passed(self, filename):
|
||||
is_test_letter=is_test_key)
|
||||
|
||||
update_letter_pdf_status(
|
||||
reference,
|
||||
NOTIFICATION_DELIVERED if is_test_key else NOTIFICATION_CREATED
|
||||
reference=reference,
|
||||
status=NOTIFICATION_DELIVERED if is_test_key else NOTIFICATION_CREATED,
|
||||
billable_units=billable_units
|
||||
)
|
||||
|
||||
scan_pdf_object.delete()
|
||||
|
||||
|
||||
def _get_page_count(notification, old_pdf):
|
||||
try:
|
||||
pages = pdf_page_count(io.BytesIO(old_pdf))
|
||||
pages_per_sheet = 2
|
||||
billable_units = math.ceil(pages / pages_per_sheet)
|
||||
return billable_units
|
||||
except PdfReadError as e:
|
||||
current_app.logger.exception(msg='Invalid PDF received for notification_id: {}'.format(notification.id))
|
||||
update_letter_pdf_status(
|
||||
reference=notification.reference,
|
||||
status=NOTIFICATION_VALIDATION_FAILED,
|
||||
billable_units=0
|
||||
)
|
||||
raise e
|
||||
|
||||
|
||||
def _upload_pdf_to_test_or_live_pdf_bucket(pdf_data, filename, is_test_letter):
|
||||
target_bucket_config = 'TEST_LETTERS_BUCKET_NAME' if is_test_letter else 'LETTERS_PDF_BUCKET_NAME'
|
||||
target_bucket_name = current_app.config[target_bucket_config]
|
||||
@@ -271,7 +292,7 @@ def process_virus_scan_failed(filename):
|
||||
move_failed_pdf(filename, ScanErrorType.FAILURE)
|
||||
reference = get_reference_from_filename(filename)
|
||||
notification = dao_get_notification_by_reference(reference)
|
||||
updated_count = update_letter_pdf_status(reference, NOTIFICATION_VIRUS_SCAN_FAILED)
|
||||
updated_count = update_letter_pdf_status(reference, NOTIFICATION_VIRUS_SCAN_FAILED, billable_units=0)
|
||||
|
||||
if updated_count != 1:
|
||||
raise Exception(
|
||||
@@ -290,7 +311,7 @@ def process_virus_scan_error(filename):
|
||||
move_failed_pdf(filename, ScanErrorType.ERROR)
|
||||
reference = get_reference_from_filename(filename)
|
||||
notification = dao_get_notification_by_reference(reference)
|
||||
updated_count = update_letter_pdf_status(reference, NOTIFICATION_TECHNICAL_FAILURE)
|
||||
updated_count = update_letter_pdf_status(reference, NOTIFICATION_TECHNICAL_FAILURE, billable_units=0)
|
||||
|
||||
if updated_count != 1:
|
||||
raise Exception(
|
||||
@@ -303,11 +324,12 @@ def process_virus_scan_error(filename):
|
||||
raise error
|
||||
|
||||
|
||||
def update_letter_pdf_status(reference, status):
|
||||
def update_letter_pdf_status(reference, status, billable_units):
|
||||
return dao_update_notifications_by_reference(
|
||||
references=[reference],
|
||||
update_dict={
|
||||
'status': status,
|
||||
'billable_units': billable_units,
|
||||
'updated_at': datetime.utcnow()
|
||||
})[0]
|
||||
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
import base64
|
||||
import functools
|
||||
import io
|
||||
import math
|
||||
|
||||
import werkzeug
|
||||
from flask import request, jsonify, current_app, abort
|
||||
from notifications_utils.pdf import pdf_page_count, PdfReadError
|
||||
from notifications_utils.recipients import try_validate_and_format_phone_number
|
||||
|
||||
from app import api_user, authenticated_service, notify_celery, document_download_client
|
||||
from app.celery.letters_pdf_tasks import create_letters_pdf
|
||||
from app.celery.research_mode_tasks import create_fake_letter_response_file
|
||||
from app.clients.document_download import DocumentDownloadError
|
||||
from app.config import QueueNames, TaskNames
|
||||
from app.dao.notifications_dao import update_notification_status_by_reference
|
||||
@@ -30,17 +29,15 @@ from app.models import (
|
||||
NOTIFICATION_DELIVERED,
|
||||
NOTIFICATION_PENDING_VIRUS_CHECK,
|
||||
)
|
||||
from app.celery.letters_pdf_tasks import create_letters_pdf
|
||||
from app.celery.research_mode_tasks import create_fake_letter_response_file
|
||||
from app.notifications.process_letter_notifications import (
|
||||
create_letter_notification
|
||||
)
|
||||
from app.notifications.process_notifications import (
|
||||
persist_notification,
|
||||
persist_scheduled_notification,
|
||||
send_notification_to_queue,
|
||||
simulated_recipient
|
||||
)
|
||||
from app.notifications.process_letter_notifications import (
|
||||
create_letter_notification
|
||||
)
|
||||
from app.notifications.validators import (
|
||||
validate_and_format_recipient,
|
||||
check_rate_limiting,
|
||||
@@ -53,17 +50,17 @@ from app.notifications.validators import (
|
||||
from app.schema_validation import validate
|
||||
from app.v2.errors import BadRequestError
|
||||
from app.v2.notifications import v2_notification_blueprint
|
||||
from app.v2.notifications.create_response import (
|
||||
create_post_sms_response_from_notification,
|
||||
create_post_email_response_from_notification,
|
||||
create_post_letter_response_from_notification
|
||||
)
|
||||
from app.v2.notifications.notification_schemas import (
|
||||
post_sms_request,
|
||||
post_email_request,
|
||||
post_letter_request,
|
||||
post_precompiled_letter_request
|
||||
)
|
||||
from app.v2.notifications.create_response import (
|
||||
create_post_sms_response_from_notification,
|
||||
create_post_email_response_from_notification,
|
||||
create_post_letter_response_from_notification
|
||||
)
|
||||
|
||||
|
||||
@v2_notification_blueprint.route('/{}'.format(LETTER_TYPE), methods=['POST'])
|
||||
@@ -293,21 +290,14 @@ def process_precompiled_letter_notifications(*, letter_data, api_key, template,
|
||||
try:
|
||||
status = NOTIFICATION_PENDING_VIRUS_CHECK
|
||||
letter_content = base64.b64decode(letter_data['content'])
|
||||
pages = pdf_page_count(io.BytesIO(letter_content))
|
||||
except ValueError:
|
||||
raise BadRequestError(message='Cannot decode letter content (invalid base64 encoding)', status_code=400)
|
||||
except PdfReadError:
|
||||
current_app.logger.exception(msg='Invalid PDF received')
|
||||
raise BadRequestError(message='Letter content is not a valid PDF', status_code=400)
|
||||
|
||||
pages_per_sheet = 2
|
||||
billable_units = math.ceil(pages / pages_per_sheet)
|
||||
notification = create_letter_notification(letter_data=letter_data,
|
||||
template=template,
|
||||
api_key=api_key,
|
||||
status=status,
|
||||
reply_to_text=reply_to_text,
|
||||
billable_units=billable_units)
|
||||
reply_to_text=reply_to_text)
|
||||
|
||||
filename = upload_letter_pdf(notification, letter_content, precompiled=True)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user