Commit 153b603f authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

Add callbacks ON_CREATE_LISTS2 and ON_UPDATE_LISTS2.

parent 2bd6cbcb
# -*- coding: utf-8 -*-
from callbacks import INHIBIT_CASCADE_DELETE
from callbacks import (INHIBIT_CASCADE_DELETE,
ON_CREATE_LISTS2,
ON_UPDATE_LISTS2)
from dataframes import (db2df,
debug_df,
......
......@@ -3,13 +3,20 @@
and _before_update callbacks.
"""
import json
from dataframes import to_extjs_gridcolumns
from gluon import current
from gluon.tools import PluginManager
from plugin_dbui import CALLBACK_ERRORS, get_where_query
MSG_INHIBIT_DELETE = \
"Can't delete this record since several transactions refer to it."
MSG_NO_DATA = \
"Can not fill the field 'columns' since there is no available data in %i"
def INHIBIT_CASCADE_DELETE(myset):
"""Inhibit the delete.
......@@ -54,3 +61,72 @@ def INHIBIT_CASCADE_DELETE(myset):
return True
return False
def ON_CREATE_LISTS2(values):
"""Fill the field column with values when it is empty:
* The values depend on the source.
* The DataFrame is instantiated for the source, the event and the
current year.
* The handler has to be called before the creation of a List2 record.
Args:
values (dict): field, value pairs
Returns:
bool: ``True`` when the record is rejected.
"""
if len(values["columns"]) > 0:
return False
# try to instantiate the DataFrame with a minimal set of criteria
year = current.request.now.year
criteria = dict(year_start=year, id_events=values["id_events"])
func = PluginManager("event").event.sources[values["source"]].func
df = func(**criteria)
if df.empty:
field._table[CALLBACK_ERRORS] = current.T(MSG_NO_DATA % year)
return True
# generate the configuration for Ext.grid.column.Column
lst = to_extjs_gridcolumns(df)
values["columns"] = json.dumps(lst, indent=4, sort_keys=True)
return False
def ON_UPDATE_LISTS2(myset, values):
"""Fill the field column with values when it is empty:
* The values depend on the source.
* The DataFrame is instantiated for the source, the event and the
current year.
* The handler has to be called before updating a List2 record.
Args:
myset (gluon.dal.Set): object describing the row to be updated.
values (dict): field, value pairs
Returns:
bool: ``True`` when the update is rejected.
"""
if ("columns" not in values) \
or ("columns" in values and len(values["columns"]) > 0):
return False
db = current.globalenv['db']
# get the id_events and source
if "id_events" not in values or "source" not in values:
row = db.lists2[values["id"]]
if "id_events" not in values:
values["id_events"] = row.id_events
if "source" not in values:
values["source"] = row.source
return ON_CREATE_LISTS2(values)
......@@ -5,7 +5,9 @@
import plugin_dbui as dbui
from callbacks import INHIBIT_CASCADE_DELETE
from callbacks import (INHIBIT_CASCADE_DELETE,
ON_CREATE_LISTS2,
ON_UPDATE_LISTS2)
from gluon import current, IS_IN_DB, IS_IN_SET
from gluon.tools import PluginManager
from pydal import Field
......@@ -181,8 +183,8 @@ class Report(object):
pyDAL.Table
"""
def_columns = (None if db._migrate else DEF_COLUMNS_LISTS)
def_features = (None if db._migrate else DEF_FEATURES)
migrate = current.globalenv["MIGRATE"]
def_features = (None if migrate else DEF_FEATURES)
table = db.define_table(
"lists2",
......@@ -207,7 +209,7 @@ class Report(object):
Field("sorters", "list:string", comment=TP_SORTERS),
Field("columns", "text",
default=def_columns,
default="",
comment=T(TP_COLUMNS)),
Field("features",
......@@ -219,9 +221,11 @@ class Report(object):
sources = PluginManager("event").event.sources.keys()
sources.sort()
db.lists2.source.requires = IS_IN_SET(sources)
db.lists2._before_insert.append(ON_CREATE_LISTS2)
db.lists2._before_update.append(ON_UPDATE_LISTS2)
return table
@staticmethod
......
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