Beispiel #1
0
def authentication_exception_handler(exc):
    """ Returns redirect to login page only when requesting HTML. """
    request = get_request()

    if (isinstance(exc, NotAuthenticated) and
            'text/html' in request.negotiator.get_accept_list(request)):
        return HttpResponseRedirect(settings.LOGIN_URL + "?next=" + request.path)

    response = exception_handler(exc)

    return response
Beispiel #2
0
    def process(cls, widget, job, data):
        """Class method to generate list of circle objects.

        Subclass should manipulate this list into specific
        JSON structures as needed.
        """

        request = get_request()
        maps_version = request.user.userprofile.maps_version
        post_process = POST_PROCESS_MAP[maps_version]

        columns = job.get_columns()

        ColInfo = namedtuple('ColInfo', ['col', 'dataindex'])

        keycol = None
        latcol = None
        longcol = None
        valuecol = None
        labelcol = None
        for i, c in enumerate(columns):
            if c.name == widget.options.key:
                keycol = ColInfo(c, i)
            elif c.name == widget.options.latitude:
                latcol = ColInfo(c, i)
            elif c.name == widget.options.longitude:
                longcol = ColInfo(c, i)
            elif c.name == widget.options.value:
                valuecol = ColInfo(c, i)
            elif c.name == widget.options.label:
                labelcol = ColInfo(c, i)

        # Array of circle objects for each data row
        Circle = namedtuple('Circle',
                            ['title', 'lat', 'long', 'value', 'size',
                             'units', 'formatter'])
        circles = []

        if data:
            valmax = None
            if valuecol.col.isnumeric:
                values = zip(*data)[valuecol.dataindex]
                filtered = filter(bool, values)
                if filtered:
                    valmax = max(filtered)
                else:
                    valmax = 1

            geolookup = None

            if valuecol.col.datatype == 'bytes':
                formatter = 'formatBytes'
            elif valuecol.col.datatype == 'metric':
                formatter = 'formatMetric'
            else:
                formatter = None

            for rawrow in data:
                val = rawrow[valuecol.dataindex]

                # skip empty result values which are not explicitly zero
                if val is None or val == '':
                    continue

                if valmax:
                    marker_size = 15 * (val / valmax)
                else:
                    marker_size = 5

                if keycol:
                    key = rawrow[keycol.dataindex]

                    # XXXCJ - this is a hack for Profiler based host groups,
                    # need to generalize this, probably via options
                    if widget.table().options['groupby'] == 'host_group':
                        try:
                            geo = Location.objects.get(name=key)
                        except ObjectDoesNotExist:
                            continue
                    else:
                        # Perform geolookup on the key
                        # (should be an IP address...)
                        if geolookup is None:
                            geolookup = Lookup.instance()
                        geo = geolookup.lookup(key)

                    if geo:
                        # Found a match, create a Circle
                        circle = Circle(title=geo.name,
                                        lat=geo.latitude,
                                        long=geo.longitude,
                                        value=val,
                                        size=marker_size,
                                        units=valuecol.col.units,
                                        formatter=formatter)
                        circles.append(circle)
                else:
                    # use lat/long columns instead of lookups
                    lat = rawrow[latcol.dataindex]
                    long = rawrow[longcol.dataindex]
                    title = rawrow[labelcol.dataindex] if labelcol else val

                    circle = Circle(title=title,
                                    lat=lat,
                                    long=long,
                                    value=val,
                                    size=marker_size,
                                    units=valuecol.col.units,
                                    formatter=formatter)
                    circles.append(circle)

        else:
            # no data just return empty circles list
            pass

        data = {
            "chartTitle": widget.title.format(**job.actual_criteria),
            "circles": circles,
            "minbounds": widget.options.min_bounds
        }

        return post_process(data)
Beispiel #3
0
    def process(cls, widget, job, data):
        """Class method to generate list of circle objects.

        Subclass should manipulate this list into specific
        JSON structures as needed.
        """

        request = get_request()
        maps_version = request.user.userprofile.maps_version
        post_process = POST_PROCESS_MAP[maps_version]

        columns = job.get_columns()

        ColInfo = namedtuple('ColInfo', ['col', 'dataindex'])

        keycol = None
        latcol = None
        longcol = None
        valuecol = None
        labelcol = None
        for i, c in enumerate(columns):
            if c.name == widget.options.key:
                keycol = ColInfo(c, i)
            elif c.name == widget.options.latitude:
                latcol = ColInfo(c, i)
            elif c.name == widget.options.longitude:
                longcol = ColInfo(c, i)
            elif c.name == widget.options.value:
                valuecol = ColInfo(c, i)
            elif c.name == widget.options.label:
                labelcol = ColInfo(c, i)

        # Array of circle objects for each data row
        Circle = namedtuple(
            'Circle',
            ['title', 'lat', 'long', 'value', 'size', 'units', 'formatter'])
        circles = []

        if data:
            valmax = None
            if valuecol.col.isnumeric:
                values = zip(*data)[valuecol.dataindex]
                filtered = filter(bool, values)
                if filtered:
                    valmax = max(filtered)
                else:
                    valmax = 1

            geolookup = None

            if valuecol.col.datatype == 'bytes':
                formatter = 'formatBytes'
            elif valuecol.col.datatype == 'metric':
                formatter = 'formatMetric'
            else:
                formatter = None

            for rawrow in data:
                val = rawrow[valuecol.dataindex]

                # skip empty result values which are not explicitly zero
                if val is None or val == '':
                    continue

                if valmax:
                    marker_size = 15 * (val / valmax)
                else:
                    marker_size = 5

                if keycol:
                    key = rawrow[keycol.dataindex]

                    # XXXCJ - this is a hack for Profiler based host groups,
                    # need to generalize this, probably via options
                    if widget.table().options['groupby'] == 'host_group':
                        try:
                            geo = Location.objects.get(name=key)
                        except ObjectDoesNotExist:
                            continue
                    else:
                        # Perform geolookup on the key
                        # (should be an IP address...)
                        if geolookup is None:
                            geolookup = Lookup.instance()
                        geo = geolookup.lookup(key)

                    if geo:
                        # Found a match, create a Circle
                        circle = Circle(title=geo.name,
                                        lat=geo.latitude,
                                        long=geo.longitude,
                                        value=val,
                                        size=marker_size,
                                        units=valuecol.col.units,
                                        formatter=formatter)
                        circles.append(circle)
                else:
                    # use lat/long columns instead of lookups
                    lat = rawrow[latcol.dataindex]
                    long = rawrow[longcol.dataindex]
                    title = rawrow[labelcol.dataindex] if labelcol else val

                    circle = Circle(title=title,
                                    lat=lat,
                                    long=long,
                                    value=val,
                                    size=marker_size,
                                    units=valuecol.col.units,
                                    formatter=formatter)
                    circles.append(circle)

        else:
            # no data just return empty circles list
            pass

        data = {
            "chartTitle": widget.title.format(**job.actual_criteria),
            "circles": circles,
            "minbounds": widget.options.min_bounds
        }

        return post_process(data)