model_app.py 1.95 KB
Newer Older
LE GAC Renaud's avatar
LE GAC Renaud committed
1 2 3
"""Definitions of the App tables

"""
4
from .callbacks import INHIBIT_CASCADE_DELETE
LE GAC Renaud's avatar
LE GAC Renaud committed
5 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
from gluon import current
from gluon.storage import Storage

from plugin_dbui import (INHIBIT_DELETE_UNDEF,
                         INHIBIT_UPDATE_UNDEF)

from pydal import Field


class App(object):
    """Create tables to steer the application.

    """
    @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 tables to steer the application.

        Args:
            db (pyDAL.DAL): database connection
            T (gluon.languages.translator): language translator

        """
        App.preferences(db, T)

    @staticmethod
    def preferences(db, T):
        """preferences table

        Args:
            db (pyDAL.DAL): database connection
            T (gluon.languages.translator): language translator

        Returns:
            pyDAL.Table

        """
        table = db.define_table(
            "preferences",
            Field("property", "string", length=255, notnull=True, unique=True),
            Field("value", "json", length=255),
            Field("definition", "text", notnull=True),
            migrate="preferences.table")

        table.value.requires = None

        # load preference for a later use
        app = Storage()
        for row in db(db.preferences).select():
            app[row.property] = row.value

        # add local variable
LE GAC Renaud's avatar
LE GAC Renaud committed
71 72 73
        app.cfgPreferences = None
        app.reg_institute = ""
        app.institute = None
LE GAC Renaud's avatar
LE GAC Renaud committed
74 75 76 77

        current.app = app

        return table