Beispiel #1
0
    def post(self, request, pk):
        """Create new Job for the specified table using POSTed criteria."""
        table = Table.objects.get(pk=pk)
        all_fields = dict((f.keyword, f) for f in table.fields.all())

        # data needs to be not-None or form will be created as unbound
        data = self.request.POST or {}
        form = TableFieldForm(all_fields,
                              use_widgets=False,
                              include_hidden=True,
                              data=data)
        if form.is_valid(check_unknown=True):
            criteria = form.criteria()
        else:
            return Response(form.errors, status=status.HTTP_400_BAD_REQUEST)

        try:
            job = Job.create(table, criteria)
            job.start()
            serializer = JobSerializer(job, many=False)
            return Response(serializer.data,
                            status=status.HTTP_201_CREATED,
                            headers=self.get_success_headers(job))
        except Exception as e:
            msg = 'Error processing Job: %s' % e.message
            raise JobCreationError(msg)
    def post(self, request, pk):
        """Create new Job for the specified table using POSTed criteria."""
        table = Table.objects.get(pk=pk)
        all_fields = dict((f.keyword, f) for f in table.fields.all())

        # data needs to be not-None or form will be created as unbound
        data = self.request.POST or {}
        form = TableFieldForm(all_fields, use_widgets=False,
                              data=data)
        if form.is_valid(check_unknown=True):
            criteria = form.criteria()
        else:
            return Response(form.errors, status=status.HTTP_400_BAD_REQUEST)

        try:
            job = Job.create(table, criteria)
            job.start()
            serializer = JobSerializer(job, many=False)
            return Response(serializer.data, status=status.HTTP_201_CREATED,
                            headers=self.get_success_headers(job))
        except Exception as e:
            msg = 'Error processing Job: %s' % e.message
            raise JobCreationError(msg)
Beispiel #3
0
    def post(self, request, namespace, report_slug, widget_slug, format=None):
        logger.debug("Received POST for report %s, widget %s: %s" %
                     (report_slug, widget_slug, request.POST))

        report = get_object_or_404(Report, namespace=namespace,
                                   slug=report_slug)

        widget = get_object_or_404(
            Widget,
            slug=widget_slug,
            section__in=Section.objects.filter(report=report)
        )

        req_json = json.loads(request.POST['criteria'])

        fields = widget.collect_fields()
        form = TableFieldForm(fields, use_widgets=False,
                              hidden_fields=report.hidden_fields,
                              include_hidden=True,
                              data=req_json, files=request.FILES)

        if not form.is_valid():
            raise ValueError("Widget internal criteria form is invalid:\n%s" %
                             (form.errors.as_text()))

        if form.is_valid():
            logger.debug('Form passed validation: %s' % form)
            formdata = form.cleaned_data
            logger.debug('Form cleaned data: %s' % formdata)

            # parse time and localize to user profile timezone
            timezone = get_timezone(request)
            form.apply_timezone(timezone)

            try:
                form_criteria = form.criteria()
                logger.debug('Form_criteria: %s' % form_criteria)
                job = Job.create(table=widget.table(),
                                 criteria=form_criteria)
                job.start()

                wjob = WidgetJob(widget=widget, job=job)
                wjob.save()

                logger.debug("Created WidgetJob %s for report %s (handle %s)" %
                             (str(wjob), report_slug, job.handle))

                return Response({"joburl": reverse('report-job-detail',
                                                   args=[namespace,
                                                         report_slug,
                                                         widget_slug,
                                                         wjob.id])})
            except Exception as e:
                logger.exception("Failed to start job, an exception occurred")
                ei = sys.exc_info()
                resp = {}
                resp['message'] = "".join(
                    traceback.format_exception_only(*sys.exc_info()[0:2])),
                resp['exception'] = "".join(
                    traceback.format_exception(*sys.exc_info()))

                return JsonResponse(resp, status=400)

        else:
            logger.error("form is invalid, entering debugger")
            from IPython import embed; embed()
