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

Rename the class MySemector EvtSelector.

parent 704f70c8
......@@ -15,7 +15,7 @@ from plugin_event import (do_title,
Metric1D,
Metric2D,
ReportException)
from plugin_event import MySelector, SelectorActiveItemsException
from plugin_event import EvtSelector, SelectorActiveItemsException
MSG = T(" - %s entry(ies) modified in the history table. <br>"
......@@ -32,15 +32,15 @@ def graph_extjs():
"""
ui_table = virtdb.selector
selector = MySelector(ui_table, exclude_fields=('metric',))
selector = EvtSelector(ui_table, exclude_fields=("metric",))
# homomorphism between metric2D and graph
config = db.graphs[request.vars.id_graphs]
config.group_field_y = config.field_horizontal
config.group_field_x = config.field_stacked
config.metric_field_z = 'people.id'
config.aggregation_z = 'size'
config.metric_field_z = "people.id"
config.aggregation_z = "size"
# generate the metric according to user specification
report = Metric2D(config, selector)
......@@ -62,7 +62,7 @@ def graph_mpl():
# selector and configuration
try:
ui_table = virtdb.selector
selector = MySelector(ui_table)
selector = EvtSelector(ui_table)
except SelectorActiveItemsException as e:
return INLINE_ALERT % (T("Error..."), T(str(e)))
......@@ -100,7 +100,7 @@ def grid():
"""
try:
ui_table = virtdb.selector
selector = MySelector(ui_table)
selector = EvtSelector(ui_table)
except SelectorActiveItemsException as e:
return INLINE_ALERT % (T("Error..."), T(str(e)))
......@@ -160,7 +160,7 @@ def userModelConsistency():
update_keys = {}
for action, oldkey, newkey in changes:
if action != 'update':
if action != "update":
continue
# simplify the case where key1 > key2 > key3 to key1 > key3
......@@ -189,14 +189,14 @@ def userModelConsistency():
# set-up the default values dealing with data conversion
for key in model:
value = model[key]['value']
type = model[key]['type']
value = model[key]["value"]
type = model[key]["type"]
if value:
try:
if type == "boolean":
value = value.lower() in ('true', 'vrai', 'y', 'yes', '1')
value = value.lower() in ("true", "vrai", "y", "yes", "1")
elif type == "date":
value = datetime.strptime(value, "%Y-%m-%d")
......@@ -219,7 +219,7 @@ def userModelConsistency():
except ValueError as e:
return MSG_CVT % e
model[key]['value'] = value
model[key]["value"] = value
# scan and modify the history table
# the delete of old key is implicit since they are not copied
......@@ -232,7 +232,7 @@ def userModelConsistency():
# copy existing key or add new key
for key in model:
data[key] = (rd[key] if key in rd else model[key]['value'])
data[key] = (rd[key] if key in rd else model[key]["value"])
# modify key name
for oldkey, newkey in update_keys.iteritems():
......
plugin_event.MySelector
=======================
plugin_event.EvtSelector
========================
.. currentmodule:: plugin_event
.. autoclass:: MySelector
.. autoclass:: EvtSelector
:show-inheritance:
.. rubric:: Methods
......@@ -11,5 +11,5 @@ plugin_event.MySelector
.. autosummary::
:toctree: selector/
~MySelector.query
~MySelector.reset_extra_queries
~EvtSelector.query
~EvtSelector.reset_extra_queries
plugin_event.EvtSelector.query
==============================
.. currentmodule:: plugin_event
.. automethod:: EvtSelector.query
\ No newline at end of file
plugin_event.EvtSelector.reset_extra_queries
============================================
.. currentmodule:: plugin_event
.. automethod:: EvtSelector.reset_extra_queries
\ No newline at end of file
plugin_event.MySelector.query
=============================
.. currentmodule:: plugin_event
.. automethod:: MySelector.query
\ No newline at end of file
plugin_event.MySelector.reset_extra_queries
===========================================
.. currentmodule:: plugin_event
.. automethod:: MySelector.reset_extra_queries
\ No newline at end of file
......@@ -11,4 +11,4 @@ Classes
:toctree: generated/
SelectorActiveItems
MySelector
EvtSelector
......@@ -27,7 +27,7 @@ from report_objects import (do_title,
Metric1D,
Metric2D)
from selector import (MySelector,
from selector import (EvtSelector,
SelectorActiveItems,
SelectorActiveItemsException)
......
......@@ -29,13 +29,13 @@ def do_title(config, selector):
Args:
config (gluon.dal.Row): the list configuration parameter.
selector (MySelector): selector handling period of time.
selector (EvtSelector): selector handling period of time.
Returns:
str:
"""
db = current.globalenv['db']
db = current.globalenv["db"]
T = current.T
# from the configuration
......@@ -64,10 +64,10 @@ def do_title(config, selector):
years = (str(selector.year_start), str(selector.year_end))
metadata.append(T("from %s to %s") % years)
return "%s: %s" % (title, ' / '.join(metadata))
return "%s: %s" % (title, " / ".join(metadata))
def get_value(row, tablename, fieldname, keyname='', **kwargs):
def get_value(row, tablename, fieldname, keyname="", **kwargs):
"""Helper function returning the value of a database field.
The method is designed to handle standard and JSON-type database field.
......@@ -134,11 +134,11 @@ def split_dbfield(value):
is either a string or an empty string.
"""
li = value.split('.')
li = value.split(".")
if len(li) == 1:
li.extend(['', ''])
li.extend(["", ""])
elif len(li) == 2:
li.append('')
li.append("")
return tuple(li)
......@@ -151,12 +151,12 @@ class BaseReport(object):
Args:
config (gluon.dal.Row): the configuration parameter for the report.
selector (MySelector): the selector handling user criteria.
selector (EvtSelector): the selector handling user criteria.
"""
def __init__(self, config, selector):
db = current.globalenv['db']
db = current.globalenv["db"]
self.db = db
self.df = None
......@@ -248,7 +248,7 @@ class BaseReport(object):
bool: ``True`` is the pseudo field ``year`` is in maps
"""
li = [True for el in maps if el[0] == 'year']
li = [True for el in maps if el[0] == "year"]
return (True if li else False)
def to_df(self):
......@@ -274,7 +274,7 @@ class Graph(BaseReport):
config (gluon.dal.Row):
the configuration parameter for the graph.
selector (MySelector):
selector (EvtSelector):
selector handling period of time.
backend (str):
......@@ -302,7 +302,7 @@ class Graph(BaseReport):
config.plot = json.loads(config.plot)
config.steer = Storage()
for k in ('index', 'transpose', 'xlabel', 'ylabel'):
for k in ("index", "transpose", "xlabel", "ylabel"):
v = config.plot.pop(k, None)
config.steer[k] = v
......@@ -362,10 +362,10 @@ class Graph(BaseReport):
steer = self.config.steer
if steer.xlabel:
ax.set_xlabel(steer.xlabel, x=1, horizontalalignment='right')
ax.set_xlabel(steer.xlabel, x=1, horizontalalignment="right")
if steer.ylabel:
ax.set_ylabel(steer.ylabel, y=1, horizontalalignment='right')
ax.set_ylabel(steer.ylabel, y=1, horizontalalignment="right")
def _do_legend(self):
"""Deal with legend.
......@@ -376,7 +376,7 @@ class Graph(BaseReport):
if ax.get_legend():
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width, box.height * 0.9])
ax.legend(loc='lower right',
ax.legend(loc="lower right",
bbox_to_anchor=(1.01, 1.),
fontsize=10,
ncol=3)
......@@ -388,8 +388,8 @@ class Graph(BaseReport):
ax = self.ax
ax.minorticks_on()
ax.tick_params(which='major', length=8)
ax.tick_params(which='minor', length=4)
ax.tick_params(which="major", length=8)
ax.tick_params(which="minor", length=4)
def _savefig(self, fmt):
"""Save the figure as a string.
......@@ -417,7 +417,7 @@ class Graph(BaseReport):
str:
"""
return self._savefig('pdf')
return self._savefig("pdf")
def to_png(self):
"""Encode the graph using the PNG format.
......@@ -426,7 +426,7 @@ class Graph(BaseReport):
str:
"""
return self._savefig('png')
return self._savefig("png")
def to_svg(self):
"""Encode the graph using the SVG format.
......@@ -435,7 +435,7 @@ class Graph(BaseReport):
str:
"""
return self._savefig('svg')
return self._savefig("svg")
class List(BaseReport):
......@@ -456,7 +456,7 @@ class List(BaseReport):
Args:
config (gluon.dal.Row): the configuration parameter for the list.
selector (MySelector): selector handling period of time.
selector (EvtSelector): selector handling period of time.
"""
def __init__(self, config, selector):
......@@ -508,40 +508,40 @@ class List(BaseReport):
tablename, fieldname = dbfield[0:2]
# the dtype of column containing a mixture of type is object.
if (tablename == 'year') or (df[column].dtype != 'object'):
if (tablename == "year") or (df[column].dtype != "object"):
return
dbtype = self.db[tablename][fieldname].type
# the dtype for column containing string is also object
if dbtype in ('string', 'text'):
if dbtype in ("string", "text"):
return
elif dbtype == 'boolean':
df[column] = df[column].astype('bool')
elif dbtype == "boolean":
df[column] = df[column].astype("bool")
elif dbtype in ('date', 'datetime', 'time'):
elif dbtype in ("date", "datetime", "time"):
df[column] = to_datetime(df[column])
elif dbtype in ('double', 'integer'):
df[column] = df[column].astype('float64')
elif dbtype in ("double", "integer"):
df[column] = df[column].astype("float64")
# database field containing JSON-type dictionary
# The type of the key is defined in the event model but it is
# not accessible at this stage. Instead we use the grid column xtype.
elif dbtype == 'json':
elif dbtype == "json":
if xtype == 'gridcolumn':
if xtype == "gridcolumn":
pass
elif xtype == 'booleancolumn':
df[column] = df[column].astype('bool')
elif xtype == "booleancolumn":
df[column] = df[column].astype("bool")
elif xtype == 'datecolumn':
elif xtype == "datecolumn":
df[column] = to_datetime(df[column])
elif xtype == 'numbercolumn':
df[column] = df[column].astype('float64')
elif xtype == "numbercolumn":
df[column] = df[column].astype("float64")
def _check_column(self, column):
"""Check column configuration:
......@@ -569,7 +569,7 @@ class List(BaseReport):
column.map = split_dbfield(dbfield)
if not (column.dataIndex or xtype == "rownumberer"):
column.dataIndex = column.dbfield.replace('.', '')
column.dataIndex = column.dbfield.replace(".", "")
def _do_metric(self):
"""Interface the database with the DataFrame structure.
......@@ -623,7 +623,7 @@ class List(BaseReport):
"""
# extract the list of records as a JSON-string
# at this stage date/time are converted as an ISO8601 string
data = self.df.to_json(orient='records', date_format='iso')
data = self.df.to_json(orient="records", date_format="iso")
# convert the JSON-string into a list
self._store.data = json.loads(data)
......@@ -655,49 +655,49 @@ class List(BaseReport):
cfg = Storage(name=el.dataIndex)
# the pseudo field year
if el.dbfield == 'year':
cfg.type = 'int'
if el.dbfield == "year":
cfg.type = "int"
# the computed column
elif el.eval:
cfg.type = 'float'
cfg.type = "float"
# json type database field
elif keyname:
xtype = el.xtype
if xtype == 'gridcolumn':
cfg.type = 'string'
if xtype == "gridcolumn":
cfg.type = "string"
elif xtype == 'booleancolumn':
cfg.type = 'boolean'
elif xtype == "booleancolumn":
cfg.type = "boolean"
elif xtype == 'datecolumn':
cfg.type = 'date'
elif xtype == "datecolumn":
cfg.type = "date"
elif xtype == 'numbercolumn':
cfg.type = 'float'
elif xtype == "numbercolumn":
cfg.type = "float"
# standard database field, extract the type from the database field
else:
dbfield = db[tablename][fieldname]
cfg.type = dbfield.type
if dbfield.type in ('blob', 'string', 'text', 'json'):
cfg.type = 'string'
if dbfield.type in ("blob", "string", "text", "json"):
cfg.type = "string"
elif dbfield.type == 'boolean':
cfg.type = 'boolean'
elif dbfield.type == "boolean":
cfg.type = "boolean"
elif dbfield.type in ('date', 'datetime', 'time'):
cfg.type = 'date'
cfg.dateFormat = 'c'
elif dbfield.type in ("date", "datetime", "time"):
cfg.type = "date"
cfg.dateFormat = "c"
elif dbfield.type == 'double':
cfg.type = 'float'
elif dbfield.type == "double":
cfg.type = "float"
elif dbfield.type == 'integer':
cfg.type = 'int'
elif dbfield.type == "integer":
cfg.type = "int"
store.fields.append(cfg)
......@@ -717,7 +717,7 @@ class List(BaseReport):
# remove non Ext JS property
for cfg in self._columns:
for key in ('dbfield', 'eval', 'map'):
for key in ("dbfield", "eval", "map"):
if key in cfg:
del cfg[key]
......@@ -745,13 +745,13 @@ class List(BaseReport):
config = self.config
store = Store(data=[], fields=[])
store.groupField = config.group_field.replace('.', '')
store.groupField = config.group_field.replace(".", "")
store.sorters = []
for el in config.sorters:
sorter = dict(property=el.replace('.', '').replace('~', ''))
sorter = dict(property=el.replace(".", "").replace("~", ""))
if el.startswith('~'):
if el.startswith("~"):
sorter["direction"] = "DESC"
store.sorters.append(sorter)
......@@ -787,7 +787,7 @@ class Metric1D(List):
Args:
config (gluon.dal.Row): the configuration parameter for the metric.
selector (MySelector): selector handling period of time.
selector (EvtSelector): selector handling period of time.
"""
def __init__(self, config, selector):
......@@ -798,18 +798,18 @@ class Metric1D(List):
field_groupby = config.group_field
if field_groupby == "year":
index_groupby = 'year'
text_groupby = 'year'
index_groupby = "year"
text_groupby = "year"
else:
tu = split_dbfield(field_groupby)
index_groupby = field_groupby.replace('.', '')
index_groupby = field_groupby.replace(".", "")
text_groupby = (tu[2] if tu[2] else tu[1])
# first column contains group by information
first_column = Storage(aggregate="",
dbfield=field_groupby,
text=text_groupby,
xtype='gridcolumn')
xtype="gridcolumn")
# columns configuration
columns = [first_column]
......@@ -893,7 +893,7 @@ class Metric1D(List):
bool: true if the group_field is year.
"""
return self.config.group_field == 'year'
return self.config.group_field == "year"
def _set_store_data(self):
"""Generate the ``Ext.data.Store.data`` property.
......@@ -903,7 +903,7 @@ class Metric1D(List):
"""
df = self.df
data = df.to_dict(orient='records')
data = df.to_dict(orient="records")
# add the groupby value to each row
index_groupby = self._index_groupby
......@@ -925,17 +925,17 @@ class Metric1D(List):
cfg = Storage(name=column.dataIndex)
xtype = column.xtype
if xtype == 'numbercolumn':
cfg.type = 'float'
if xtype == "numbercolumn":
cfg.type = "float"
elif xtype == 'booleancolumn':
cfg.type = 'boolean'
elif xtype == "booleancolumn":
cfg.type = "boolean"
elif xtype == 'datecolumn':
cfg.type = 'date'
elif xtype == "datecolumn":
cfg.type = "date"
elif xtype == 'gridcolumn':
cfg.type = 'string'
elif xtype == "gridcolumn":
cfg.type = "string"
store.fields.append(cfg)
......@@ -958,14 +958,14 @@ class Metric1D(List):
for cfg in columns:
# remove non Ext JS property
for key in ('aggregate', 'dbfield', 'eval', 'map'):
for key in ("aggregate", "dbfield", "eval", "map"):
if key in cfg:
del cfg[key]
grid.columns.append(cfg)
# activate summary feature
grid.features = [{'ftype': 'summary'}]
grid.features = [{"ftype": "summary"}]
return grid
......@@ -999,7 +999,7 @@ class Metric2D(BaseReport):
Args:
config (gluon.dal.Row): the configuration parameter for the metric.
selector (MySelector): selector handling period of time.
selector (EvtSelector): selector handling period of time.
"""
def __init__(self, config, selector):
......@@ -1025,8 +1025,8 @@ class Metric2D(BaseReport):
address_y = config.group_field_y
address_z = config.metric_field_z
if address_y == 'year':
address_y, address_x = address_x, 'year'
if address_y == "year":
address_y, address_x = address_x, "year"
addresses = [address_x, address_y, address_z]
......@@ -1078,7 +1078,7 @@ class Metric2D(BaseReport):
df = df[ref_addresses]
# remove duplicate entries
if aggregate in ('count', 'size'):
if aggregate in ("count", "size"):
df = df.drop_duplicates()
# group the data
......@@ -1089,7 +1089,7 @@ class Metric2D(BaseReport):
df = df.unstack(level=0)
# rotate the data frame when year is along the y-axis
if config.group_field_y == 'year':
if config.group_field_y == "year":
df = df.T
# transform year MultiIndex into Index
......@@ -1107,7 +1107,7 @@ class Metric2D(BaseReport):
"""
df = self.df
name = self.config.group_field_y.replace('.', '')
name = self.config.group_field_y.replace(".", "")
# idr is the index of the row either a tuple or a string
# row is a dictionary containing row values
......@@ -1130,14 +1130,14 @@ class Metric2D(BaseReport):
store = self._store