mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-15 09:42:38 -05:00
Added letters pdf S3 upload function
- uses s3upload from notifications_utils which will create an s3 bucket if it doesn't already exist
This commit is contained in:
@@ -5,7 +5,10 @@ from flask import current_app
|
|||||||
import pytz
|
import pytz
|
||||||
from boto3 import client, resource
|
from boto3 import client, resource
|
||||||
|
|
||||||
|
from notifications_utils.s3 import s3upload as utils_s3upload
|
||||||
|
|
||||||
FILE_LOCATION_STRUCTURE = 'service-{}-notify/{}.csv'
|
FILE_LOCATION_STRUCTURE = 'service-{}-notify/{}.csv'
|
||||||
|
LETTERS_PDF_FILE_LOCATION_STRUCTURE = '{folder}/NOTIFY.{reference}.{duplex}.{letter_class}.{colour}.{crown}.{date}.pdf'
|
||||||
|
|
||||||
|
|
||||||
def get_s3_file(bucket_name, file_location):
|
def get_s3_file(bucket_name, file_location):
|
||||||
@@ -76,3 +79,23 @@ def remove_transformed_dvla_file(job_id):
|
|||||||
file_location = '{}-dvla-job.text'.format(job_id)
|
file_location = '{}-dvla-job.text'.format(job_id)
|
||||||
obj = get_s3_object(bucket_name, file_location)
|
obj = get_s3_object(bucket_name, file_location)
|
||||||
return obj.delete()
|
return obj.delete()
|
||||||
|
|
||||||
|
|
||||||
|
def upload_letters_pdf(reference, crown, filedata):
|
||||||
|
now = datetime.utcnow()
|
||||||
|
upload_file_name = LETTERS_PDF_FILE_LOCATION_STRUCTURE.format(
|
||||||
|
folder=now.date().isoformat(),
|
||||||
|
reference=reference,
|
||||||
|
duplex="D",
|
||||||
|
letter_class="2",
|
||||||
|
colour="C",
|
||||||
|
crown="C" if crown else "N",
|
||||||
|
date=now.strftime('%Y%m%d%H%M%S')
|
||||||
|
)
|
||||||
|
|
||||||
|
utils_s3upload(
|
||||||
|
filedata=filedata,
|
||||||
|
region=current_app.config['AWS_REGION'],
|
||||||
|
bucket_name=current_app.config['LETTERS_PDF_BUCKET_NAME'],
|
||||||
|
file_location=upload_file_name
|
||||||
|
)
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
from unittest.mock import call
|
from unittest.mock import call
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
import pytest
|
||||||
|
|
||||||
from flask import current_app
|
from flask import current_app
|
||||||
|
|
||||||
@@ -9,7 +10,8 @@ from app.aws.s3 import (
|
|||||||
get_s3_bucket_objects,
|
get_s3_bucket_objects,
|
||||||
get_s3_file,
|
get_s3_file,
|
||||||
filter_s3_bucket_objects_within_date_range,
|
filter_s3_bucket_objects_within_date_range,
|
||||||
remove_transformed_dvla_file
|
remove_transformed_dvla_file,
|
||||||
|
upload_letters_pdf
|
||||||
)
|
)
|
||||||
from tests.app.conftest import datetime_in_past
|
from tests.app.conftest import datetime_in_past
|
||||||
|
|
||||||
@@ -139,3 +141,21 @@ def test_get_s3_bucket_objects_does_not_return_outside_of_date_range(notify_api,
|
|||||||
filtered_items = filter_s3_bucket_objects_within_date_range(s3_objects_stub)
|
filtered_items = filter_s3_bucket_objects_within_date_range(s3_objects_stub)
|
||||||
|
|
||||||
assert len(filtered_items) == 0
|
assert len(filtered_items) == 0
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('crown_flag,expected_crown_text', [
|
||||||
|
(True, 'C'),
|
||||||
|
(False, 'N'),
|
||||||
|
])
|
||||||
|
@freeze_time("2017-12-04 15:00:00")
|
||||||
|
def test_upload_letters_pdf_calls_utils_s3upload_with_correct_args(
|
||||||
|
notify_api, mocker, crown_flag, expected_crown_text):
|
||||||
|
s3_upload_mock = mocker.patch('app.aws.s3.utils_s3upload')
|
||||||
|
upload_letters_pdf(reference='foo', crown=crown_flag, filedata='some_data')
|
||||||
|
|
||||||
|
s3_upload_mock.assert_called_with(
|
||||||
|
filedata='some_data',
|
||||||
|
region='eu-west-1',
|
||||||
|
bucket_name='test-letters-pdf',
|
||||||
|
file_location='2017-12-04/NOTIFY.foo.D.2.C.{}.20171204150000.pdf'.format(expected_crown_text)
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user