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

"""
5
import traceback
6
import urllib
7

8

9
from plugin_event import (Graph,
10 11 12 13 14 15
                          List,
                          Metric2D,
                          ReportException,
                          Source)


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

    """
27
    try:
28
        report = List(request.vars.id_list)
29 30 31
        store = report.get_store_configuration()
        grid = report.get_grid_configuration()
        title = report.get_title()
32

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

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


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

    """
53 54
    extension = request.extension

55
    try:
56
        report = Metric2D(request.vars.id_metric2d)
57

58
        graph = Graph(report)
59 60 61 62 63 64
        title = report.get_title()

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

65 66 67
            if graph.ax is not None:
                img = graph.to_svg()
                img = urllib.quote(img)
68

69 70 71 72 73 74
                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)
75 76 77

        elif extension == "pdf":
            data = graph.to_pdf()
78
            response.view = "plugin_event/graph.pdf"
79 80 81 82
            return dict(data=data, title=title)

        elif extension == "png":
            data = graph.to_png()
83
            response.view = "plugin_event/graph.png"
84
            return dict(data=data, title=title)
85

86 87
    except (IndexError, ReportException, TypeError, ValueError):
        return CODE(traceback.format_exc()).xml()
88 89 90 91


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

    """
95 96 97 98 99 100 101 102 103 104 105
    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)