Beispiel #4
0
def create_report_history(request, report, widgets):
    """Create a report history object.

    :param request: request object
    :param report: Report object
    :param widgets: List of widget definitions
    """

    # create the form to derive criteria for bookmark only
    # the form in the calling context can not be used
    # because it does not include hidden fields
    fields_by_section = report.collect_fields_by_section()
    all_fields = SortedDict()
    [all_fields.update(c) for c in fields_by_section.values()]

    form = TableFieldForm(all_fields,
                          hidden_fields=report.hidden_fields,
                          include_hidden=True,
                          data=request.POST,
                          files=request.FILES)

    # parse time and localize to user profile timezone
    timezone = get_timezone(request)
    form.apply_timezone(timezone)

    form_data = form.cleaned_data

    url = request._request.path + '?'

    # form_data contains fields that don't belong to url
    # e.g. ignore_cache, debug
    # thus use compute_field_precedence method to filter those
    table_fields = {k: form_data[k] for k in form.compute_field_precedence()}

    # Convert field values into strings suitable for bookmark
    def _get_url_fields(flds):
        for k, v in flds.iteritems():
            if k in ['starttime', 'endtime']:
                yield (k, str(datetime_to_seconds(v)))
            elif k in ['duration', 'resolution']:
                try:
                    yield (k, str(int(timedelta_total_seconds(v))))
                except AttributeError:
                    # v is of special value, not a string of some duration
                    yield (k, v.replace(' ', '+'))
            else:
                # use + as encoded white space
                yield (k, str(v).replace(' ', '+'))
        yield ('auto_run', 'true')

    url_fields = ['='.join([k, v]) for k, v in _get_url_fields(table_fields)]

    # Form the bookmark link
    url += '&'.join(url_fields)

    last_run = datetime.datetime.now(timezone)

    # iterate over the passed in widget definitions and use those
    # to calculate the actual job handles
    # since these will mimic what gets used to create the actual jobs,
    # the criteria will match more closely than using the report-level
    # criteria data
    handles = []
    for widget in widgets:
        wobj = Widget.objects.get(
            slug=widget['widgetslug'],
            section__in=Section.objects.filter(report=report)
        )

        fields = wobj.collect_fields()
        form = TableFieldForm(fields, use_widgets=False,
                              hidden_fields=report.hidden_fields,
                              include_hidden=True,
                              data=widget['criteria'], files=request.FILES)

        if form.is_valid():
            # parse time and localize to user profile timezone
            timezone = get_timezone(request)
            form.apply_timezone(timezone)

            form_criteria = form.criteria()
            widget_table = wobj.table()
            form_criteria = form_criteria.build_for_table(widget_table)
            try:
                form_criteria.compute_times()
            except ValueError:
                pass

            handle = Job._compute_handle(widget_table, form_criteria)
            logger.debug('ReportHistory: adding handle %s for widget_table %s'
                         % (handle, widget_table))
            handles.append(handle)

        else:
            # log error, but don't worry about it for adding to RH
            logger.warning("Error while calculating job handle for Widget %s, "
                           "internal criteria form is invalid: %s" %
                           (wobj, form.errors.as_text()))

    job_handles = ','.join(handles)

    if request.user.is_authenticated():
        user = request.user.username
    else:
        user = settings.GUEST_USER_NAME

    logger.debug('Creating ReportHistory for user %s at URL %s' % (user, url))

    ReportHistory.create(namespace=report.namespace,
                         slug=report.slug,
                         bookmark=url,
                         first_run=last_run,
                         last_run=last_run,
                         job_handles=job_handles,
                         user=user,
                         criteria=table_fields,
                         run_count=1)
Beispiel #5
0
    def post(self, request, namespace, report_slug, widget_slug, format=None):
        logger.debug("Received POST for report %s, widget %s: %s" %
                     (report_slug, widget_slug, request.POST))

        report = get_object_or_404(Report,
                                   namespace=namespace,
                                   slug=report_slug)

        widget = get_object_or_404(
            Widget,
            slug=widget_slug,
            section__in=Section.objects.filter(report=report))

        req_json = json.loads(request.POST['criteria'])

        fields = widget.collect_fields()
        form = TableFieldForm(fields,
                              use_widgets=False,
                              hidden_fields=report.hidden_fields,
                              include_hidden=True,
                              data=req_json,
                              files=request.FILES)

        if not form.is_valid():
            raise ValueError("Widget internal criteria form is invalid:\n%s" %
                             (form.errors.as_text()))

        if form.is_valid():
            logger.debug('Form passed validation: %s' % form)
            formdata = form.cleaned_data
            logger.debug('Form cleaned data: %s' % formdata)

            # parse time and localize to user profile timezone
            timezone = pytz.timezone(request.user.timezone)
            form.apply_timezone(timezone)

            try:
                form_criteria = form.criteria()
                logger.debug('Form_criteria: %s' % form_criteria)
                job = Job.create(table=widget.table(), criteria=form_criteria)
                job.start()

                wjob = WidgetJob(widget=widget, job=job)
                wjob.save()

                logger.debug("Created WidgetJob %s for report %s (handle %s)" %
                             (str(wjob), report_slug, job.handle))

                return Response({
                    "joburl":
                    reverse(
                        'report-job-detail',
                        args=[namespace, report_slug, widget_slug, wjob.id])
                })
            except Exception as e:
                logger.exception("Failed to start job, an exception occurred")
                ei = sys.exc_info()
                resp = {}
                resp['message'] = "".join(
                    traceback.format_exception_only(*sys.exc_info()[0:2])),
                resp['exception'] = "".join(
                    traceback.format_exception(*sys.exc_info()))

                return JsonResponse(resp, status=400)

        else:
            logger.error("form is invalid, entering debugger")
            from IPython import embed
            embed()
