dbui.py 12.6 KB
Newer Older
1 2 3 4 5
# -*- coding: utf-8 -*-
""" dbui

"""
import copy
6
import plugin_dbui as dbui
7 8


9
from collections import OrderedDict
10
from gluon import current
11 12 13 14
from gluon.storage import Storage
from gluon.tools import PluginManager
from helper import get_language

15
myapp = current.request.application
16 17 18 19

defaults = Storage(

    # application
20
    app_about="static/ABOUT.html",
21
    app_css=None,
22 23
    app_changelog="static/CHANGELOG",
    app_db_schema="static/docs/database.png",
24 25 26
    app_debug=None,
    app_git=None,
    app_lg=None,
27
    app_license=None,
28 29 30
    app_libmin=None,
    app_script=None,
    app_script_dir=None,
31 32 33 34 35 36 37
    app_html_api="static/docs/api/index.html",
    app_html_jsduck="static/docs/jsduck/index.html",
    app_html_user="static/docs/user/index.html",
    app_html_reference="static/docs/reference/index.html",
    app_pdf_api="static/docs/pdf/%s_api.pdf" % myapp,
    app_pdf_reference="static/docs/pdf/%s_reference.pdf" % myapp,
    app_pdf_user="static/docs/pdf/%s_user.pdf" % myapp,
38

39
    # dbui URL
40 41
    dbui_conf="/%s/plugin_dbui/dbui_conf" % myapp,
    dbui_conf_debug="/%s/plugin_dbui/dbui_conf?debug" % myapp,
42
    dbui_script="static/plugin_dbui/main.js",
43

44 45 46 47 48 49 50 51
    # dbui documentation and license
    dbui_html_api="static/plugin_dbui/docs/api/index.html",
    dbui_html_jsduck="static/plugin_dbui/docs/jsduck/index.html",
    dbui_html_reference="static/plugin_dbui/docs/reference/index.html",
    dbui_license="static/plugin_dbui/LICENCE_UK.html",
    dbui_pdf_api=None,
    dbui_pdf_reference=None,

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
    # paths for plugins ace, dbui, extjs and mathjax
    # Warning the order of the plugins matter
    # since extjs has to be called before dbui
    plugins_paths=OrderedDict([
        (
            "ace", {
                "css": "static/plugin_ace/css/editor.css",
                "debug": "static/plugin_ace/src-noconflict/ace.js",
                "lg": None,
                "libmin": "static/plugin_ace/src-min-noconflict/ace.js"}
        ), (
            "extjs", {
                "css": "static/plugin_extjs/build/classic/theme-classic/resources/theme-classic-all.css",
                "debug": ["static/plugin_extjs/build/ext-all-debug.js",
                          "static/plugin_extjs/build/classic/theme-classic/theme-classic-debug.js"],
                "lg": "static/plugin_extjs/build/classic/locale/locale-%s.js",
                "libmin": ["static/plugin_extjs/build/ext-all.js",
                           "static/plugin_extjs/build/classic/theme-classic/theme-classic.js"]}
        ), (
            "dbui", {
                "css": ["static/plugin_dbui/resources/css/kde-oxygen.css",
                        "static/plugin_dbui/resources/css/dbui.css"],
                "debug": "static/plugin_dbui/src/Dbui.js",
                "lg": "static/plugin_dbui/locale/dbui-lang-%s.js",
                "libmin": "static/plugin_dbui/dbui-min.js"}
        ), (
            "mathjax", {
                "css": None,
                "debug": "static/plugin_mathjax/MathJax.js",
                "lg": None,
                "libmin": "static/plugin_mathjax/MathJax.js"}
        )
    ])
)
86 87 88 89 90 91 92


class Dbui(object):
    """Set of tools to initialise the plugin Dbui.

    """
    @staticmethod
93
    def initialise_paths(**kwargs):
