Esempio n. 1
0
    def get_report_content(self, attach_excel=False):
        """
        Get the report's HTML content as rendered by the static view format.

        """
        try:
            if self.report is None:
                return _(
                    "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

        from django.http import HttpRequest, QueryDict
        request = HttpRequest()
        request.couch_user = self.owner
        request.user = self.owner.get_django_user()
        request.domain = self.domain
        request.couch_user.current_domain = self.domain

        request.GET = QueryDict(self.query_string + '&filterSet=true')

        # Make sure the request gets processed by PRBAC Middleware
        CCHQPRBACMiddleware.process_request(request)

        try:
            response = self._dispatcher.dispatch(request,
                                                 render_as='email',
                                                 **self.view_kwargs)
            if attach_excel is True:
                file_obj = self._dispatcher.dispatch(request,
                                                     render_as='excel',
                                                     **self.view_kwargs)
            else:
                file_obj = None
            return json.loads(response.content)['report'], file_obj
        except PermissionDenied:
            return _("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':
                         "%s%s" %
                         (get_url_base(),
                          reverse('saved_reports', args=[request.domain])),
                     }, None
        except Exception as e:
            notify_exception(None, "Error generating report")
            return _("An error occurred while generating this report."), None
Esempio n. 2
0
    def get_report_content(self, attach_excel=False):
        """
        Get the report's HTML content as rendered by the static view format.

        """
        try:
            if self.report is None:
                return _("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

        from django.http import HttpRequest, QueryDict
        request = HttpRequest()
        request.couch_user = self.owner
        request.user = self.owner.get_django_user()
        request.domain = self.domain
        request.couch_user.current_domain = self.domain

        request.GET = QueryDict(self.query_string + '&filterSet=true')

        # Make sure the request gets processed by PRBAC Middleware
        CCHQPRBACMiddleware.process_request(request)

        try:
            response = self._dispatcher.dispatch(request, render_as='email',
                **self.view_kwargs)
            if attach_excel is True:
                file_obj = self._dispatcher.dispatch(request, render_as='excel',
                **self.view_kwargs)
            else:
                file_obj = None
            return json.loads(response.content)['report'], file_obj
        except PermissionDenied:
            return _("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': "%s%s" % (
                             get_url_base(), reverse(
                                 'saved_reports', args=[request.domain])),
                     }, None
        except Exception as e:
            notify_exception(None, "Error generating report")
            return _("An error occurred while generating this report."), None