Commit 3cdfbd8d authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

Create the modules plugin_tev and the classes EvtModelCore, EvtModelReport,...

Create the modules plugin_tev and the classes EvtModelCore, EvtModelReport, EvtUiCore and EvtUiReport.
parent dec59bd1
from model_core import EvtModelCore
from model_report import EvtModelReport
\ No newline at end of file
# -*- coding: utf-8 -*-
"""Definitions of the core tables
"""
import plugin_dbui as dbui
from callbacks import INHIBIT_CASCADE_DELETE
from datetime import datetime
from gluon import current
from gluon.validators import IS_IN_DB
class EvtModelCore(object):
"""The core tables for the tev plugin.
"""
@staticmethod
def _protect(table):
"""protect the content of the table.
* Record containing the UNDEF string can not be update or delete.
* Inhibit cascade delte for foreign elements
Args:
table (pyDAL.Table): database table
"""
table._before_delete.append(INHIBIT_CASCADE_DELETE)
table._before_delete.append(dbui.INHIBIT_DELETE_UNDEF)
table._before_update.append(dbui.INHIBIT_UPDATE_UNDEF)
@staticmethod
def create(db, T):
"""Create all tables belonging to the core, including
alias table for object and people categories.
Args:
db (pyDAL.DAL): database connection
T (): language translator
"""
# foreign tables
EvtModelCore.domains(db, T)
EvtModelCore.events(db, T)
EvtModelCore.fundings(db, T)
EvtModelCore.object_categories(db, T)
EvtModelCore.object(db, T)
EvtModelCore.people_categories(db, T)
EvtModelCore.people(db, T)
EvtModelCore.projects(db, T)
EvtModelCore.teams(db, T)
# the main table
EvtModelCore.history(db, T)
# alias
db.object_categories.with_alias("alias_object_categories")
db.people_categories.with_alias("alias_people_categories")
@staticmethod
def domains(db, T):
"""domains table
Args:
db (pyDAL.DAL): database connection
T (gluon.languages.translator): language translator
Returns:
pyDAL.Table
"""
table = db.define_table(
"domains",
Field("domain", "string", length=255, notnull=True, unique=True),
migrate="domains.table")
EvtModelCore._protect(table)
return table
@staticmethod
def events(db, T):
"""events table
Args:
db (pyDAl.DAL): database connection
T (gluon.languages.translator): language translator
Returns:
pyDAL.Table
"""
table = db.define_table(
"events",
Field("event", "string", length=255, notnull=True, unique=True),
Field("definition", "text"),
Field("data", "json", label="Model"),
migrate="events.table")
EvtModelCore._protect(table)
# NOTE: we have to remove the json validator to work !!
table.data.requires = None
return table
@staticmethod
def fundings(db, T):
"""fundings table
Args:
db (pyDAl.DAL): database connection
T (gluon.languages.translator): language translator
Returns:
pyDAL.Table
"""
table = db.define_table(
"fundings",
Field("agency", "string", length=255, notnull=True, unique=True),
Field("definition", "text"),
migrate="fundings.table")
EvtModelCore._protect(table)
return table
@staticmethod
def history(db, T):
"""history table
Args:
db (pyDAl.DAL): database connection
T (gluon.languages.translator): language translator
Returns:
pyDAL.Table
"""
today = datetime.now().strftime("%Y-%m-%d")
undef_id = dbui.UNDEF_ID
table = db.define_table(
"history",
Field("id_events",
"reference events",
default=undef_id,
label="Event"),
Field("id_domains",
"reference domains",
default=undef_id,
label="Domain"),
Field("id_teams",
"reference teams",
default=undef_id,
label="Team"),
Field("id_projects",
"reference projects",
default=undef_id,
label="Project"),
Field("id_fundings",
"reference fundings",
default=undef_id,
label="Funding"),
Field("id_people",
"reference people",
default=undef_id,
label="Person"),
Field("id_people_categories",
"reference people_categories",
default=undef_id,
label="People category"),
Field("percentage", "integer", default=None),
Field("id_objects",
"reference objects",
default=undef_id,
label="Object"),
Field("id_object_categories",
"reference object_categories",
default=undef_id,
label="Object category"),
Field("start_date", "date", default=today, notnull=True),
Field("end_date", "date"),
Field("note", "text"),
Field("data", "json", default=dict()),
migrate="history.table")
# NOTE: we have to remove the json validator to work !!
table.data.requires = None
table.id_events.requires = IS_IN_DB(db, "events.event")
table.id_people.requires = IS_IN_DB(db, "people.last_name")
@staticmethod
def object_categories(db, T):
"""object_categories table
Args:
db (pyDAl.DAL): database connection
T (gluon.languages.translator): language translator
Returns:
pyDAL.Table
"""
table = db.define_table(
"object_categories",
Field("code", "string", length=255, notnull=True, unique=True),
Field("category", "string", length=255, notnull=True),
Field("definition", "text"),
migrate="object_categories.table")
EvtModelCore._protect(table)
return table
@staticmethod
def objects(db, T):
"""objects table
Args:
db (pyDAl.DAL): database connection
T (gluon.languages.translator): language translator
Returns:
pyDAL.Table
"""
table = db.define_table(
"objects",
Field("reference", "string", length=255, notnull=True, unique=True),
Field("serial_number", "string", length=255),
Field("batch_number", "string", length=255),
Field("note", "text"),
migrate="objects.table")
EvtModelCore._protect(table)
return table
@staticmethod
def people_categories(db, T):
"""people_categories table
Args:
db (pyDAl.DAL): database connection
T (gluon.languages.translator): language translator
Returns:
pyDAL.Table
"""
table = db.define_table(
"people_categories",
Field("code",
"string",
length=255,
notnull=True,
unique=True,
label=T("Quality")),
Field("category", "string", length=255, notnull=True),
Field("definition", "text"),
migrate="people_categories.table")
EvtModelCore._protect(table)
return table
@staticmethod
def people(db, T):
"""people table
Args:
db (pyDAl.DAL): database connection
T (gluon.languages.translator): language translator
Returns:
pyDAL.Table
"""
table = db.define_table(
"people",
Field("first_name", "string", length=255, notnull=True),
Field("last_name", "string", length=255, notnull=True),
Field("initials", "string", length=255, notnull=True),
Field("birth_date", "date"),
Field("note", "text"),
migrate="people.table")
EvtModelCore._protect(table)
return table
@staticmethod
def projects(db, T):
"""projects table
Args:
db (pyDAl.DAL): database connection
T (gluon.languages.translator): language translator
Returns:
pyDAL.Table
"""
table = db.define_table(
"projects",
Field("project", "string", length=255, notnull=True, unique=True),
migrate="projects.table")
EvtModelCore._protect(table)
return table
@staticmethod
def tables():
"""Tool returning the list of table names.
Returns:
list
"""
lst = ["domains",
"events",
"fundings",
"object_categories",
"objects",
"people",
"people_categories",
"projects",
"teams"]
return lst
@staticmethod
def teams(db, T):
"""teams table
Args:
db (pyDAl.DAL): database connection
T (gluon.languages.translator): language translator
Returns:
pyDAL.Table
"""
table = db.define_table(
"teams",
Field("team", "string", length=255, notnull=True, unique=True),
migrate="teams.table")
EvtModelCore._protect(table)
return table
# -*- coding: utf-8 -*-
"""Definitions of the report tables
"""
import plugin_dbui as dbui
from callbacks import INHIBIT_CASCADE_DELETE
from datetime import datetime
from gluon import current, IS_IN_DB
DEF_COLUMNS_LISTS = \
"""[{
"xtype": "rownumberer"
}, {
"dbfield": "?.?",
"flex": 1,
"text": "?",
"xtype": "gridcolumn"
}]"""
DEF_COLUMNS_METRIC1D = \
"""[{
"aggregate": "?",
"align": "right",
"dbfield": "?.?",
"format": "0.0",
"text": "?",
"xtype": "numbercolumn"
}]"""
DEF_FEATURES = \
"""[{
"ftype": "groupingsummary",
"groupHeaderTpl": "{name}",
"startCollapsed": false
}, {
"ftype": "summary"
}]"""
DEF_PLOT = \
"""{
"kind": "?",
"stacked": false
}"""
TP_AGG = \
"The aggregation function applies on the metric field. " \
"It is computed on the subset of row having a given values for " \
"the group_field x and y."
TP_COLUMNS = "Configure the column of the grid displayed in the view."
TP_CONDITIONS = \
"Database query to select history records:<br> " \
"(db.events.event == 'People') & (db.history.data.like('%cdd%'))<br>"
TP_GROUP_FIELD_LISTS = \
"Row are grouped according to the value of that field. " \
"It can be any field of the history table including those of " \
"the foreign tables, the individual property of the history.data " \
"dictionary, or 'year'."
TP_GROUP_FIELD_METRICS = \
"Metric are computed for each value of that field. " \
"It can be any field of the history table including those of the " \
"foreign tables, the individual property of the history.data dictionary " \
"or 'year'"
TP_FEATURES = "Summary value can be computed for columns."
TP_SORTERS = \
"Entries are sorted according to the value of these fields. " \
"It can be any field of the history table including those of the " \
"foreign tables, the individual property of the history.data dictionary, " \
"or 'year'. Descending order is obtained by using the '~field' construct."
class EvtModelReport(object):
"""The report tables for the tev plugin.
"""
@staticmethod
def create(db, T):
"""Create all the tables for reporting.
Args:
db (pyDAL.DAL): database connection
T (gluon.languages.translator): language translator
"""
EvtModelReport.graphs(db, T)
EvtModelReport.lists(db, T)
EvtModelReport.metrics1d(db, T)
EvtModelReport.metrics2d(db, T)
@staticmethod
def graphs(db, T):
"""graphs table
Args:
db (pyDAL.DAL): database connection
T (gluon.languages.translator): language translator
Returns:
pyDAL.Table
"""
def_plot = (None if db.migrate else DEF_PLOT)
table = db.define_table(
"graphs",
Field("name", "string", length=255, notnull=True, unique=True),
Field("title", "string", length=255),
Field("report_type", "string", length=255, notnull=True),
Field("report_name", "string", length=255, notnull=True),
Field("plot", "text", notnull=True, default=def_plot),
Field("definition", "text"),
migrate="graphs.table")
return table
@staticmethod
def lists(db, T):
"""lists table
Args:
db (pyDAL.DAL): database connection
T (gluon.languages.translator): language translator
Returns:
pyDAL.Table
"""
def_columns = (None if db.migrate else DEF_COLUMNS_LISTS)
def_features = (None if db.migrate else DEF_FEATURES)
table = db.define_table(
"lists",
Field("name", "string", length=255, notnull=True, unique=True),
Field("title", "string", length=255),
Field("conditions", "text", comment=T(TP_CONDITIONS)),
Field("group_field",
"string",
length=255,
comment=T(TP_GROUP_FIELD_LISTS)),
Field("sorters", "list:string", comment=TP_SORTERS),
Field("columns",
"text",
default=def_columns,
comment=T(TP_COLUMNS),
notnull=True),
Field("features",
"text",
default=def_features,
comment=T(TP_FEATURES)),
Field("definition", "text"),
migrate="lists.table")
return table
@staticmethod
def metrics1d(db, T):
"""metrics1d table
Args:
db (pyDAL.DAL): database connection
T (gluon.languages.translator): language translator
Returns:
pyDAL.Table
"""
def_columns = (None if db.migrate else DEF_COLUMNS_METRIC1D)
table = db.define_table(
"metrics1d",
Field("name", "string", length=255, notnull=True, unique=True),
Field("title", "string", length=255),
Field("conditions", "text", comment=T(TP_CONDITIONS)),
Field("group_field",
"string",
length=255,
comment=T(TP_GROUP_FIELD_METRICS),
notnull=True),
Field("columns",
"text",
default=def_columns,
comment=T(TP_COLUMNS),
notnull=True),
Field("definition", "text"),
migrate="metrics1d.table")
@staticmethod
def metrics2d(db, T):
"""metrics2d table
Args:
db (pyDAL.DAL): database connection
T (gluon.languages.translator): language translator
Returns:
pyDAL.Table
"""
table = db.define_table(
"metrics2d",
Field("name", "string", length=255, notnull=True, unique=True),
Field("title", "string", length=255),
Field("conditions", "text", comment=T(TP_CONDITIONS)),
Field("group_field_x",
"string",
length=255,
notnull=True,
comment=T(TP_GROUP_FIELD_METRICS)),
Field("group_field_y",
"string",
length=255,
notnull=True,
comment=T(TP_GROUP_FIELD_METRICS)),
Field("metric_field_z",
"string",
length=255,
notnull=True,
comment=T(TP_GROUP_FIELD_METRICS)),
Field("aggregation_z",
"string",
length=255,
notnull=True,
comment=T(TP_AGG)),
Field("definition", "text"),
migrate="metrics2d.table")