plugin_event.py 3.62 KB
Newer Older
1
# -*- coding: utf-8 -*-
2
"""plugin_event controllers
3 4

"""
5
import json
6
import traceback
7
import urllib
8

9

10 11 12 13 14 15 16 17 18 19 20 21 22 23
from gluon.storage import Storage
from plugin_dbui import Selector, Store
from plugin_event import (Event,
                          Graph,
                          List,
                          Metric2D,
                          ReportException,
                          Source)


FIELD_TYPES = {"gridcolumn": "string",
               "booleancolumn": "boolean",
               "datecolumn": "date",
               "numbercolumn": "float"}
24 25


26 27
def grid():
    """The list is rendered such as an ``Ext.grid.Panel`` widget.
28 29 30 31 32 33 34
    Its context menu allows to export the content of the table as a CSV, LaTeX
    or PDF format.

    The controller extracts the configuration of the report from the database.
    It builds the DataFrame, extracts the configuration for the
    ``Ext.data.Store`` as well as the ``Ext.grid.Panel``. The former contains
    the data displayed in the grid.
35 36

    """
37
    try:
38
        report = List(request.vars.id_list)
39 40 41
        store = report.get_store_configuration()
        grid = report.get_grid_configuration()
        title = report.get_title()
42

43 44
    except (IndexError, ReportException, TypeError, ValueError):
        return CODE(traceback.format_exc()).xml()
45

46
    response.view = "plugin_event/grid.html"
47
    return dict(cfg_store=store, grid=grid, title=title)
48 49


50
def metric2d():
51
    """The metric 2D is a 2D table rendered by an ``Ext.grid.Panel`` widget
52 53 54 55 56 57 58 59 60 61 62
    and a graph rendered as an image. The context menu associated to the grid
    allows to export the content of the table as a CSV, LaTeX or PDF format.
    The context menu associated to the image allow to export in PNG or PDF
    format.

    The controller extracts the configuration of the report from the database.
    It builds the DataFrame, extracts the the configuration for the
    ``Ext.data.Store`` as well as the ``Ext.grid.Panel`` and generated
    the graph.

    """
63 64
    extension = request.extension

65
    try:
66
        report = Metric2D(request.vars.id_metric2d)
67

68
        graph = Graph(report)
69 70 71 72 73 74
        title = report.get_title()

        if extension == "html":
            store = report.get_store_configuration()
            grid = report.get_grid_configuration()

75 76 77
            if graph.ax is not None:
                img = graph.to_svg()
                img = urllib.quote(img)
78

79 80 81 82 83 84
                response.view = "plugin_event/grid_and_graph.html"
                return dict(cfg_store=store, grid=grid, img=img, title=title)

            else:
                response.view = "plugin_event/grid.html"
                return dict(cfg_store=store, grid=grid, title=title)
85 86 87

        elif extension == "pdf":
            data = graph.to_pdf()
88
            response.view = "plugin_event/graph.pdf"
89 90 91 92
            return dict(data=data, title=title)

        elif extension == "png":
            data = graph.to_png()
93
            response.view = "plugin_event/graph.png"
94
            return dict(data=data, title=title)
95

96 97
    except (IndexError, ReportException, TypeError, ValueError):
        return CODE(traceback.format_exc()).xml()
98 99 100 101


def source():
    """Display the content of a source via an ``Ext.grid.Panel``.
102
    Values send by the selector are used to filter the content of the source.
103 104

    """
105 106 107 108 109 110 111 112 113 114 115
    try:
        report = Source(request.vars.source)
        store = report.get_store_configuration()
        grid = report.get_grid_configuration()
        title = report.get_title()

    except (IndexError, ReportException, TypeError, ValueError):
        return CODE(traceback.format_exc()).xml()

    response.view = "plugin_event/grid.html"
    return dict(cfg_store=store, grid=grid, title=title)