Beispiel #6
0
def create_report_history(request, report, widgets):
    """Create a report history object.

    :param request: request object
    :param report: Report object
    :param widgets: List of widget definitions
    """

    # create the form to derive criteria for bookmark only
    # the form in the calling context can not be used
    # because it does not include hidden fields
    fields_by_section = report.collect_fields_by_section()
    all_fields = OrderedDict()
    [all_fields.update(c) for c in fields_by_section.values()]

    form = TableFieldForm(all_fields,
                          hidden_fields=report.hidden_fields,
                          include_hidden=True,
                          data=request.POST,
                          files=request.FILES)

    # parse time and localize to user profile timezone
    timezone = get_timezone(request)
    form.apply_timezone(timezone)

    form_data = form.cleaned_data

    url = request._request.path + '?'

    # form_data contains fields that don't belong to url
    # e.g. ignore_cache, debug
    # thus use compute_field_precedence method to filter those
    table_fields = {k: form_data[k] for k in form.compute_field_precedence()}

    # Convert field values into strings suitable for bookmark
    def _get_url_fields(flds):
        for k, v in flds.iteritems():
            if k in ['starttime', 'endtime']:
                yield (k, str(datetime_to_seconds(v)))
            elif k in ['duration', 'resolution']:
                try:
                    yield (k, str(int(timedelta_total_seconds(v))))
                except AttributeError:
                    # v is of special value, not a string of some duration
                    yield (k, v.replace(' ', '+'))
            else:
                # use + as encoded white space
                yield (k, str(v).replace(' ', '+'))
        yield ('auto_run', 'true')

    url_fields = ['='.join([k, v]) for k, v in _get_url_fields(table_fields)]

    # Form the bookmark link
    url += '&'.join(url_fields)

    last_run = datetime.datetime.now(timezone)

    # iterate over the passed in widget definitions and use those
    # to calculate the actual job handles
    # since these will mimic what gets used to create the actual jobs,
    # the criteria will match more closely than using the report-level
    # criteria data
    handles = []
    for widget in widgets:
        wobj = Widget.objects.get(
            slug=widget['widgetslug'],
            section__in=Section.objects.filter(report=report)
        )

        fields = wobj.collect_fields()
        form = TableFieldForm(fields, use_widgets=False,
                              hidden_fields=report.hidden_fields,
                              include_hidden=True,
                              data=widget['criteria'], files=request.FILES)

        if form.is_valid():
            # parse time and localize to user profile timezone
            timezone = get_timezone(request)
            form.apply_timezone(timezone)

            form_criteria = form.criteria()
            widget_table = wobj.table()
            form_criteria = form_criteria.build_for_table(widget_table)
            try:
                form_criteria.compute_times()
            except ValueError:
                pass

            handle = Job._compute_handle(widget_table, form_criteria)
            logger.debug('ReportHistory: adding handle %s for widget_table %s'
                         % (handle, widget_table))
            handles.append(handle)

        else:
            # log error, but don't worry about it for adding to RH
            logger.warning("Error while calculating job handle for Widget %s, "
                           "internal criteria form is invalid: %s" %
                           (wobj, form.errors.as_text()))

    job_handles = ','.join(handles)

    if request.user.is_authenticated():
        user = request.user.username
    else:
        user = settings.GUEST_USER_NAME

    logger.debug('Creating ReportHistory for user %s at URL %s' % (user, url))

    ReportHistory.create(namespace=report.namespace,
                         slug=report.slug,
                         bookmark=url,
                         first_run=last_run,
                         last_run=last_run,
                         job_handles=job_handles,
                         user=user,
                         criteria=table_fields,
                         run_count=1)