def get_report_export_request(self):
     try:
         self.report_export_request = self.report_request.exports.get(
             format=self.kwargs['output'])
     except ReportRequestExport.DoesNotExist:
         self.report_export_request = ReportRequestExport(
             report_request=self.report_request,
             format=self.kwargs['output'],
             token=(self.report_request.token + self.kwargs['output']),
         )
         self.report_export_request.save()
         #TODO if the parent report is done and has under a certain number of rows, then no async is needed
         #however if opting the no-async route then it may not be necessary to create this object and upload the result to s3
         if self.asynchronous_report:
             self.task = self.report_export_request.schedule_task()
         else:
             self.report_export_request.build_report()
Esempio n. 2
0
 def get_report_export_request(self):
     try:
         self.report_export_request = self.report_request.exports.get(format=self.kwargs['output'])
     except ReportRequestExport.DoesNotExist:
         self.report_export_request = ReportRequestExport(report_request=self.report_request,
                                                          format=self.kwargs['output'],
                                                          token=(self.report_request.token + self.kwargs['output']),)
         self.report_export_request.save()
         #TODO if the parent report is done and has under a certain number of rows, then no async is needed
         #however if opting the no-async route then it may not be necessary to create this object and upload the result to s3
         if self.asynchronous_report:
             self.task = self.report_export_request.schedule_task()
         else:
             self.report_export_request.build_report()
class ReportExportView(TemplateView, RequestReportMixin):
    asynchronous_report = ASYNC_REPORTS

    def get_report_request(self):
        token = self.kwargs['token']
        self.report_request = ReportRequest.objects.get(token=token)
        self.report = self.report_request.get_report()
        ReportRequest.objects.filter(pk=self.report_request.pk).update(
            viewed_on=datetime.datetime.now())

    def get_report_export_request(self):
        try:
            self.report_export_request = self.report_request.exports.get(
                format=self.kwargs['output'])
        except ReportRequestExport.DoesNotExist:
            self.report_export_request = ReportRequestExport(
                report_request=self.report_request,
                format=self.kwargs['output'],
                token=(self.report_request.token + self.kwargs['output']),
            )
            self.report_export_request.save()
            #TODO if the parent report is done and has under a certain number of rows, then no async is needed
            #however if opting the no-async route then it may not be necessary to create this object and upload the result to s3
            if self.asynchronous_report:
                self.task = self.report_export_request.schedule_task()
            else:
                self.report_export_request.build_report()

    def check_report_export_status(self):
        #check to see if the report is not complete but async is off
        if not self.report_export_request.completion_timestamp and (
                not self.asynchronous_report
                or getattr(settings, 'CELERY_ALWAYS_EAGER', False)):
            self.report_export_request.build_report()
            assert self.report_export_request.completion_timestamp

        #check to see if the task failed
        if self.report_export_request.task_status() in ('FAILURE', ):
            return {'error': 'Task Failed', 'completed': False}

        return {
            'completed': bool(self.report_export_request.completion_timestamp)
        }

    def get(self, request, *args, **kwargs):
        try:
            self.get_report_request()
        except ReportRequest.DoesNotExist:
            raise Http404()
        status = self.check_report_status()
        if 'error' in status:  #there was an error, try recreating the report
            #CONSIDER add max retries
            return HttpResponseRedirect(self.report_request.get_report_url())
        if not status['completed']:
            assert self.asynchronous_report
            cx = {
                "report_request": self.report_request,
                "report": self.report,
                'title': self.report.verbose_name,
                'format': self.kwargs['output'],
            }
            print cx
            return render_to_response("reportengine/async_wait.html",
                                      cx,
                                      context_instance=RequestContext(
                                          self.request))

        #if the report is small enough there is no need to create a task to export
        if self.report_request.rows.all().count() <= MAX_ROWS_FOR_QUICK_EXPORT:
            return ReportView.as_view()(self.request, *self.args,
                                        **self.kwargs)

        self.get_report_export_request()
        status = self.check_report_export_status()
        if 'error' in status:  #there was an error, try recreating the report
            #CONSIDER add max retries
            return HttpResponseRedirect(self.report_request.get_report_url())
        if not status['completed']:
            assert self.asynchronous_report
            cx = {
                "report_request": self.report_request,
                "report": self.report,
                'title': self.report.verbose_name,
                'format': self.kwargs['output'],
            }
            return render_to_response("reportengine/async_wait.html",
                                      cx,
                                      context_instance=RequestContext(
                                          self.request))
        return HttpResponseRedirect(self.report_export_request.payload.url)
Esempio n. 4
0
class ReportExportView(TemplateView, RequestReportMixin):
    asynchronous_report = ASYNC_REPORTS
    
    def get_report_request(self):
        token = self.kwargs['token']
        self.report_request = ReportRequest.objects.get(token=token)
        self.report = self.report_request.get_report()
        ReportRequest.objects.filter(pk=self.report_request.pk).update(viewed_on=datetime.datetime.now())
    
    def get_report_export_request(self):
        try:
            self.report_export_request = self.report_request.exports.get(format=self.kwargs['output'])
        except ReportRequestExport.DoesNotExist:
            self.report_export_request = ReportRequestExport(report_request=self.report_request,
                                                             format=self.kwargs['output'],
                                                             token=(self.report_request.token + self.kwargs['output']),)
            self.report_export_request.save()
            #TODO if the parent report is done and has under a certain number of rows, then no async is needed
            #however if opting the no-async route then it may not be necessary to create this object and upload the result to s3
            if self.asynchronous_report:
                self.task = self.report_export_request.schedule_task()
            else:
                self.report_export_request.build_report()
    
    def check_report_export_status(self):
        #check to see if the report is not complete but async is off
        if not self.report_export_request.completion_timestamp and (not self.asynchronous_report or getattr(settings, 'CELERY_ALWAYS_EAGER', False)):
            self.report_export_request.build_report()
            assert self.report_export_request.completion_timestamp
        
        #check to see if the task failed
        if self.report_export_request.task_status() in ('FAILURE',):
            return {'error':'Task Failed', 'completed':False}
        
        return {'completed':bool(self.report_export_request.completion_timestamp)}
    
    def get(self, request, *args, **kwargs):
        try:
            self.get_report_request()
        except ReportRequest.DoesNotExist:
            raise Http404()
        status = self.check_report_status()
        if 'error' in status: #there was an error, try recreating the report
            #CONSIDER add max retries
            return HttpResponseRedirect(self.report_request.get_report_url())
        if not status['completed']:
            assert self.asynchronous_report
            cx = {"report_request":self.report_request,
                  "report":self.report,
                  'title':self.report.verbose_name,
                  'format':self.kwargs['output'],}
            print(cx)
            return render_to_response("reportengine/async_wait.html",
                                      cx,
                                      context_instance=RequestContext(self.request))
        
        #if the report is small enough there is no need to create a task to export
        if self.report_request.rows.all().count() <=  MAX_ROWS_FOR_QUICK_EXPORT:
            return ReportView.as_view()(self.request, *self.args, **self.kwargs)
        
        self.get_report_export_request()
        status = self.check_report_export_status()
        if 'error' in status: #there was an error, try recreating the report
            #CONSIDER add max retries
            return HttpResponseRedirect(self.report_request.get_report_url())
        if not status['completed']:
            assert self.asynchronous_report
            cx = {"report_request":self.report_request,
                  "report":self.report,
                  'title':self.report.verbose_name,
                  'format':self.kwargs['output'],}
            return render_to_response("reportengine/async_wait.html",
                                      cx,
                                      context_instance=RequestContext(self.request))
        return HttpResponseRedirect(self.report_export_request.payload.url)