Refactor code to move business logic

- DVLA specific logic is now moved to letters_pdf_tasks
This commit is contained in:
Ken Tsang
2018-01-23 17:45:35 +00:00
parent 3355a29d01
commit edd030e5dd
4 changed files with 75 additions and 73 deletions

View File

@@ -1,3 +1,4 @@
from datetime import datetime, timedelta
import math
from flask import current_app
@@ -7,6 +8,8 @@ from requests import (
)
from botocore.exceptions import ClientError as BotoClientError
from notifications_utils.s3 import s3upload
from app import notify_celery
from app.aws import s3
from app.config import QueueNames, TaskNames
@@ -19,6 +22,29 @@ from app.dao.notifications_dao import (
from app.models import NOTIFICATION_CREATED
from app.statsd_decorators import statsd
LETTERS_PDF_FILE_LOCATION_STRUCTURE = \
'{folder}/NOTIFY.{reference}.{duplex}.{letter_class}.{colour}.{crown}.{date}.pdf'
def get_letter_pdf_filename(reference, crown):
now = datetime.utcnow()
print_datetime = now
if now.time() > current_app.config.get('LETTER_PROCESSING_DEADLINE'):
print_datetime = now + timedelta(days=1)
upload_file_name = LETTERS_PDF_FILE_LOCATION_STRUCTURE.format(
folder=print_datetime.date(),
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
@notify_celery.task(bind=True, name="create-letters-pdf", max_retries=15, default_retry_delay=300)
@statsd(namespace="tasks")
@@ -34,7 +60,19 @@ def create_letters_pdf(self, notification_id):
)
current_app.logger.info("PDF Letter {} reference {} created at {}, {} bytes".format(
notification.id, notification.reference, notification.created_at, len(pdf_data)))
s3.upload_letters_pdf(reference=notification.reference, crown=notification.service.crown, filedata=pdf_data)
upload_file_name = get_letter_pdf_filename(
notification.reference, notification.service.crown)
s3upload(
filedata=pdf_data,
region=current_app.config['AWS_REGION'],
bucket_name=current_app.config['LETTERS_PDF_BUCKET_NAME'],
file_location=upload_file_name
)
current_app.logger.info("Uploaded letters PDF {} to {}".format(
upload_file_name, current_app.config['LETTERS_PDF_BUCKET_NAME']))
notification.billable_units = billable_units
dao_update_notification(notification)