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 _gir_csv_response(month, year): query_month = "{year}-{month}-01".format(year=year, month=month) prev_month = "{year}-{month}-01".format(year=year, month=month - 1) two_ago = "{year}-{month}-01".format(year=year, month=month - 2) if not GIRRow.objects.filter(month=query_month).exists(): return HttpResponse('Sorry, that month is not yet available') queryset = GIRRow.objects.filter( month__in=[query_month, prev_month, two_ago]).order_by('-month') domain_months = defaultdict(list) for item in queryset: domain_months[item.domain_name].append(item) field_names = GIR_FIELDS response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = u'attachment; filename=gir.csv' writer = UnicodeWriter(response) writer.writerow(list(field_names)) for months in domain_months.values(): writer.writerow(months[0].export_row(months[1:])) return response
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])