(self, lang, attach_excel=False, couch_user=None)
Get the report's HTML content as rendered by the static view format.
Get the report's HTML content as rendered by the static view format.
Get the report s HTML content as rendered by the static view format .
def get_report_content(self, lang, attach_excel=False, couch_user=None):
        """
        Get the report's HTML content as rendered by the static view format.

        """
        from corehq.apps.locations.middleware import LocationAccessMiddleware

        if couch_user is None:
            couch_user = self.owner

        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 = couch_user
        mock_request.user = couch_user.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']
        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 , couch_user = None ) : from corehq . apps . locations . middleware import LocationAccessMiddleware if couch_user is None : couch_user = self . owner 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 = couch_user mock_request . user = couch_user . 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' ] 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 , )