model_report.py 10.2 KB
Newer Older
LE GAC Renaud's avatar
LE GAC Renaud committed
1 2 3
"""Definitions of the report's tables

"""
4
from .callbacks import INHIBIT_CASCADE_DELETE
5
from gluon.validators import IS_IN_DB, IS_IN_SET
LE GAC Renaud's avatar
LE GAC Renaud committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333

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
334
        table.id_renderers.requires = IS_IN_DB(db, "renderers.renderer")
LE GAC Renaud's avatar
LE GAC Renaud committed
335 336

        return table