def create_file(export): """ This creates the export file for a given export. This creates a temp file, downloads all suppressions, writing them to the temp file. After it is complete is uploads the file to s3, and updates the export record in the database. """ filename = '/tmp/{}-{}.csv'.format(export.domain, uuid4()) headers = False total = 0 with open(filename, 'w') as fp: csv_writer = csv.writer(fp) try: response = mailgun.list_suppressions(export) while response['items']: for item in response['items']: if not headers: headers = list(item.keys()) csv_writer.writerow(headers) total += 1 csv_writer.writerow([item[h] for h in headers]) response = mailgun.list_suppressions( export, response['paging']['next']) export.filename = s3.upload(filename) export.status = "completed" export.total = total exports.update(export.key, export.serialize()) finally: os.remove(filename)
def create_file(export): """ This creates the export file for a given export. This creates a temp file, downloads all suppressions, writing them to the temp file. After it is complete is uploads the file to s3, and updates the export record in the database. """ filename = '/tmp/{}-{}.csv'.format(export.domain, uuid4()) headers = False total = 0 with open(filename, 'w') as fp: csv_writer = csv.writer(fp) try: response = mailgun.list_suppressions(export) while response['items']: for item in response['items']: if not headers: headers = list(item.keys()) csv_writer.writerow(headers) total += 1 csv_writer.writerow([item[h] for h in headers]) response = mailgun.list_suppressions(export, response['paging']['next']) export.filename = s3.upload(filename) export.status = "completed" export.total = total exports.update(export.key, export.serialize()) finally: os.remove(filename)
def process_pending(): for e in exports.find({'status': "pending"}): try: export = Export(e) export.status = 'processing' exports.update(export.key, export.serialize()) create_file(export) except Exception as e: export.status = 'error' exports.update(export.key, export.serialize()) print(str(e)) # Keep processing files, but log it