def get_report_content(self, lang, attach_excel=False): """ Get the report's HTML content as rendered by the static view format. """ from corehq.apps.locations.middleware import LocationAccessMiddleware try: if self.report is None: return ReportContent( _("The report used to create this scheduled report is no" " longer available on CommCare HQ. Please delete this" " scheduled report and create a new one using an available" " report."), None, ) except Exception: pass if getattr(self.report, 'is_deprecated', False): return ReportContent( self.report.deprecation_email_message or _("[DEPRECATED] %s report has been deprecated and will stop working soon. " "Please update your saved reports email settings if needed." % self.report.name), None, ) 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 = lang mock_request.method = 'GET' mock_request.bypass_two_factor = True mock_query_string_parts = [self.query_string, 'filterSet=true'] if self.is_configurable_report: mock_query_string_parts.append(urlencode(self.filters, True)) mock_query_string_parts.append( urlencode(self.get_date_range(), True)) mock_request.GET = QueryDict('&'.join(mock_query_string_parts)) # Make sure the request gets processed by PRBAC Middleware CCHQPRBACMiddleware.apply_prbac(mock_request) LocationAccessMiddleware.apply_location_access(mock_request) try: dispatch_func = functools.partial( self._dispatcher.__class__.as_view(), mock_request, **self.view_kwargs) email_response = dispatch_func(render_as='email') if email_response.status_code == 302: return ReportContent( _("We are sorry, but your saved report '%(config_name)s' " "is no longer accessible because the owner %(username)s " "is no longer active.") % { 'config_name': self.name, 'username': self.owner.username }, None, ) try: content_json = json.loads(email_response.content) except ValueError: email_text = email_response.content else: email_text = content_json['report'] excel_attachment = dispatch_func( render_as='excel') if attach_excel else None return ReportContent(email_text, excel_attachment) except PermissionDenied: return ReportContent( _("We are sorry, but your saved report '%(config_name)s' " "is no longer accessible because your subscription does " "not allow Custom Reporting. Please talk to your Project " "Administrator about enabling Custom Reports. If you " "want CommCare HQ to stop sending this message, please " "visit %(saved_reports_url)s to remove this " "Emailed Report.") % { 'config_name': self.name, 'saved_reports_url': absolute_reverse('saved_reports', args=[mock_request.domain]), }, None, ) except Http404: return ReportContent( _("We are sorry, but your saved report '%(config_name)s' " "can not be generated since you do not have the correct permissions. " "Please talk to your Project Administrator about getting permissions for this" "report.") % { 'config_name': self.name, }, None, ) except UnsupportedSavedReportError: return ReportContent( _("We are sorry, but your saved report '%(config_name)s' " "is no longer available. If you think this is a mistake, please report an issue." ) % { 'config_name': self.name, }, None, )
def get_report_content(self, lang, attach_excel=False): """ Get the report's HTML content as rendered by the static view format. """ from corehq.apps.locations.middleware import LocationAccessMiddleware try: if self.report is None: return ReportContent( _("The report used to create this scheduled report is no" " longer available on CommCare HQ. Please delete this" " scheduled report and create a new one using an available" " report."), None, ) except Exception: pass if getattr(self.report, 'is_deprecated', False): return ReportContent( self.report.deprecation_email_message or _("[DEPRECATED] %s report has been deprecated and will stop working soon. " "Please update your saved reports email settings if needed." % self.report.name ), None, ) from django.http import HttpRequest, QueryDict 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 = lang mock_request.method = 'GET' mock_request.bypass_two_factor = True mock_query_string_parts = [self.query_string, 'filterSet=true'] if self.is_configurable_report: mock_query_string_parts.append(urlencode(self.filters, True)) mock_query_string_parts.append(urlencode(self.get_date_range(), True)) mock_request.GET = QueryDict('&'.join(mock_query_string_parts)) # Make sure the request gets processed by PRBAC Middleware CCHQPRBACMiddleware.apply_prbac(mock_request) LocationAccessMiddleware.apply_location_access(mock_request) try: dispatch_func = functools.partial(self._dispatcher.__class__.as_view(), mock_request, **self.view_kwargs) email_response = dispatch_func(render_as='email') if email_response.status_code == 302: return ReportContent( _( "We are sorry, but your saved report '%(config_name)s' " "is no longer accessible because the owner %(username)s " "is no longer active." ) % { 'config_name': self.name, 'username': self.owner.username }, None, ) return ReportContent( json.loads(email_response.content)['report'], dispatch_func(render_as='excel') if attach_excel else None, ) except PermissionDenied: return ReportContent( _( "We are sorry, but your saved report '%(config_name)s' " "is no longer accessible because your subscription does " "not allow Custom Reporting. Please talk to your Project " "Administrator about enabling Custom Reports. If you " "want CommCare HQ to stop sending this message, please " "visit %(saved_reports_url)s to remove this " "Emailed Report." ) % { 'config_name': self.name, 'saved_reports_url': absolute_reverse( 'saved_reports', args=[mock_request.domain] ), }, None, ) except Http404: return ReportContent( _( "We are sorry, but your saved report '%(config_name)s' " "can not be generated since you do not have the correct permissions. " "Please talk to your Project Administrator about getting permissions for this" "report." ) % { 'config_name': self.name, }, None, ) except UnsupportedSavedReportError: return ReportContent( _( "We are sorry, but your saved report '%(config_name)s' " "is no longer available. If you think this is a mistake, please report an issue." ) % { 'config_name': self.name, }, None, ) except Exception: notify_exception(None, "Error generating report: {}".format(self.report_slug), details={ 'domain': self.domain, 'user': self.owner.username, 'report': self.report_slug, 'report config': self.get_id }) return ReportContent(_("An error occurred while generating this report."), None)