94 95 96 97 98 99 100 101 102
        """Initialise the configuration of the the plugin dbui, namely paths
        and a persistent storage.

        Paths are relative to the main directory of the application.
        They can target a file (static/myapp.css) or a directory (static/css).

        The persistent storage contains the configuration of the widgets
        used in the user interface.

103 104 105 106 107 108 109
        Note:
            The configuration of the plugin is persistent across the
            application. It can be accessed at any time using the syntax::

                from gluon.tools import PluginManager
                PluginManager("dbui").dbui

110 111
        Args:

112 113 114 115
            app_about (str):
                relative path to the ABOUT file,
                *e.g.* ``static/ABOUT.html``.

116
            app_css (str or list):
117
                relative path to CSS files for the application,
118 119
                *e.g.* ``static/myapp/ressources/css``. Default is ``None``

120 121 122 123 124 125 126 127
            app_changelog (str):
                relative path to the CHANGELOG,
                *e.g.* ``static/CHANGELOG``.

            app_db_schema (str):
                relative path to the database schema,
                *e.g.* ``static/docs/database.png``.

128
            app_debug (str or list):
129
                relative path to the debug version for the application library,
130 131 132 133
                *e.g.* ``static/myapp/src`` or ``static/myapp/myapp-debug.js``.
                Default is ``None``

            app_git (str):
134 135
                the URL of the git repository for the application,
                *e.g.* ``https://gitlab.in2p3.fr/w2pext/plugin_dbui.git``
136 137 138
                Default is ``None``.

            app_lg(str or list):
139 140
                relative path to languages files for the application,
                *e.g.* ``static/myapp/locale/myapp-lang-fr.js``.
141 142
                Default is ``None``.

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
            app_html_api (str):
                relative path to the documentation for the python api,
                *e.g.* ``static/docs/api/index.html``.
                The format is HTML.

            app_html_jsduck (str):
                relative path to the documentation for the javascript api,
                *e.g.* ``static/docs/jsduck/index.html``.
                The format is HTML.

            app_html_reference (str):
                relative path to the documentation for developers,
                *e.g.* ``static/docs/reference/index.html``.
                The format is HTML.

            app_html_user (str):
                relative path to the documentation for users,
                *e.g.* ``static/docs/user/index.html``.
                The format is HTML.

163
            app_libmin (str or list):
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
                relative path to the minified version for the application
                library, *e.g.* ``static/myapp-min.js``.

            app_license (str):
                relative path to the license for the application.

            app_pdf_api (str):
                relative path to the documentation for the python api,
                *e.g.* ``static/docs/pdf/myapp_api.pdf``.
                The format is PDF.

            app_pdf_reference (str):
                relative path to the documentation for developers,
                *e.g.* ``static/docs/pdf/myapp_reference.pdf``.
                The format is PDF.

            app_pdf_user (str):
                relative path to the documentation for user,
                *e.g.* ``static/docs/pdf/myapp_user.pdf``.
                The format is PDF.
184 185

            app_script (str):
186 187
                relative path to the main script to run the application,
                *e.g.* ``static/myapp.js``.
188 189

            app_script_dir (str):
190 191
                relative path to a directory containing a collection
                of JavaScript, *e.g* ``static/scripts``.
192 193 194 195
                The script named *foo* can be run via the URL:
                ``https://myverver/myapp?script=foo``. The default is ``None``.

            dbui_conf (str):
196
                the ULR returning the configuration for the User Interface.
197
                The default is ``/myapp/plugin_dbui/dbui_conf``,
198 199

            dbui_conf_debug (str):
200
                the URL returning the debug version of the configuration for
201
                the User Interface. The default is
202
                ``/myapp/plugin_dbui/dbui_conf?debug``.
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
            dbui_html_api (str):
                relative path to the documentation for the python api.
                *e.g.* ``static/plugin_dbui/docs/api/index.html``.
                The format is HTML.

            dbui_html_jsduck (str):
                relative path to the documentation for the javascript api,
                *e.g.* ``static/plugin_dbui/docs/jsduck/index.html``.
                The format is HTML.

            dbui_html_reference (str):
                relative path to the documentation for developers,
                *e.g.* ``static/plugin_dbui/docs/reference/index.html``.
                The format is HTML.

            dbui_license (str):
                relative path to the license for the plugin dbui,
                *e.g* ``static/plugin_dbui/LICENCE_UK.html``.

            dbui_pdf_api (str):
                relative path to the documentation for the python api,
                *e.g.* ``static/plugin_dbui/docs/pdf/dbui_api.pdf``.
                The format is PDF.

            dbui_pdf_reference (str):
                relative path to the documentation for developers,
                *e.g.* ``static/plugin_dbui/docs/pdf/dbui_reference.pdf``.
                The format is PDF.

233
            dbui_script (str):
234 235 236
                default script to run the application:
                ``static/plugin_dbui/main.js``.
                It is used when the option ``app_script`` is not defined.
237

238
            plugins_paths (dict):
239 240 241
                contains paths defining javascript libraries for plugins.
                The key is the name of the plugin while the value is a
                dictionary with 4 keys:
242

243 244 245
                    * css (str or list or None):
                        relative path to CSS files for plugin:
                        ``static/plugin_key/resources/css``
246

247 248 249
                    * debug (str or list or None):
                        relative path to the debug version of the plugin
                        library: ``static/plugin_key/dbui-debug.js``.
250

251 252 253
                    * lg (str or list or None):
                        relative path to languages files for the plugin
                        library: ``static/plugin_key/locale/key-lang-fr.js``.
254

255 256 257
                    * libmin (str or list or None):
                        relative path to languages files for the plugin
                        library: ``static/plugin_key/locale/key-lang-fr.js``.
258

259
                By default, plugins *ace*, *dbui*, *extjs* and *mathjax* are
260 261 262 263 264 265
                configure.

                Note:
                    It is possible to register others plugins by adding them
                    to the plugins_paths dictionary. There libraries will be
                    laod properly by the controller ``plugin_dbui/index``.
266 267 268 269 270 271 272 273 274 275 276


        """
        plugin = PluginManager("dbui").dbui

        # apply default values
        plugin.update(copy.deepcopy(defaults))
        plugin.update(kwargs)

        # update values depending on language
        lg = get_language()
