def _write_file(self, slug, headers, process_domain): row_count = 0 csv_file = io.BytesIO() writer = UnicodeWriter(csv_file) writer.writerow(headers) for domain in [ domain for domain in map(Domain.get_by_name, self.domain_names) if domain ]: rows = process_domain(domain) row_count = row_count + len(rows) writer.writerows(rows) print('Wrote {} lines of {}'.format(row_count, slug)) attachment = { 'title': 'enterprise_{}_{}.csv'.format(slug, self.now.strftime('%Y%m%d_%H%M%S')), 'mimetype': 'text/csv', 'file_obj': csv_file, } return (attachment, row_count)
def _mass_email_attachment(name, rows): csv_file = io.BytesIO() writer = UnicodeWriter(csv_file) writer.writerow(['Email', 'Error']) writer.writerows(rows) attachment = { 'title': "mass_email_{}.csv".format(name), 'mimetype': 'text/csv', 'file_obj': csv_file, } return attachment
def enterprise_dashboard_download(request, account_id, slug): account = BillingAccount.objects.get(id=account_id) report = EnterpriseReport.create(slug, account.id, request.couch_user) response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = 'attachment; filename="{}"'.format( report.filename) writer = UnicodeWriter(response) writer.writerow(report.headers) writer.writerows(report.rows) return response
def send_status_email(domain, async_result): errors = [] duration = datetime.timedelta() updates = 0 noupdates = 0 batch_info_template = "Batch {index}: Completed in {duration}. Errors: {errors}. Updates: {updates}\n" batch_info_message = "" for i, batch_info in enumerate(async_result): errors += batch_info.errors duration += batch_info.duration updates += batch_info.update_count noupdates += batch_info.noupdate_count batch_info_message += batch_info_template.format( index=i + 1, duration=batch_info.duration, updates=batch_info.update_count, errors=len(batch_info.errors), ) subject = "eNikshay Episode Task results for: {}".format( datetime.date.today()) recipient = "{}@{}.{}".format('commcarehq-ops+admins', 'dimagi', 'com') cc = "{}@{}.{}".format('frener', 'dimagi', 'com') csv_file = io.BytesIO() writer = UnicodeWriter(csv_file) writer.writerow(['Episode ID', 'Domain', 'Updater Class', 'Error']) writer.writerows(errors) message = ( "domain: {domain},\n Summary: \n " "total duration: {duration} \n" "total updates: {updates} \n total errors: {errors} \n total non-updates: {noupdates} \n" "".format(domain=domain, duration=duration, updates=updates, errors=len(errors), noupdates=noupdates)) message += batch_info_message attachment = { 'title': "failed_episodes_{}.csv".format(datetime.date.today()), 'mimetype': 'text/csv', 'file_obj': csv_file, } send_html_email_async(subject, recipient, message, cc=[cc], text_content=message, file_attachments=[attachment])
def _write_file(self, slug): report = EnterpriseReport.create(slug, self.account_id, self.couch_user) row_count = 0 csv_file = io.BytesIO() writer = UnicodeWriter(csv_file) writer.writerow(report.headers) rows = report.rows row_count = len(rows) writer.writerows(rows) print('Wrote {} lines of {}'.format(row_count, slug)) attachment = { 'title': report.filename, 'mimetype': 'text/csv', 'file_obj': csv_file, } return (attachment, row_count)
def send_status_email(domain, async_result): errors = [] duration = datetime.timedelta() updates = 0 noupdates = 0 for batch_info in async_result: errors += batch_info.errors duration += batch_info.duration updates += batch_info.update_count noupdates += batch_info.noupdate_count subject = "eNikshay Episode Task results for: {}".format( datetime.date.today()) recipient = "{}@{}.{}".format('commcarehq-ops+admins', 'dimagi', 'com') cc = "{}@{}.{}".format('frener', 'dimagi', 'com') csv_file = StringIO() writer = UnicodeWriter(csv_file) writer.writerow(['Episode ID', 'Domain', 'Updater Class', 'Error']) writer.writerows(errors) message = ( "Summary of enikshay_task: domain: {domain}, duration (sec): {duration} " "Cases Updated {updates}, cases errored {errors} and {noupdates} " "cases didn't need update. ".format(domain=domain, duration=duration, updates=updates, errors=len(errors), noupdates=noupdates)) attachment = { 'title': "failed_episodes_{}.csv".format(datetime.date.today()), 'mimetype': 'text/csv', 'file_obj': csv_file, } send_html_email_async(subject, recipient, message, cc=[cc], text_content=message, file_attachments=[attachment])