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

"""
5
import urllib
6

7

8
from plugin_dbui import INLINE_ALERT
9
from plugin_event import Graph2, List2, Metric2D2, ReportException
10 11


12 13
def grid():
    """The list is rendered such as an ``Ext.grid.Panel`` widget.
14 15 16 17 18 19 20
    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.
21 22

    """
23
    try:
24 25 26 27
        report = List2(request.vars.id_list)
        store = report.get_store_configuration()
        grid = report.get_grid_configuration()
        title = report.get_title()
28 29 30

    except (IndexError, ReportException, TypeError, ValueError) as e:
        return INLINE_ALERT % ("REPORT Error...", e)
31

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


36
def metric2d():
37
    """The metric 2D is a 2D table rendered by an ``Ext.grid.Panel`` widget
38 39 40 41 42 43 44 45 46 47 48
    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.

    """
49 50
    extension = request.extension

51 52 53 54
    try:
        report = Metric2D2(request.vars.id_metric2d)

        graph = Graph2(report)
55 56 57 58 59 60 61 62 63 64 65 66 67 68
        title = report.get_title()

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

            img = graph.to_svg()
            img = urllib.quote(img)

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

        elif extension == "pdf":
            data = graph.to_pdf()
69
            response.view = "plugin_event/graph.pdf"
70 71 72 73
            return dict(data=data, title=title)

        elif extension == "png":
            data = graph.to_png()
74
            response.view = "plugin_event/graph.png"
75
            return dict(data=data, title=title)
76 77 78

    except (IndexError, ReportException, TypeError, ValueError) as e:
        return INLINE_ALERT % ("REPORT Error...", e)