def test_get_scheduled_report_response(self): domain = 'test-scheduled-reports' user = WebUser.create( domain=domain, username='******', password='******', ) report_config = ReportConfig.wrap({ "date_range": "last30", "days": 30, "domain": domain, "report_slug": "worker_activity", "report_type": "project_report", "owner_id": user._id, }) report_config.save() report = ReportNotification( hour=12, minute=None, day=30, interval='monthly', config_ids=[report_config._id] ) report.save() response = get_scheduled_report_response( couch_user=user, domain=domain, scheduled_report_id=report._id )[0] self.assertEqual(200, response.status_code) self.assertTrue(user.username in response.serialize())
def _get_and_send_report(self, language, emails): from corehq.apps.reports.views import get_scheduled_report_response, render_full_report_notification with localize(language): title = (_(DEFAULT_REPORT_NOTIF_SUBJECT) if self.email_subject == DEFAULT_REPORT_NOTIF_SUBJECT else self.email_subject) attach_excel = getattr(self, 'attach_excel', False) content, excel_files = get_scheduled_report_response( self.owner, self.domain, self._id, attach_excel=attach_excel) slugs = [r.report_slug for r in self.configs] for email in emails: body = render_full_report_notification(None, content, email, self).content send_html_email_async.delay( title, email, body, email_from=settings.DEFAULT_FROM_EMAIL, file_attachments=excel_files, ga_track=True, ga_tracking_info={ 'cd4': self.domain, 'cd10': ', '.join(slugs) }, )
def _get_and_send_report(self, language, emails): from corehq.apps.reports.views import get_scheduled_report_response, render_full_report_notification with localize(language): title = (_(DEFAULT_REPORT_NOTIF_SUBJECT) if self.email_subject == DEFAULT_REPORT_NOTIF_SUBJECT else self.email_subject) attach_excel = getattr(self, 'attach_excel', False) content, excel_files = get_scheduled_report_response( self.owner, self.domain, self._id, attach_excel=attach_excel, send_only_active=True) # Will be False if ALL the ReportConfigs in the ReportNotification # have a start_date in the future. if content is False: return for email in emails: body = render_full_report_notification(None, content, email, self).content send_html_email_async.delay( title, email, body, email_from=settings.DEFAULT_FROM_EMAIL, file_attachments=excel_files)
def test_get_scheduled_report_response(self): domain = self.domain user = WebUser.create( domain=domain, username='******', password='******', ) report_config = ReportConfig.wrap({ "date_range": "last30", "days": 30, "domain": domain, "report_slug": "worker_activity", "report_type": "project_report", "owner_id": user._id, }) report_config.save() report = ReportNotification(hour=12, minute=None, day=30, interval='monthly', config_ids=[report_config._id]) report.save() response = get_scheduled_report_response( couch_user=user, domain=domain, scheduled_report_id=report._id)[0] self.assertTrue(user.username in response)
def _generate_report(self, attach_excel, title, emails): from corehq.apps.reports.views import get_scheduled_report_response report_text = None excel_files = None try: report_text, excel_files = get_scheduled_report_response( self.owner, self.domain, self._id, attach_excel=attach_excel, send_only_active=True) # TODO: Be more specific with our error-handling. If building the report could fail, # we should have a reasonable idea of why except Exception as er: notify_exception( None, message="Encountered error while generating report", details={ 'subject': title, 'recipients': str(emails), 'error': er, }) if isinstance(er, ESError): # An ElasticSearch error could indicate that the report itself is too large. # Try exporting the report instead, as that builds the report in chunks, # rather than all at once. # TODO: narrow down this handling so that we don't try this if ElasticSearch is simply down, # for example self._export_report(emails, title) return report_text, excel_files
def _get_and_send_report(self, language, emails): from corehq.apps.reports.views import get_scheduled_report_response, render_full_report_notification with localize(language): title = ( _(DEFAULT_REPORT_NOTIF_SUBJECT) if self.email_subject == DEFAULT_REPORT_NOTIF_SUBJECT else self.email_subject ) attach_excel = getattr(self, 'attach_excel', False) content, excel_files = get_scheduled_report_response( self.owner, self.domain, self._id, attach_excel=attach_excel, send_only_active=True ) # Will be False if ALL the ReportConfigs in the ReportNotification # have a start_date in the future. if content is False: return for email in emails: body = render_full_report_notification(None, content, email, self).content send_html_email_async.delay( title, email, body, email_from=settings.DEFAULT_FROM_EMAIL, file_attachments=excel_files)
def _get_and_send_report(self, language, emails): from corehq.apps.reports.views import get_scheduled_report_response, render_full_report_notification with localize(language): title = ( _(DEFAULT_REPORT_NOTIF_SUBJECT) if self.email_subject == DEFAULT_REPORT_NOTIF_SUBJECT else self.email_subject ) attach_excel = getattr(self, "attach_excel", False) content, excel_files = get_scheduled_report_response( self.owner, self.domain, self._id, attach_excel=attach_excel ) slugs = [r.report_slug for r in self.configs] for email in emails: body = render_full_report_notification(None, content, email, self).content send_html_email_async.delay( title, email, body, email_from=settings.DEFAULT_FROM_EMAIL, file_attachments=excel_files, ga_track=True, ga_tracking_info={"cd4": self.domain, "cd10": ", ".join(slugs)}, )
def _get_and_send_report(self, language, emails): from corehq.apps.reports.views import get_scheduled_report_response with localize(language): title = _("Scheduled report from CommCare HQ") attach_excel = getattr(self, 'attach_excel', False) body, excel_files = get_scheduled_report_response( self.owner, self.domain, self._id, attach_excel=attach_excel ) for email in emails: send_html_email_async.delay(title, email, body.content, email_from=settings.DEFAULT_FROM_EMAIL, file_attachments=excel_files)
def send(self): from dimagi.utils.django.email import send_HTML_email from corehq.apps.reports.views import get_scheduled_report_response # Scenario: user has been removed from the domain that they # have scheduled reports for. Delete this scheduled report if not self.owner.is_member_of(self.domain): self.delete() return if self.all_recipient_emails: title = "Scheduled report from CommCare HQ" body = get_scheduled_report_response(self.owner, self.domain, self._id).content for email in self.all_recipient_emails: send_HTML_email(title, email, body, email_from=settings.DEFAULT_FROM_EMAIL)
def _get_and_send_report(self, language, emails): from corehq.apps.reports.views import get_scheduled_report_response with localize(language): title = ( _(DEFAULT_REPORT_NOTIF_SUBJECT) if self.email_subject == DEFAULT_REPORT_NOTIF_SUBJECT else self.email_subject ) attach_excel = getattr(self, 'attach_excel', False) body, excel_files = get_scheduled_report_response( self.owner, self.domain, self._id, attach_excel=attach_excel ) for email in emails: send_html_email_async.delay(title, email, body.content, email_from=settings.DEFAULT_FROM_EMAIL, file_attachments=excel_files)
def send(self): from dimagi.utils.django.email import send_HTML_email from corehq.apps.reports.views import get_scheduled_report_response # Scenario: user has been removed from the domain that they # have scheduled reports for. Delete this scheduled report if not self.owner.is_member_of(self.domain): self.delete() return if self.all_recipient_emails: title = "Scheduled report from CommCare HQ" if hasattr(self, "attach_excel"): attach_excel = self.attach_excel else: attach_excel = False body, excel_files = get_scheduled_report_response(self.owner, self.domain, self._id, attach_excel=attach_excel) for email in self.all_recipient_emails: send_HTML_email(title, email, body.content, email_from=settings.DEFAULT_FROM_EMAIL, file_attachments=excel_files)
def send(self): from dimagi.utils.django.email import send_HTML_email from corehq.apps.reports.views import get_scheduled_report_response # Scenario: user has been removed from the domain that they # have scheduled reports for. Delete this scheduled report if not self.owner.is_member_of(self.domain): self.delete() return title = "Scheduled report from CommCare HQ" body = get_scheduled_report_response(self.owner, self.domain, self._id).content for email in self.all_recipient_emails: send_HTML_email(title, email, body, email_from=settings.DEFAULT_FROM_EMAIL)
def test_get_scheduled_report_response(self): domain = self.domain report_config = ReportConfig.wrap({ "date_range": "last30", "days": 30, "domain": domain, "report_slug": "worker_activity", "report_type": "project_report", "owner_id": self.user._id, }) report_config.save() report = ReportNotification( hour=12, minute=None, day=30, interval='monthly', config_ids=[report_config._id] ) report.save() response = get_scheduled_report_response( couch_user=self.user, domain=domain, scheduled_report_id=report._id )[0] self.assertTrue(self.user.username in response.decode('utf-8'))
def test_get_scheduled_report_response(self): domain = self.domain report_config = ReportConfig.wrap({ "date_range": "last30", "days": 30, "domain": domain, "report_slug": "worker_activity", "report_type": "project_report", "owner_id": self.user._id, }) report_config.save() report = ReportNotification(hour=12, minute=None, day=30, interval='monthly', config_ids=[report_config._id]) report.save() response = get_scheduled_report_response( couch_user=self.user, domain=domain, scheduled_report_id=report._id)[0] self.assertTrue(self.user.username in response.decode('utf-8'))
def _get_and_send_report(self, language, emails): from corehq.apps.reports.views import get_scheduled_report_response, render_full_report_notification with localize(language): title = (_(DEFAULT_REPORT_NOTIF_SUBJECT) if self.email_subject == DEFAULT_REPORT_NOTIF_SUBJECT else self.email_subject) attach_excel = getattr(self, 'attach_excel', False) try: content, excel_files = get_scheduled_report_response( self.owner, self.domain, self._id, attach_excel=attach_excel, send_only_active=True) # Will be False if ALL the ReportConfigs in the ReportNotification # have a start_date in the future. if content is False: return for email in emails: body = render_full_report_notification( None, content, email, self).content send_html_email_async( title, email, body, email_from=settings.DEFAULT_FROM_EMAIL, file_attachments=excel_files, smtp_exception_skip_list=LARGE_FILE_SIZE_ERROR_CODES) except Exception as er: notify_exception( None, message= "Encountered error while generating report or sending email", details={ 'subject': title, 'recipients': str(emails), 'error': er, }) if getattr(er, 'smtp_code', None) in LARGE_FILE_SIZE_ERROR_CODES or type( er) == ESError: # If the email doesn't work because it is too large to fit in the HTML body, # send it as an excel attachment, by creating a mock request with the right data. for report_config in self.configs: mock_request = HttpRequest() mock_request.couch_user = self.owner mock_request.user = self.owner.get_django_user() mock_request.domain = self.domain mock_request.couch_user.current_domain = self.domain mock_request.couch_user.language = self.language mock_request.method = 'GET' mock_request.bypass_two_factor = True mock_query_string_parts = [ report_config.query_string, 'filterSet=true' ] if report_config.is_configurable_report: mock_query_string_parts.append( urlencode(report_config.filters, True)) mock_query_string_parts.append( urlencode(report_config.get_date_range(), True)) mock_request.GET = QueryDict( '&'.join(mock_query_string_parts)) date_range = report_config.get_date_range() start_date = datetime.strptime(date_range['startdate'], '%Y-%m-%d') end_date = datetime.strptime(date_range['enddate'], '%Y-%m-%d') datespan = DateSpan(start_date, end_date) request_data = vars(mock_request) request_data[ 'couch_user'] = mock_request.couch_user.userID request_data['datespan'] = datespan full_request = { 'request': request_data, 'domain': request_data['domain'], 'context': {}, 'request_params': json_request(request_data['GET']) } export_all_rows_task(report_config.report, full_request, emails, title)