mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-10 07:12:20 -05:00
Merge pull request #2436 from alphagov/command-to-get-zips-sent-details
Add command to get notification details from 'zips sent' files
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import csv
|
||||
import functools
|
||||
import uuid
|
||||
from datetime import datetime, timedelta
|
||||
@@ -720,3 +721,39 @@ def populate_organisations_from_file(file_name):
|
||||
except IntegrityError:
|
||||
print("duplicate domain", d.strip())
|
||||
db.session.rollback()
|
||||
|
||||
|
||||
@notify_command(name='get-letter-details-from-zips-sent-file')
|
||||
@click.argument('file_paths', required=True, nargs=-1)
|
||||
@statsd(namespace="tasks")
|
||||
def get_letter_details_from_zips_sent_file(file_paths):
|
||||
"""Get notification details from letters listed in zips_sent file(s)
|
||||
|
||||
This takes one or more file paths for the zips_sent files in S3 as its parameters, for example:
|
||||
get-letter-details-from-zips-sent-file '2019-04-01/zips_sent/filename_1' '2019-04-01/zips_sent/filename_2'
|
||||
"""
|
||||
|
||||
rows_from_file = []
|
||||
|
||||
for path in file_paths:
|
||||
file_contents = s3.get_s3_file(
|
||||
bucket_name=current_app.config['LETTERS_PDF_BUCKET_NAME'],
|
||||
file_location=path
|
||||
)
|
||||
rows_from_file.extend(json.loads(file_contents))
|
||||
|
||||
notification_references = tuple(row[18:34] for row in rows_from_file)
|
||||
|
||||
sql = """
|
||||
SELECT id, service_id, reference, job_id, created_at
|
||||
FROM notifications
|
||||
WHERE reference IN :notification_references
|
||||
ORDER BY service_id, job_id"""
|
||||
result = db.session.execute(sql, {'notification_references': notification_references}).fetchall()
|
||||
|
||||
with open('zips_sent_details.csv', 'w') as csvfile:
|
||||
csv_writer = csv.writer(csvfile)
|
||||
csv_writer.writerow(['notification_id', 'service_id', 'reference', 'job_id', 'created_at'])
|
||||
|
||||
for row in result:
|
||||
csv_writer.writerow(row)
|
||||
|
||||
Reference in New Issue
Block a user