def email_enterprise_report(domain, slug, couch_user): account = BillingAccount.get_account_by_domain(domain) report = EnterpriseReport.create(slug, account.id, couch_user) # Generate file csv_file = io.StringIO() writer = csv.writer(csv_file) writer.writerow(report.headers) writer.writerows(report.rows) # Store file in redis hash_id = uuid.uuid4().hex redis = get_redis_client() redis.set(hash_id, csv_file.getvalue()) redis.expire(hash_id, 60 * 60 * 24) csv_file.close() # Send email url = absolute_reverse("enterprise_dashboard_download", args=[domain, report.slug, str(hash_id)]) link = "<a href='{}'>{}</a>".format(url, url) subject = _("Enterprise Dashboard: {}").format(report.title) body = "The enterprise report you requested for the account {} is ready.<br>" \ "You can download the data at the following link: {}<br><br>" \ "Please remember that this link will only be active for 24 hours.".format(account.name, link) send_html_email_async(subject, couch_user.username, body)
def enterprise_dashboard_email(request, domain, slug): account = _get_account_or_404(request, domain) report = EnterpriseReport.create(slug, account.id, request.couch_user) email_enterprise_report.delay(domain, slug, request.couch_user) message = _("Generating {title} report, will email to {email} when complete.").format(**{ 'title': report.title, 'email': request.couch_user.username, }) return JsonResponse({'message': message})
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 enterprise_dashboard_download(request, domain, slug, export_hash): account = _get_account_or_404(request, domain) report = EnterpriseReport.create(slug, account.id, request.couch_user) redis = get_redis_client() content = redis.get(export_hash) if content: file = ContentFile(content) response = HttpResponse(file, Format.FORMAT_DICT[Format.UNZIPPED_CSV]) response['Content-Length'] = file.size response['Content-Disposition'] = 'attachment; filename="{}"'.format(report.filename) return response return HttpResponseNotFound(_("That report was not found. Please remember that " "download links expire after 24 hours."))
def enterprise_dashboard(request, domain): account = _get_account_or_404(request, domain) context = { 'account': account, 'domain': domain, 'reports': [EnterpriseReport.create(slug, account.id, request.couch_user) for slug in ( EnterpriseReport.DOMAINS, EnterpriseReport.WEB_USERS, EnterpriseReport.MOBILE_USERS, EnterpriseReport.FORM_SUBMISSIONS, )], 'current_page': { 'page_name': _('Enterprise Dashboard'), } } return render(request, "accounting/enterprise_dashboard.html", context)
def enterprise_dashboard(request, domain): account = _get_account_or_404(request, domain) context = { 'account': account, 'domain': domain, 'reports': [EnterpriseReport.create(slug, account.id, request.couch_user) for slug in ( EnterpriseReport.DOMAINS, EnterpriseReport.WEB_USERS, EnterpriseReport.MOBILE_USERS, EnterpriseReport.FORM_SUBMISSIONS, )], 'current_page': { 'page_name': _('Enterprise Dashboard'), 'title': _('Enterprise Dashboard'), } } return render(request, "accounting/enterprise_dashboard.html", context)
def enterprise_dashboard(request, account_id): account = BillingAccount.objects.get(id=account_id) context = { 'account': account, 'reports': [ EnterpriseReport.create(slug, account.id, request.couch_user) for slug in ( EnterpriseReport.DOMAINS, EnterpriseReport.WEB_USERS, EnterpriseReport.MOBILE_USERS, EnterpriseReport.FORM_SUBMISSIONS, ) ], } return render(request, "accounting/enterprise_dashboard.html", context)
def _write_file(self, slug): report = EnterpriseReport.create(slug, self.account_id, self.couch_user) row_count = 0 csv_file = io.StringIO() writer = csv.writer(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 enterprise_dashboard(request, domain): account = _get_account_or_404(request, domain) if not has_privilege(request, privileges.PROJECT_ACCESS): return HttpResponseRedirect(reverse(EnterpriseBillingStatementsView.urlname, args=(domain,))) context = { 'account': account, 'domain': domain, 'reports': [EnterpriseReport.create(slug, account.id, request.couch_user) for slug in ( EnterpriseReport.DOMAINS, EnterpriseReport.WEB_USERS, EnterpriseReport.MOBILE_USERS, EnterpriseReport.FORM_SUBMISSIONS, )], 'current_page': { 'page_name': _('Enterprise Dashboard'), 'title': _('Enterprise Dashboard'), } } return render(request, "accounting/enterprise_dashboard.html", context)
def enterprise_dashboard_total(request, domain, slug): account = _get_account_or_404(request, domain) report = EnterpriseReport.create(slug, account.id, request.couch_user) return JsonResponse({'total': report.total})