Commit ce049b2e authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

Add the controller source and the class Source.

parent 55f235a2
......@@ -2,11 +2,25 @@
"""plugin_event controllers
"""
import json
import traceback
import urllib
from plugin_event import Graph, List, Metric2D, ReportException
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"}
def grid():
......@@ -85,7 +99,17 @@ def metric2d():
def source():
"""Display the content of a source via an ``Ext.grid.Panel``.
Value send by the selector are used to filter the content of the source.
Values send by the selector are used to filter the content of the source.
"""
return "Hello"
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)
......@@ -51,7 +51,8 @@ from report_tools import (BaseReport,
Graph,
List,
Metric2D,
ReportException)
ReportException,
Source)
from ui_core import CoreUi
from ui_report import ReportUi
......
......@@ -71,7 +71,7 @@ class Event(object):
raise EventException("Plugin event sources are not configured.")
if name not in sources:
raise EventException("The source % is not registered" % name)
raise EventException("The source %s is not registered" % name)
return sources[name].func
......
......@@ -6,6 +6,8 @@ import json
import matplotlib as mpl
import pandas as pd
from dataframes import to_extjs_gridcolumns
from event import Event
from gluon import current
from gluon.storage import Storage
......@@ -39,11 +41,18 @@ class ReportException(BaseException):
class BaseReport(object):
"""Base class to build list, metric or graph reports.
"""Base class to build reports.
Instantiate the DataFrame either using the config option or the
source name. In both user criteria are applied. They are extracted
by the selector from the requested variables.
Args:
table (gluon.dal.Table): contains the report configuration.
id_report (int): identifier of the report in the table.
config (gluon.storage.Storage): the report configuration.
The argument ``source_name`` has to be defined when
the report configuration is empty.
source_name (str): name of the source uses to generate the DataFrame.
Useful when it is not defined in the report configuration.
**Public attributes**:
* **config** (*gluon.dal.Row*): configuration for the report
......@@ -53,7 +62,7 @@ class BaseReport(object):
* **grid** (*dict*): configuration of the ``Ext.grid.Panel`` widget
"""
def __init__(self, table, id_report):
def __init__(self, config=Storage(), source_name=None):
virtdb = current.globalenv["virtdb"]
......@@ -65,18 +74,22 @@ class BaseReport(object):
# ....................................................................
#
# Get the configuration for the metric2d
# Get the configuration
#
self.config = config = table[id_report]
self.config = config
# ....................................................................
#
# Fill the the DataFrame
#
criteria = selector.as_dict()
criteria["id_events"] = config.id_events
criteria = Storage(selector.as_dict())
if config.id_events not in (None, ""):
criteria.id_events = config.id_events
func = Event.get_source(config.source)
name = (source_name if config.source is None else config.source)
func = Event.get_source(name)
self.df = df = func(**criteria)
......@@ -418,7 +431,7 @@ class List(BaseReport):
def __init__(self, id_list):
db = current.globalenv["db"]
BaseReport.__init__(self, db.lists2, id_list)
BaseReport.__init__(self, config=db.lists2[id_list])
# ....................................................................
#
......@@ -543,7 +556,7 @@ class Metric2D(BaseReport):
def __init__(self, id_report):
db = current.globalenv["db"]
BaseReport.__init__(self, db.metrics2d2, id_report)
BaseReport.__init__(self, config=db.metrics2d2[id_report])
self.is_summary = False
......@@ -734,3 +747,54 @@ class Metric2D(BaseReport):
df1 = pd.concat((df1, dfy.T))
return df1
class Source(BaseReport):
"""Transform the content of the source into configuration for
an ``Ext.grid.Panel`` and ``Ext.data.Store`` objects.
Args:
name: name of the source
Raises:
ReportException:
"""
def __init__(self, name):
BaseReport.__init__(self, source_name=name)
df = self.df
self.source_name = name
# ....................................................................
#
# Get the configuration for the Ext.grid.Panel
#
self.grid = grid = Storage(columns=to_extjs_gridcolumns(df, meta=True))
# ....................................................................
#
# Get the configuration for the Ext.data.Store
#
self.store = store = Store(data=[], fields=[], sorters=[])
for col in grid.columns:
if "dataIndex" not in col:
continue
cfg = Storage(name=col["dataIndex"],
type=FIELD_TYPES[col["xtype"]])
store.fields.append(cfg)
if not df.empty:
data = df.to_json(orient="records", date_format="iso")
store.data = json.loads(data)
def get_title(self):
T = current.T
self.config.title = "%s %s" % (T("Source"), self.source_name)
return BaseReport.get_title(self)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment