2019-10-16 13:35:30 +01:00
|
|
|
import boto3
|
2019-09-05 16:45:48 +01:00
|
|
|
import io
|
2019-10-16 13:35:30 +01:00
|
|
|
import json
|
2019-09-05 16:45:48 +01:00
|
|
|
import math
|
2019-10-16 13:35:30 +01:00
|
|
|
|
|
|
|
|
from app.models import KEY_TYPE_TEST, SECOND_CLASS, RESOLVE_POSTAGE_FOR_FILE_NAME, NOTIFICATION_VALIDATION_FAILED
|
|
|
|
|
|
2018-02-23 10:39:32 +00:00
|
|
|
from datetime import datetime, timedelta
|
2018-03-23 15:27:24 +00:00
|
|
|
from enum import Enum
|
2018-02-23 10:39:32 +00:00
|
|
|
|
|
|
|
|
from flask import current_app
|
|
|
|
|
|
2018-11-26 12:53:39 +00:00
|
|
|
from notifications_utils.letter_timings import LETTER_PROCESSING_DEADLINE
|
2019-09-05 16:45:48 +01:00
|
|
|
from notifications_utils.pdf import pdf_page_count
|
2018-02-23 10:39:32 +00:00
|
|
|
from notifications_utils.s3 import s3upload
|
2018-11-26 12:53:39 +00:00
|
|
|
from notifications_utils.timezones import convert_utc_to_bst
|
2018-02-23 10:39:32 +00:00
|
|
|
|
|
|
|
|
|
2018-03-23 15:27:24 +00:00
|
|
|
class ScanErrorType(Enum):
|
|
|
|
|
ERROR = 1
|
|
|
|
|
FAILURE = 2
|
|
|
|
|
|
|
|
|
|
|
2018-02-23 10:39:32 +00:00
|
|
|
LETTERS_PDF_FILE_LOCATION_STRUCTURE = \
|
2018-03-14 18:15:00 +00:00
|
|
|
'{folder}NOTIFY.{reference}.{duplex}.{letter_class}.{colour}.{crown}.{date}.pdf'
|
2018-02-23 10:39:32 +00:00
|
|
|
|
2018-03-14 21:27:07 +00:00
|
|
|
PRECOMPILED_BUCKET_PREFIX = '{folder}NOTIFY.{reference}'
|
2018-03-02 14:54:28 +00:00
|
|
|
|
2018-02-23 10:39:32 +00:00
|
|
|
|
2019-09-20 16:25:25 +01:00
|
|
|
def get_folder_name(_now, *, dont_use_sending_date=False):
|
2019-09-16 15:10:10 +01:00
|
|
|
if dont_use_sending_date:
|
2018-03-14 18:15:00 +00:00
|
|
|
folder_name = ''
|
|
|
|
|
else:
|
2018-07-02 14:16:40 +01:00
|
|
|
print_datetime = convert_utc_to_bst(_now)
|
2018-11-26 12:53:39 +00:00
|
|
|
if print_datetime.time() > LETTER_PROCESSING_DEADLINE:
|
2018-07-02 14:16:40 +01:00
|
|
|
print_datetime += timedelta(days=1)
|
2018-03-14 18:15:00 +00:00
|
|
|
folder_name = '{}/'.format(print_datetime.date())
|
|
|
|
|
return folder_name
|
|
|
|
|
|
2018-02-23 10:39:32 +00:00
|
|
|
|
2019-09-16 15:10:10 +01:00
|
|
|
def get_letter_pdf_filename(reference, crown, sending_date, dont_use_sending_date=False, postage=SECOND_CLASS):
|
2018-02-23 10:39:32 +00:00
|
|
|
upload_file_name = LETTERS_PDF_FILE_LOCATION_STRUCTURE.format(
|
2019-09-20 16:25:25 +01:00
|
|
|
folder=get_folder_name(sending_date, dont_use_sending_date=dont_use_sending_date),
|
2018-02-23 10:39:32 +00:00
|
|
|
reference=reference,
|
|
|
|
|
duplex="D",
|
2018-09-25 11:04:58 +01:00
|
|
|
letter_class=RESOLVE_POSTAGE_FOR_FILE_NAME[postage],
|
2018-02-23 10:39:32 +00:00
|
|
|
colour="C",
|
|
|
|
|
crown="C" if crown else "N",
|
2019-09-16 14:20:40 +01:00
|
|
|
date=sending_date.strftime('%Y%m%d%H%M%S')
|
2018-02-23 10:39:32 +00:00
|
|
|
).upper()
|
|
|
|
|
return upload_file_name
|
|
|
|
|
|
|
|
|
|
|
2018-10-17 16:09:30 +01:00
|
|
|
def get_bucket_name_and_prefix_for_notification(notification):
|
|
|
|
|
folder = ''
|
|
|
|
|
if notification.status == NOTIFICATION_VALIDATION_FAILED:
|
|
|
|
|
bucket_name = current_app.config['INVALID_PDF_BUCKET_NAME']
|
2019-09-09 12:44:28 +01:00
|
|
|
elif notification.key_type == KEY_TYPE_TEST:
|
2018-10-17 16:09:30 +01:00
|
|
|
bucket_name = current_app.config['TEST_LETTERS_BUCKET_NAME']
|
|
|
|
|
else:
|
|
|
|
|
bucket_name = current_app.config['LETTERS_PDF_BUCKET_NAME']
|
2019-09-20 16:25:25 +01:00
|
|
|
folder = get_folder_name(notification.created_at, dont_use_sending_date=False)
|
2018-10-17 16:09:30 +01:00
|
|
|
|
2018-03-02 14:54:28 +00:00
|
|
|
upload_file_name = PRECOMPILED_BUCKET_PREFIX.format(
|
2018-10-17 16:09:30 +01:00
|
|
|
folder=folder,
|
2018-03-02 14:54:28 +00:00
|
|
|
reference=notification.reference
|
|
|
|
|
).upper()
|
|
|
|
|
|
2018-10-17 16:09:30 +01:00
|
|
|
return bucket_name, upload_file_name
|
2018-03-02 14:54:28 +00:00
|
|
|
|
|
|
|
|
|
2018-03-19 13:28:16 +00:00
|
|
|
def get_reference_from_filename(filename):
|
|
|
|
|
# filename looks like '2018-01-13/NOTIFY.ABCDEF1234567890.D.2.C.C.20180113120000.PDF'
|
|
|
|
|
filename_parts = filename.split('.')
|
|
|
|
|
return filename_parts[1]
|
|
|
|
|
|
|
|
|
|
|
2018-04-09 13:56:44 +01:00
|
|
|
def upload_letter_pdf(notification, pdf_data, precompiled=False):
|
2018-02-23 10:39:32 +00:00
|
|
|
current_app.logger.info("PDF Letter {} reference {} created at {}, {} bytes".format(
|
|
|
|
|
notification.id, notification.reference, notification.created_at, len(pdf_data)))
|
|
|
|
|
|
|
|
|
|
upload_file_name = get_letter_pdf_filename(
|
2019-09-16 14:20:40 +01:00
|
|
|
reference=notification.reference,
|
|
|
|
|
crown=notification.service.crown,
|
|
|
|
|
sending_date=notification.created_at,
|
2019-09-16 15:10:10 +01:00
|
|
|
dont_use_sending_date=precompiled or notification.key_type == KEY_TYPE_TEST,
|
2018-09-25 11:04:58 +01:00
|
|
|
postage=notification.postage
|
|
|
|
|
)
|
2018-03-14 18:15:00 +00:00
|
|
|
|
2018-04-09 13:56:44 +01:00
|
|
|
if precompiled:
|
2018-03-23 12:04:37 +00:00
|
|
|
bucket_name = current_app.config['LETTERS_SCAN_BUCKET_NAME']
|
2019-09-09 12:44:28 +01:00
|
|
|
elif notification.key_type == KEY_TYPE_TEST:
|
|
|
|
|
bucket_name = current_app.config['TEST_LETTERS_BUCKET_NAME']
|
2018-03-14 18:15:00 +00:00
|
|
|
else:
|
2018-03-23 12:04:37 +00:00
|
|
|
bucket_name = current_app.config['LETTERS_PDF_BUCKET_NAME']
|
2018-03-13 14:08:34 +00:00
|
|
|
|
2018-02-23 10:39:32 +00:00
|
|
|
s3upload(
|
|
|
|
|
filedata=pdf_data,
|
|
|
|
|
region=current_app.config['AWS_REGION'],
|
2018-03-14 18:15:00 +00:00
|
|
|
bucket_name=bucket_name,
|
2018-08-08 16:20:25 +01:00
|
|
|
file_location=upload_file_name
|
2018-02-23 10:39:32 +00:00
|
|
|
)
|
|
|
|
|
|
2018-03-01 10:37:07 +00:00
|
|
|
current_app.logger.info("Uploaded letters PDF {} to {} for notification id {}".format(
|
2018-03-14 18:15:00 +00:00
|
|
|
upload_file_name, bucket_name, notification.id))
|
2018-03-19 13:28:16 +00:00
|
|
|
return upload_file_name
|
|
|
|
|
|
|
|
|
|
|
2018-03-27 10:32:40 +01:00
|
|
|
def move_failed_pdf(source_filename, scan_error_type):
|
|
|
|
|
scan_bucket = current_app.config['LETTERS_SCAN_BUCKET_NAME']
|
2018-03-23 15:27:24 +00:00
|
|
|
|
2018-03-27 13:32:46 +01:00
|
|
|
target_filename = ('ERROR/' if scan_error_type == ScanErrorType.ERROR else 'FAILURE/') + source_filename
|
2018-03-23 15:27:24 +00:00
|
|
|
|
2018-03-27 10:32:40 +01:00
|
|
|
_move_s3_object(scan_bucket, source_filename, scan_bucket, target_filename)
|
2018-03-23 15:27:24 +00:00
|
|
|
|
|
|
|
|
|
2018-06-27 16:40:30 +01:00
|
|
|
def move_error_pdf_to_scan_bucket(source_filename):
|
|
|
|
|
scan_bucket = current_app.config['LETTERS_SCAN_BUCKET_NAME']
|
|
|
|
|
error_file = 'ERROR/' + source_filename
|
|
|
|
|
|
|
|
|
|
_move_s3_object(scan_bucket, error_file, scan_bucket, source_filename)
|
|
|
|
|
|
|
|
|
|
|
2019-10-11 12:24:53 +01:00
|
|
|
def move_scan_to_invalid_pdf_bucket(source_filename, message=None, invalid_pages=None, page_count=None):
|
|
|
|
|
metadata = {}
|
|
|
|
|
if message:
|
2019-10-16 13:35:30 +01:00
|
|
|
metadata["message"] = message
|
2019-10-11 12:24:53 +01:00
|
|
|
if invalid_pages:
|
2019-10-16 13:35:30 +01:00
|
|
|
metadata["invalid_pages"] = json.dumps(invalid_pages)
|
2019-10-11 12:24:53 +01:00
|
|
|
if page_count:
|
|
|
|
|
metadata["page_count"] = str(page_count)
|
2019-10-14 10:41:37 +01:00
|
|
|
|
2019-10-11 12:24:53 +01:00
|
|
|
_move_s3_object(
|
|
|
|
|
source_bucket=current_app.config['LETTERS_SCAN_BUCKET_NAME'],
|
|
|
|
|
source_filename=source_filename,
|
|
|
|
|
target_bucket=current_app.config['INVALID_PDF_BUCKET_NAME'],
|
|
|
|
|
target_filename=source_filename,
|
|
|
|
|
metadata=metadata
|
|
|
|
|
)
|
2018-09-03 13:24:51 +01:00
|
|
|
|
|
|
|
|
|
2019-09-05 17:36:02 +01:00
|
|
|
def move_uploaded_pdf_to_letters_bucket(source_filename, upload_filename):
|
|
|
|
|
_move_s3_object(
|
|
|
|
|
source_bucket=current_app.config['TRANSIENT_UPLOADED_LETTERS'],
|
|
|
|
|
source_filename=source_filename,
|
|
|
|
|
target_bucket=current_app.config['LETTERS_PDF_BUCKET_NAME'],
|
2019-10-11 12:24:53 +01:00
|
|
|
target_filename=upload_filename,
|
2019-09-05 17:36:02 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2019-12-04 16:02:46 +00:00
|
|
|
def move_sanitised_letter_to_test_or_live_pdf_bucket(filename, is_test_letter, created_at):
|
|
|
|
|
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]
|
|
|
|
|
target_filename = get_folder_name(created_at, dont_use_sending_date=is_test_letter) + filename
|
|
|
|
|
|
|
|
|
|
_move_s3_object(
|
|
|
|
|
source_bucket=current_app.config['LETTER_SANITISE_BUCKET_NAME'],
|
|
|
|
|
source_filename=filename,
|
|
|
|
|
target_bucket=target_bucket_name,
|
|
|
|
|
target_filename=target_filename,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2018-06-27 16:40:30 +01:00
|
|
|
def get_file_names_from_error_bucket():
|
|
|
|
|
s3 = boto3.resource('s3')
|
|
|
|
|
scan_bucket = current_app.config['LETTERS_SCAN_BUCKET_NAME']
|
|
|
|
|
bucket = s3.Bucket(scan_bucket)
|
|
|
|
|
|
|
|
|
|
return bucket.objects.filter(Prefix="ERROR")
|
|
|
|
|
|
|
|
|
|
|
2019-10-29 16:19:50 +00:00
|
|
|
def get_letter_pdf_and_metadata(notification):
|
2018-10-17 16:09:30 +01:00
|
|
|
bucket_name, prefix = get_bucket_name_and_prefix_for_notification(notification)
|
2018-03-02 14:54:28 +00:00
|
|
|
|
|
|
|
|
s3 = boto3.resource('s3')
|
|
|
|
|
bucket = s3.Bucket(bucket_name)
|
2018-10-17 16:09:30 +01:00
|
|
|
item = next(x for x in bucket.objects.filter(Prefix=prefix))
|
2018-08-08 17:21:16 +01:00
|
|
|
|
|
|
|
|
obj = s3.Object(
|
|
|
|
|
bucket_name=bucket_name,
|
|
|
|
|
key=item.key
|
2019-10-29 16:19:50 +00:00
|
|
|
).get()
|
|
|
|
|
return obj["Body"].read(), obj["Metadata"]
|
2018-03-27 10:32:40 +01:00
|
|
|
|
|
|
|
|
|
2019-10-11 12:24:53 +01:00
|
|
|
def _move_s3_object(source_bucket, source_filename, target_bucket, target_filename, metadata=None):
|
2018-03-27 10:32:40 +01:00
|
|
|
s3 = boto3.resource('s3')
|
|
|
|
|
copy_source = {'Bucket': source_bucket, 'Key': source_filename}
|
|
|
|
|
|
|
|
|
|
target_bucket = s3.Bucket(target_bucket)
|
|
|
|
|
obj = target_bucket.Object(target_filename)
|
|
|
|
|
|
|
|
|
|
# Tags are copied across but the expiration time is reset in the destination bucket
|
|
|
|
|
# e.g. if a file has 5 days left to expire on a ONE_WEEK retention in the source bucket,
|
|
|
|
|
# in the destination bucket the expiration time will be reset to 7 days left to expire
|
2019-10-11 12:24:53 +01:00
|
|
|
put_args = {'ServerSideEncryption': 'AES256'}
|
|
|
|
|
if metadata:
|
2019-10-14 10:41:37 +01:00
|
|
|
put_args['Metadata'] = metadata
|
|
|
|
|
put_args["MetadataDirective"] = "REPLACE"
|
2019-10-11 12:24:53 +01:00
|
|
|
obj.copy(copy_source, ExtraArgs=put_args)
|
2018-03-27 10:32:40 +01:00
|
|
|
|
|
|
|
|
s3.Object(source_bucket, source_filename).delete()
|
|
|
|
|
|
|
|
|
|
current_app.logger.info("Moved letter PDF: {}/{} to {}/{}".format(
|
|
|
|
|
source_bucket, source_filename, target_bucket, target_filename))
|
2018-11-22 11:53:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def letter_print_day(created_at):
|
|
|
|
|
bst_print_datetime = convert_utc_to_bst(created_at) + timedelta(hours=6, minutes=30)
|
|
|
|
|
bst_print_date = bst_print_datetime.date()
|
|
|
|
|
|
|
|
|
|
current_bst_date = convert_utc_to_bst(datetime.utcnow()).date()
|
|
|
|
|
|
|
|
|
|
if bst_print_date >= current_bst_date:
|
|
|
|
|
return 'today'
|
|
|
|
|
else:
|
|
|
|
|
print_date = bst_print_datetime.strftime('%d %B').lstrip('0')
|
|
|
|
|
return 'on {}'.format(print_date)
|
2019-09-05 16:45:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_page_count(pdf):
|
2019-10-16 13:35:30 +01:00
|
|
|
return pdf_page_count(io.BytesIO(pdf))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_billable_units_for_letter_page_count(page_count):
|
|
|
|
|
if not page_count:
|
|
|
|
|
return 0
|
2019-09-05 16:45:48 +01:00
|
|
|
pages_per_sheet = 2
|
2019-10-16 13:35:30 +01:00
|
|
|
billable_units = math.ceil(page_count / pages_per_sheet)
|
2019-09-05 16:45:48 +01:00
|
|
|
return billable_units
|