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
|
|
|
|
2018-03-02 14:54:28 +00:00
|
|
|
import boto3
|
2018-02-23 10:39:32 +00:00
|
|
|
from flask import current_app
|
|
|
|
|
|
|
|
|
|
from notifications_utils.s3 import s3upload
|
|
|
|
|
|
2018-03-14 21:27:07 +00:00
|
|
|
from app.models import KEY_TYPE_TEST
|
2018-02-23 10:39:32 +00:00
|
|
|
from app.variables import Retention
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2018-03-19 13:28:16 +00:00
|
|
|
def get_folder_name(_now, is_test_or_scan_letter=False):
|
|
|
|
|
if is_test_or_scan_letter:
|
2018-03-14 18:15:00 +00:00
|
|
|
folder_name = ''
|
|
|
|
|
else:
|
|
|
|
|
print_datetime = _now
|
|
|
|
|
if _now.time() > current_app.config.get('LETTER_PROCESSING_DEADLINE'):
|
|
|
|
|
print_datetime = _now + timedelta(days=1)
|
|
|
|
|
folder_name = '{}/'.format(print_datetime.date())
|
|
|
|
|
return folder_name
|
|
|
|
|
|
2018-02-23 10:39:32 +00:00
|
|
|
|
2018-03-23 12:04:37 +00:00
|
|
|
def get_letter_pdf_filename(reference, crown, is_scan_letter=False):
|
2018-03-14 18:15:00 +00:00
|
|
|
now = datetime.utcnow()
|
2018-02-23 10:39:32 +00:00
|
|
|
|
|
|
|
|
upload_file_name = LETTERS_PDF_FILE_LOCATION_STRUCTURE.format(
|
2018-03-23 12:04:37 +00:00
|
|
|
folder=get_folder_name(now, is_scan_letter),
|
2018-02-23 10:39:32 +00:00
|
|
|
reference=reference,
|
|
|
|
|
duplex="D",
|
|
|
|
|
letter_class="2",
|
|
|
|
|
colour="C",
|
|
|
|
|
crown="C" if crown else "N",
|
|
|
|
|
date=now.strftime('%Y%m%d%H%M%S')
|
|
|
|
|
).upper()
|
|
|
|
|
|
|
|
|
|
return upload_file_name
|
|
|
|
|
|
|
|
|
|
|
2018-03-14 21:27:07 +00:00
|
|
|
def get_bucket_prefix_for_notification(notification, is_test_letter=False):
|
2018-03-02 14:54:28 +00:00
|
|
|
upload_file_name = PRECOMPILED_BUCKET_PREFIX.format(
|
2018-03-14 21:27:07 +00:00
|
|
|
folder='' if is_test_letter else
|
|
|
|
|
'{}/'.format(notification.created_at.date()),
|
2018-03-02 14:54:28 +00:00
|
|
|
reference=notification.reference
|
|
|
|
|
).upper()
|
|
|
|
|
|
|
|
|
|
return upload_file_name
|
|
|
|
|
|
|
|
|
|
|
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-03-23 12:04:37 +00:00
|
|
|
def upload_letter_pdf(notification, pdf_data):
|
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(
|
2018-03-19 13:28:16 +00:00
|
|
|
notification.reference,
|
|
|
|
|
notification.service.crown,
|
2018-03-23 12:04:37 +00:00
|
|
|
is_scan_letter=notification.template.is_precompiled_letter)
|
2018-03-14 18:15:00 +00:00
|
|
|
|
2018-03-23 12:04:37 +00:00
|
|
|
if notification.template.is_precompiled_letter:
|
|
|
|
|
bucket_name = current_app.config['LETTERS_SCAN_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-02-23 10:39:32 +00:00
|
|
|
file_location=upload_file_name,
|
|
|
|
|
tags={Retention.KEY: Retention.ONE_WEEK}
|
|
|
|
|
)
|
|
|
|
|
|
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-23 12:04:37 +00:00
|
|
|
def move_scanned_pdf_to_test_or_live_pdf_bucket(filename, is_test_letter=False):
|
2018-03-19 13:28:16 +00:00
|
|
|
source_bucket_name = current_app.config['LETTERS_SCAN_BUCKET_NAME']
|
2018-03-23 12:04:37 +00:00
|
|
|
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]
|
2018-03-19 13:28:16 +00:00
|
|
|
|
|
|
|
|
s3 = boto3.resource('s3')
|
|
|
|
|
copy_source = {'Bucket': source_bucket_name, 'Key': filename}
|
2018-03-23 14:59:48 +00:00
|
|
|
target_filename = get_folder_name(datetime.utcnow(), is_test_letter) + filename
|
|
|
|
|
|
2018-03-19 13:28:16 +00:00
|
|
|
target_bucket = s3.Bucket(target_bucket_name)
|
|
|
|
|
obj = target_bucket.Object(target_filename)
|
2018-03-21 16:29:48 +00:00
|
|
|
|
|
|
|
|
# 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
|
2018-03-19 13:28:16 +00:00
|
|
|
obj.copy(copy_source, ExtraArgs={'ServerSideEncryption': 'AES256'})
|
|
|
|
|
|
|
|
|
|
s3.Object(source_bucket_name, filename).delete()
|
|
|
|
|
|
|
|
|
|
current_app.logger.info("Moved letter PDF: {}/{} to {}/{}".format(
|
|
|
|
|
source_bucket_name, filename, target_bucket_name, target_filename))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def delete_pdf_from_letters_scan_bucket(filename):
|
|
|
|
|
bucket_name = current_app.config['LETTERS_SCAN_BUCKET_NAME']
|
|
|
|
|
|
|
|
|
|
s3 = boto3.resource('s3')
|
|
|
|
|
s3.Object(bucket_name, filename).delete()
|
|
|
|
|
|
|
|
|
|
current_app.logger.info("Deleted letter PDF: {}/{}".format(bucket_name, filename))
|
2018-03-02 14:54:28 +00:00
|
|
|
|
|
|
|
|
|
2018-03-23 15:27:24 +00:00
|
|
|
def move_failed_pdf(filename, scan_error_type):
|
|
|
|
|
bucket_name = current_app.config['LETTERS_SCAN_BUCKET_NAME']
|
|
|
|
|
|
|
|
|
|
s3 = boto3.resource('s3')
|
|
|
|
|
copy_source = {'Bucket': bucket_name, 'Key': filename}
|
|
|
|
|
|
|
|
|
|
if scan_error_type == ScanErrorType.ERROR:
|
|
|
|
|
target_filename = 'ERROR/' + filename
|
|
|
|
|
elif scan_error_type == ScanErrorType.FAILURE:
|
|
|
|
|
target_filename = 'FAILURE/' + filename
|
|
|
|
|
|
|
|
|
|
target_bucket = s3.Bucket(bucket_name)
|
|
|
|
|
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
|
|
|
|
|
obj.copy(copy_source, ExtraArgs={'ServerSideEncryption': 'AES256'})
|
|
|
|
|
|
|
|
|
|
s3.Object(bucket_name, filename).delete()
|
|
|
|
|
|
|
|
|
|
current_app.logger.info("Moved letter PDF: {}/{} to {}/{}".format(
|
|
|
|
|
bucket_name, filename, bucket_name, target_filename))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def move_pdf_from_letters_scan_bucket(filename, scan_error_type):
|
|
|
|
|
bucket_name = current_app.config['LETTERS_SCAN_BUCKET_NAME']
|
|
|
|
|
|
|
|
|
|
s3 = boto3.resource('s3')
|
|
|
|
|
|
|
|
|
|
if scan_error_type == ScanErrorType.ERROR:
|
|
|
|
|
file_path = 'ERROR/' + filename
|
|
|
|
|
elif scan_error_type == ScanErrorType.FA:
|
|
|
|
|
file_path = 'FAILURE/' + filename
|
|
|
|
|
|
|
|
|
|
s3.Object(bucket_name, file_path).delete()
|
|
|
|
|
|
|
|
|
|
current_app.logger.info("Moved letter PDF: {}/{} to {}/{}".format(bucket_name, filename, bucket_name, file_path))
|
|
|
|
|
|
|
|
|
|
|
2018-03-02 14:54:28 +00:00
|
|
|
def get_letter_pdf(notification):
|
2018-03-14 21:27:07 +00:00
|
|
|
is_test_letter = notification.key_type == KEY_TYPE_TEST and notification.template.is_precompiled_letter
|
|
|
|
|
if is_test_letter:
|
|
|
|
|
bucket_name = current_app.config['TEST_LETTERS_BUCKET_NAME']
|
|
|
|
|
else:
|
|
|
|
|
bucket_name = current_app.config['LETTERS_PDF_BUCKET_NAME']
|
2018-03-02 14:54:28 +00:00
|
|
|
|
|
|
|
|
s3 = boto3.resource('s3')
|
|
|
|
|
bucket = s3.Bucket(bucket_name)
|
|
|
|
|
|
2018-03-14 21:27:07 +00:00
|
|
|
for item in bucket.objects.filter(Prefix=get_bucket_prefix_for_notification(notification, is_test_letter)):
|
2018-03-02 14:54:28 +00:00
|
|
|
obj = s3.Object(
|
|
|
|
|
bucket_name=bucket_name,
|
|
|
|
|
key=item.key
|
|
|
|
|
)
|
|
|
|
|
file_content = obj.get()["Body"].read()
|
|
|
|
|
|
|
|
|
|
return file_content
|