mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 07:35:34 -05:00
Updated API to handle pre-compiled pdfs
* added a method to letter/utils.py to get the PDF document from the S3 bucket * added the logic to return the pdf or to produce a png of the pdf
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
import boto3
|
||||
from flask import current_app
|
||||
|
||||
from notifications_utils.s3 import s3upload
|
||||
@@ -10,6 +11,8 @@ from app.variables import Retention
|
||||
LETTERS_PDF_FILE_LOCATION_STRUCTURE = \
|
||||
'{folder}/NOTIFY.{reference}.{duplex}.{letter_class}.{colour}.{crown}.{date}.pdf'
|
||||
|
||||
PRECOMPILED_BUCKET_PREFIX = '{folder}/NOTIFY.{reference}'
|
||||
|
||||
|
||||
def get_letter_pdf_filename(reference, crown):
|
||||
now = datetime.utcnow()
|
||||
@@ -31,6 +34,15 @@ def get_letter_pdf_filename(reference, crown):
|
||||
return upload_file_name
|
||||
|
||||
|
||||
def get_bucket_prefix_for_notification(notification):
|
||||
upload_file_name = PRECOMPILED_BUCKET_PREFIX.format(
|
||||
folder=notification.created_at.date(),
|
||||
reference=notification.reference
|
||||
).upper()
|
||||
|
||||
return upload_file_name
|
||||
|
||||
|
||||
def upload_letter_pdf(notification, pdf_data):
|
||||
current_app.logger.info("PDF Letter {} reference {} created at {}, {} bytes".format(
|
||||
notification.id, notification.reference, notification.created_at, len(pdf_data)))
|
||||
@@ -48,3 +60,19 @@ def upload_letter_pdf(notification, pdf_data):
|
||||
|
||||
current_app.logger.info("Uploaded letters PDF {} to {} for notification id {}".format(
|
||||
upload_file_name, current_app.config['LETTERS_PDF_BUCKET_NAME'], notification.id))
|
||||
|
||||
|
||||
def get_letter_pdf(notification):
|
||||
bucket_name = current_app.config['LETTERS_PDF_BUCKET_NAME']
|
||||
|
||||
s3 = boto3.resource('s3')
|
||||
bucket = s3.Bucket(bucket_name)
|
||||
|
||||
for item in bucket.objects.filter(Prefix=get_bucket_prefix_for_notification(notification)):
|
||||
obj = s3.Object(
|
||||
bucket_name=bucket_name,
|
||||
key=item.key
|
||||
)
|
||||
file_content = obj.get()["Body"].read()
|
||||
|
||||
return file_content
|
||||
|
||||
Reference in New Issue
Block a user