277 278 279 280
        plugins_paths = plugin.plugins_paths

        paths_dbui = plugins_paths["dbui"]
        paths_dbui["lg"] = paths_dbui["lg"] % lg
281

282 283
        paths_extjs = plugins_paths["extjs"]
        paths_extjs["lg"] = paths_extjs["lg"] % lg
284

285 286 287
    @staticmethod
    def initialise_ui():
        """Initialise persistent storage for the plugin dbui.
288

289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
        Attributes:
            modifier_fields (dict):
            modifier_forms (dict):
            modifier_grids (dict):
            modifier_stores (dict):
            modifier_viewports (gluon.storage.Storage):
            static_stores (dict):

        """
        plugin = PluginManager("dbui").dbui

        plugin.modifier_fields = {}
        plugin.modifier_forms = {}
        plugin.modifier_grids = {}
        plugin.modifier_stores = {}
        plugin.modifier_viewports = Storage(extjs={})
        plugin.static_stores = {}
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 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361

    @staticmethod
    def start_directSvc():
        """Start services to interact with the database from the
        User Interface using the Ext JS Direct protocol.

        Returns:
            directSvc:

        """
        db = current.globalenv["db"]

        # Start common services
        dbSvc = dbui.DbSvc()
        directSvc = dbui.DirectSvc()

        # activate the debug mode
        # this session variable is set by the control index or debug
        session = current.session
        dbSvc.debug = session.debug
        directSvc.debug = session.debug

        # register method available on the client side
        @directSvc.register
        def close_user_session():
            return dbui.close_user_session()

        @directSvc.register
        def getForm(tablename):
            return dbui.to_formPanel(db[tablename])

        @directSvc.register
        def getGrid(tablename):
            return dbui.to_gridPanel(db[tablename])

        @directSvc.register
        def create(data):
            return dbSvc.create(data)

        @directSvc.register
        def count(data):
            return dbSvc.count(data)

        @directSvc.register
        def destroy(data):
            return dbSvc.destroy(data)

        @directSvc.register
        def read(data):
            return dbSvc.read(data)

        @directSvc.register
        def update(data):
            return dbSvc.update(data)

        return directSvc