"""Definitions of the report's tables """ from .callbacks import INHIBIT_CASCADE_DELETE from gluon.validators import IS_IN_DB, IS_IN_SET from plugin_dbui import (CLEAN_SPACES, INHIBIT_DELETE_UNDEF, INHIBIT_UPDATE_UNDEF) from pydal import Field DIRS = ["ASC", "DESC"] TP_AXIS_LABEL = \ "Axis values are used to defined the title of each level in section. "\ "This field allows to replace some values by another ones. " \ 'Rule: label1: "value1", label2: "value2",....' TP_CONDITIONS = \ "Can be applied on any field of the table using the SQL WHERE syntax. "\ "Be aware that foreign key are not resolved " \ "(see smart_query in the web2py)" class Report(object): """Create the ``lists`` and ``metrics`` table and associated tables. """ @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(INHIBIT_DELETE_UNDEF) table._before_update.append(INHIBIT_UPDATE_UNDEF) @staticmethod def define_tables(db, T): """define the core tables Args: db (pyDAL.DAL): database connection T (gluon.languages.translator): language translator """ Report.axes(db, T) Report.graphs(db, T) Report.lists(db, T) Report.metrics(db, T) Report.renderers(db, T) Report.sections(db, T) @staticmethod def axes(db, T): """axes table Args: db (pyDAL.DAL): database connection T (gluon.languages.translator): language translator Returns: pyDAL.Table """ table = db.define_table( "axes", Field("axis", "string", length=255, notnull=True), Field("granularity", "string", length=255, notnull=True), Field("tr_axis", "string", length=255, notnull=True), Field("tr_granularity", "string", length=255, notnull=True), migrate="axes.table") return table @staticmethod def graphs(db, T): """graphs table Args: db (pyDAL.DAL): database connection T (gluon.languages.translator): language translator Returns: pyDAL.Table """ tp_stacked = \ T("The graph can be rendered as line or stacked chart. " "The latter is used when the stacked fields are defined. ") table = db.define_table( "graphs", Field("graph", "string", length=255, notnull=True), Field("definition", "text", default=""), Field("stack_axis", "string", length=255, comment=tp_stacked), Field("stack_granularity", "string", length=255), migrate="graphs.table") table.graph.filter_in = CLEAN_SPACES return table @staticmethod def lists(db, T): """lists table Args: db (pyDAL.DAL): database connection T (gluon.languages.translator): language translator Returns: pyDAL.Table """ tp_list = T("Name of the list.") tp_sections = \ T("List of sections separated by a comma: Articles, Proceedings.") table = db.define_table( "lists", Field("list", "string", comment=tp_list, length=255, notnull=True), Field("definition", "text", default=""), Field("sections", "text", comment=tp_sections, notnull=True), Field("axis_level_1", "string", length=255, default="", label=T("Level 1")), Field("axis_level_2", "string", length=255, default="", label=T("Level 2")), Field("axis_level_3", "string", length=255, default="", label=T("Level 3")), Field("axis_level_4", "string", length=255, default="", label=T("Level 4")), Field("granularity_level_1", "string", length=255), Field("granularity_level_2", "string", length=255), Field("granularity_level_3", "string", length=255), Field("granularity_level_4", "string", length=255), Field("direction_level_1", "string", length=255, default="ASC"), Field("direction_level_2", "string", length=255, default="ASC"), Field("direction_level_3", "string", length=255, default="ASC"), Field("direction_level_4", "string", length=255, default="ASC"), Field("axis_label_converters", "text", comment=T(TP_AXIS_LABEL), default="", label="Converters"), Field("header_flag", "boolean", default=True, label="Header"), Field("footer_flag", "boolean", default=True, label="Footer"), migrate="lists.table") table.list.filter_in = CLEAN_SPACES table.sections.filter_in = CLEAN_SPACES table.direction_level_1.requires = IS_IN_SET(DIRS) table.direction_level_2.requires = IS_IN_SET(DIRS) table.direction_level_3.requires = IS_IN_SET(DIRS) table.direction_level_4.requires = IS_IN_SET(DIRS) return table @staticmethod def metrics(db, T): """metrics table Args: db (pyDAL.DAL): database connection T (gluon.languages.translator): language translator Returns: pyDAL.Table """ table = db.define_table( "metrics", Field("metric", "string", length=255, notnull=True), Field("definition", "text", default=""), Field("axis_vertical", "string", default="", length=255, notnull=True), Field("axis_horizontal", "string", default="", length=255, notnull=True), Field("granularity_vertical", "string", length=255), Field("granularity_horizontal", "string", length=255), Field("direction_vertical", "string", length=255, default="ASC"), Field("direction_horizontal", "string", length=255, default="ASC"), Field("axis_label_converters", "text", comment=T(TP_AXIS_LABEL), default="", label="Converters"), Field("conditions", "text", comment=T(TP_CONDITIONS), default=""), Field("header_flag", "boolean", default=True, label="Header"), Field("footer_flag", "boolean", default=True, label="Footer"), migrate="metrics.table") table.metric.filter_in = CLEAN_SPACES table.direction_horizontal.requires = IS_IN_SET(DIRS) table.direction_vertical.requires = IS_IN_SET(DIRS) return table @staticmethod def renderers(db, T): """renderers table Args: db (pyDAL.DAL): database connection T (gluon.languages.translator): language translator Returns: pyDAL.Table """ tp_postprocessing = \ T("Name of a function located in the modules list_postprocessing. " "Can be a list of name separated by comma.") tp_template = \ T("String containing blabla and the database fields to be " "displayed. The substitution mechanism is: " "{tablename.fieldname} or {foreigntablename.fieldname}") table = db.define_table( "renderers", Field("renderer", "string", length=255, notnull=True), Field("template", "text", comment=tp_template, notnull=True), Field("postprocessing", "text", comment=tp_postprocessing, default=""), Field("definition", "text", default=""), migrate="renderers.table") return table @staticmethod def sections(db, T): """sections table Args: db (pyDAL.DAL): database connection T (gluon.languages.translator): language translator Returns: pyDAL.Table """ tp_categories = \ T("List of category codes separated by a comma: ACL, ACLN. " "Only the publications with the given categories will be " "processed in the section.") tp_section = T("Name of the section.") tp_sort_field = \ T("Sort the publications list associated to the section according " "to the database field. The field has to belong to the table " "publications. The publications of this section will be sort " "according to this field.") table = db.define_table( "sections", Field("section", "string", length=255, comment=tp_section, notnull=True), Field("id_renderers", "reference renderers", notnull=True, label="Render"), Field("category_codes", "text", comment=tp_categories, default=""), Field("conditions", "text", comment=T(TP_CONDITIONS), default=""), Field("sort_field", "string", length=255, comment=tp_sort_field, default=""), Field("direction", "string", length=255, default="ASC", requires=IS_IN_SET(DIRS)), Field("definition", "text", default=""), migrate="sections.table") table.section.filter_in = CLEAN_SPACES table.id_renderers.requires = IS_IN_DB(db, "renderers.renderer") return table