Updated to retrieve csv upload from new bucket.

Fix test errors.
This commit is contained in:
Nicholas Staples
2016-04-07 13:44:04 +01:00
parent 74700334e6
commit 143d1b0db8
9 changed files with 88 additions and 45 deletions

View File

@@ -1,16 +1,23 @@
from boto3 import resource
from flask import current_app
FILE_LOCATION_STRUCTURE = 'service-{}-notify/{}.csv'
def get_s3_job_object(bucket_name, job_id):
def get_s3_job_object(bucket_name, file_location):
s3 = resource('s3')
return s3.Object(bucket_name, '{}.csv'.format(job_id))
return s3.Object(bucket_name, file_location)
def get_job_from_s3(bucket_name, job_id):
obj = get_s3_job_object(bucket_name, job_id)
def get_job_from_s3(service_id, job_id):
bucket_name = current_app.config['CSV_UPLOAD_BUCKET_NAME']
file_location = FILE_LOCATION_STRUCTURE.format(service_id, job_id)
obj = get_s3_job_object(bucket_name, file_location)
return obj.get()['Body'].read().decode('utf-8')
def remove_job_from_s3(bucket_name, job_id):
obj = get_s3_job_object(bucket_name, job_id)
def remove_job_from_s3(service_id, job_id):
bucket_name = current_app.config['CSV_UPLOAD_BUCKET_NAME']
file_location = FILE_LOCATION_STRUCTURE.format(service_id, job_id)
obj = get_s3_job_object(bucket_name, file_location)
return obj.delete()

View File

@@ -146,7 +146,7 @@ def process_job(job_id):
)
for recipient, personalisation in RecipientCSV(
s3.get_job_from_s3(job.bucket_name, job_id),
s3.get_job_from_s3(str(service.id), str(job_id)),
template_type=template.template_type,
placeholders=template.placeholders
).recipients_and_personalisation:
@@ -191,7 +191,7 @@ def process_job(job_id):
@notify_celery.task(name="remove-job")
def remove_job(job_id):
job = dao_get_job_by_id(job_id)
s3.remove_job_from_s3(job.bucket_name, job_id)
s3.remove_job_from_s3(job.service.id, str(job_id))
current_app.logger.info("Job {} has been removed from s3.".format(job_id))

View File

@@ -171,8 +171,6 @@ class Job(db.Model):
id = db.Column(UUID(as_uuid=True), primary_key=True)
original_file_name = db.Column(db.String, nullable=False)
bucket_name = db.Column(db.String, nullable=False)
file_name = db.Column(db.String, nullable=False)
service_id = db.Column(UUID(as_uuid=True), db.ForeignKey('services.id'), index=True, unique=False, nullable=False)
service = db.relationship('Service', backref=db.backref('jobs', lazy='dynamic'))
template_id = db.Column(db.BigInteger, db.ForeignKey('templates.id'), index=True, unique=False)