ui_selector.py 19.5 KB
Newer Older
1 2 3 4 5 6
# -*- coding: utf-8 -*-
"""User Interface for selectors

"""
from gluon.html import URL
from gluon.storage import Storage
7 8 9 10 11
from plugin_dbui import (FieldsModifier,
                         FieldSet,
                         map_tabpanel,
                         PanelWithUrlSelector,
                         to_fields)
12 13


14
class SelectorUi(object):
15

16
    @staticmethod
17 18
    def configure(virtdb, db, T):
        """Configure the user interface for selectors.
19

20 21 22 23
        Args:
            virtdb (pyDAL.DAL): connection to the virtual database
            db (pyDAL.DAL): database connection
            T (gluon.languages.translator): language translator
24

25
        """
26 27 28
        SelectorUi.selector_list(virtdb, db, T)
        SelectorUi.selector_metric2d(virtdb, db, T)
        SelectorUi.selector_source(virtdb, db, T)
29

30
    @staticmethod
31 32
    def selector_list(virtdb, db, T):
        """UI for the selector table list
33 34 35 36 37 38 39 40 41 42 43

        Args:
            virtdb (pyDAL.DAL): connection to the virtual database
            db (pyDAL.DAL): database connection
            T (gluon.languages.translator): language translator

        Returns:
            gluon.storage.Storage:
                the configuration of the PanelWithUrlSelector.

        """
LE GAC Renaud's avatar
LE GAC Renaud committed
44
        # ....................................................................
45 46 47
        #
        # Fields
        #
48
        mdf = FieldsModifier("selector_list")
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

        mytype = "xcomboboxuserreset"
        text = T("select...")

        mdf.configure_field("id_categories", emptyText=text, xtype=mytype)
        mdf.configure_field("id_domains", emptyText=text, xtype=mytype)
        mdf.configure_field("id_fundings", emptyText=text, xtype=mytype)
        mdf.configure_field("id_objects", emptyText=text, xtype=mytype)

        # combobox for object categories
        li = [el.category for el in db(db.object_categories.id > 0).select()]
        categories = sorted(set(li))

        mdf.configure_field("object_categories_category",
                            allowBlank=True,
                            emptyText=T("select..."),
                            store=categories,
                            xtype="combobox")

        # to have an unique key use xcomboboxmaster instead of userreset
        mdf.configure_field("id_object_code", emptyText=text, xtype=mytype)
        mdf.configure_field("id_people", emptyText=text, xtype=mytype)

        # combobox for people categories
        li = [el.category for el in db(db.people_categories.id > 0).select()]
        categories = sorted(set(li))

        mdf.configure_field("people_categories_category",
                            allowBlank=True,
                            emptyText=T("select..."),
                            store=categories,
                            xtype="combobox")

        # to have an unique key use xcomboboxmaster instead of userreset
        mdf.configure_field("id_people_code", emptyText=text, xtype=mytype)
        mdf.configure_field("id_teams", emptyText=text, xtype=mytype)
        mdf.configure_field("id_projects", emptyText=text, xtype=mytype)

        mdf.configure_field("query",
                            hideLabel=True,
                            emptyText="variable are column names while "
                                      "operators are in, not in, and, or, "
                                      "&, |, ...")

        # ....................................................................
        #
        # PanelWithUrlSelector
        #    - configure it once since it is used many times (lists, ...)
        #    - organise the field in tabs
        #    - Use an accordion layout for the first tab
        #

        # SELECT TAB .........................................................
        #
        # map between field name and field configuration
        # keys are the field name or field label for merge fields
        mapfields = Storage()
106
        for cfgfield in to_fields(virtdb.selector_list):
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
            key = (cfgfield.name if cfgfield.name else cfgfield.fieldLabel)
            mapfields[key] = cfgfield

        # Organise fields within different panels
        # and replace field name by their configuration
        items = [
            {
                "title": T("Period"),
                "collapsible": True,
                "items": [mapfields["Selector_listYear_start"],
                          mapfields["Selector_listYear_end"]],
                "xtype": "fieldset"
            }, {
                "title": T("Metadata"),
                "collapsible": True,
                "items": [mapfields["Selector_listId_domains"],
                          mapfields["Selector_listId_teams"],
                          mapfields["Selector_listId_projects"]],
                "xtype": "fieldset"
            }, {
                "title": T("People"),
                "collapsible": True,
                "collapsed": True,
                "items": [mapfields["Selector_listId_people"],
                          mapfields["Selector_listPeople_categories_category"],
                          mapfields["Selector_listId_people_code"]],
                "xtype": "fieldset"
            }, {
                "title": T("Object"),
                "collapsible": True,
                "collapsed": True,
                "items": [mapfields["Selector_listId_objects"],
                          mapfields["Selector_listId_object_categories"],
                          mapfields["Selector_listId_object_code"]],
                "xtype": "fieldset"
            }, {
                "title": T("Fundings"),
                "collapsible": True,
                "collapsed": True,
                "items": [mapfields["Selector_listId_fundings"]],
                "xtype": "fieldset"
            }, {
                "title": T("Query"),
                "collapsible": True,
                "collapsed": False,
                "items": [mapfields["Selector_listQuery"]],
                "xtype": "fieldset"
            }
        ]

        # wrap fields within a FieldSet
158
        fieldset1 = FieldSet(
159 160 161 162 163 164 165
            defaults=dict(frame=True, layout="form"),
            items=items,
            layout="anchor",
            plugins=["pFormToolTip"],
            title=T("Select"))

        # configure the PanelWithUrlSelector
166
        selector_panel = PanelWithUrlSelector(
167
            baseUrl=URL("report", "grid"),
168
            selectorItems=map_tabpanel([fieldset1]))
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

        return selector_panel

    @staticmethod
    def selector_metric2d(virtdb, db, T):
        """UI for the selector table metric2d

        Args:
            virtdb (pyDAL.DAL): connection to the virtual database
            db (pyDAL.DAL): database connection
            T (gluon.languages.translator): language translator

        Returns:
            gluon.storage.Storage:
                the configuration of the PanelWithUrlSelector.

        """
        # ....................................................................
        #
        # Fields
        #
190
        mdf = FieldsModifier("selector_metric2d")
191 192 193 194 195 196 197

        mytype = "xcomboboxuserreset"
        text = T("select...")

        mdf.configure_field("id_categories", emptyText=text, xtype=mytype)
        mdf.configure_field("id_domains", emptyText=text, xtype=mytype)
        mdf.configure_field("id_fundings", emptyText=text, xtype=mytype)
198
        mdf.configure_field("id_objects", emptyText=text, xtype=mytype)
199

200 201 202 203 204 205 206 207 208
        # combobox for object categories
        li = [el.category for el in db(db.object_categories.id > 0).select()]
        categories = sorted(set(li))

        mdf.configure_field("object_categories_category",
                            allowBlank=True,
                            emptyText=T("select..."),
                            store=categories,
                            xtype="combobox")
209

210
        # to have an unique key use xcomboboxmaster instead of userreset
211
        mdf.configure_field("id_object_code", emptyText=text, xtype=mytype)
212
        mdf.configure_field("id_people", emptyText=text, xtype=mytype)
213

214 215 216 217 218 219 220 221 222
        # combobox for people categories
        li = [el.category for el in db(db.people_categories.id > 0).select()]
        categories = sorted(set(li))

        mdf.configure_field("people_categories_category",
                            allowBlank=True,
                            emptyText=T("select..."),
                            store=categories,
                            xtype="combobox")
223

224
        # to have an unique key use xcomboboxmaster instead of userreset
225 226 227 228
        mdf.configure_field("id_people_code", emptyText=text, xtype=mytype)
        mdf.configure_field("id_teams", emptyText=text, xtype=mytype)
        mdf.configure_field("id_projects", emptyText=text, xtype=mytype)

229 230 231 232 233 234
        mdf.configure_field("query",
                            hideLabel=True,
                            emptyText="variable are column names while "
                                      "operators are in, not in, and, or, "
                                      "&, |, ...")

235 236 237
        mdf.configure_field("summary_x", hideLabel=True, xtype="xsummaryfield")
        mdf.configure_field("summary_y", hideLabel=True, xtype="xsummaryfield")
        mdf.configure_field("graph", hideLabel=True, xtype="xgraphfield")
238

LE GAC Renaud's avatar
LE GAC Renaud committed
239
        # ....................................................................
240 241
        #
        # PanelWithUrlSelector
242 243 244
        #    - configure it once since it is used many times (lists, ...)
        #    - organise the field in tabs
        #    - Use an accordion layout for the first tab
245 246
        #

247 248
        # SELECT TAB .........................................................
        #
249 250 251
        # map between field name and field configuration
        # keys are the field name or field label for merge fields
        mapfields = Storage()
252
        for cfgfield in to_fields(virtdb.selector_metric2d):
253 254 255 256 257 258 259 260
            key = (cfgfield.name if cfgfield.name else cfgfield.fieldLabel)
            mapfields[key] = cfgfield

        # Organise fields within different panels
        # and replace field name by their configuration
        items = [
            {
                "title": T("Period"),
261
                "collapsible": True,
262 263
                "items": [mapfields["Selector_metric2dYear_start"],
                          mapfields["Selector_metric2dYear_end"]],
264
                "xtype": "fieldset"
265
            }, {
266 267
                "title": T("Metadata"),
                "collapsible": True,
268 269 270
                "items": [mapfields["Selector_metric2dId_domains"],
                          mapfields["Selector_metric2dId_teams"],
                          mapfields["Selector_metric2dId_projects"]],
271
                "xtype": "fieldset"
272 273
            }, {
                "title": T("People"),
274
                "collapsible": True,
275
                "collapsed": True,
276 277 278
                "items": [mapfields["Selector_metric2dId_people"],
                          mapfields["Selector_metric2dPeople_categories_category"],
                          mapfields["Selector_metric2dId_people_code"]],
279
                "xtype": "fieldset"
280 281
            }, {
                "title": T("Object"),
282
                "collapsible": True,
283
                "collapsed": True,
284 285 286
                "items": [mapfields["Selector_metric2dId_objects"],
                          mapfields["Selector_metric2dId_object_categories"],
                          mapfields["Selector_metric2dId_object_code"]],
287
                "xtype": "fieldset"
288 289
            }, {
                "title": T("Fundings"),
290
                "collapsible": True,
291
                "collapsed": True,
292
                "items": [mapfields["Selector_metric2dId_fundings"]],
293
                "xtype": "fieldset"
294 295
            }, {
                "title": T("Query"),
296
                "collapsible": True,
297
                "collapsed": False,
298
                "items": [mapfields["Selector_metric2dQuery"]],
299
                "xtype": "fieldset"
300 301 302 303
            }
        ]

        # wrap fields within a FieldSet
304
        fieldset1 = FieldSet(
305
            defaults=dict(frame=True, layout="form"),
306
            items=items,
307
            layout="anchor",
308 309 310
            plugins=["pFormToolTip"],
            title=T("Select"))

311
        # SUMMARY TAB ........................................................
312 313 314
        items = [
            {
                "checkbox": {
315
                    "name": "Selector_metric2dIs_summary_x"
316 317 318
                },
                "checkboxToggle": True,
                "collapsed": True,
319
                "items": mapfields["Selector_metric2dSummary_x"],
320 321 322 323
                "title": "summary per row",
                "xtype": "fieldset"
            }, {
                "checkbox": {
324
                    "name": "Selector_metric2dIs_summary_y"
325 326 327
                },
                "checkboxToggle": True,
                "collapsed": True,
328
                "items": mapfields["Selector_metric2dSummary_y"],
329 330 331 332
                "title": "summary per column",
                "xtype": "fieldset"
            }
        ]
333

334
        fieldset2 = FieldSet(
335 336 337 338 339 340 341
            defaults=dict(frame=True),
            items=items,
            layout="anchor",
            plugins=["pFormToolTip"],
            title=T("Metric2D"))

        # GRAPH TAB ..........................................................
342 343 344
        items = [
            {
                "checkbox": {
345
                    "name": "Selector_metric2dIs_graph"
346 347 348
                },
                "checkboxToggle": True,
                "collapsed": True,
349
                "items": [mapfields["Selector_metric2dGraph"]],
350 351 352 353
                "title": "tune graph",
                "xtype": "fieldset"
            }
        ]
354

355
        fieldset3 = FieldSet(
356 357
            defaults=dict(frame=True),
            items=items,
358
            layout="anchor",
359 360 361
            plugins=["pFormToolTip"],
            title=T("Graph"))

362
        # configure the PanelWithUrlSelector
363
        selector_panel = PanelWithUrlSelector(
364
            baseUrl=URL("report", "grid"),
365
            selectorItems=map_tabpanel([fieldset1, fieldset2, fieldset3]))
366 367

        return selector_panel
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386

    @staticmethod
    def selector_source(virtdb, db, T):
        """UI for the selector table source

        Args:
            virtdb (pyDAL.DAL): connection to the virtual database
            db (pyDAL.DAL): database connection
            T (gluon.languages.translator): language translator

        Returns:
            gluon.storage.Storage:
                the configuration of the PanelWithUrlSelector.

        """
        # ....................................................................
        #
        # Fields
        #
387
        mdf = FieldsModifier("selector_source")
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445

        mytype = "xcomboboxuserreset"
        text = T("select...")

        mdf.configure_field("id_categories", emptyText=text, xtype=mytype)
        mdf.configure_field("id_domains", emptyText=text, xtype=mytype)
        mdf.configure_field("id_events", emptyText=text, xtype=mytype)
        mdf.configure_field("id_fundings", emptyText=text, xtype=mytype)
        mdf.configure_field("id_objects", emptyText=text, xtype=mytype)

        # combobox for object categories
        li = [el.category for el in db(db.object_categories.id > 0).select()]
        categories = sorted(set(li))

        mdf.configure_field("object_categories_category",
                            allowBlank=True,
                            emptyText=T("select..."),
                            store=categories,
                            xtype="combobox")

        # to have an unique key use xcomboboxmaster instead of userreset
        mdf.configure_field("id_object_code", emptyText=text, xtype=mytype)
        mdf.configure_field("id_people", emptyText=text, xtype=mytype)

        # combobox for people categories
        li = [el.category for el in db(db.people_categories.id > 0).select()]
        categories = sorted(set(li))

        mdf.configure_field("people_categories_category",
                            allowBlank=True,
                            emptyText=T("select..."),
                            store=categories,
                            xtype="combobox")

        # to have an unique key use xcomboboxmaster instead of userreset
        mdf.configure_field("id_people_code", emptyText=text, xtype=mytype)
        mdf.configure_field("id_teams", emptyText=text, xtype=mytype)
        mdf.configure_field("id_projects", emptyText=text, xtype=mytype)

        mdf.configure_field("query",
                            hideLabel=True,
                            emptyText="variable are column names while "
                                      "operators are in, not in, and, or, "
                                      "&, |, ...")

        # ....................................................................
        #
        # PanelWithUrlSelector
        #    - configure it once since it is used many times (lists, ...)
        #    - organise the field in tabs
        #    - Use an accordion layout for the first tab
        #

        # SELECT TAB .........................................................
        #
        # map between field name and field configuration
        # keys are the field name or field label for merge fields
        mapfields = Storage()
446
        for cfgfield in to_fields(virtdb.selector_source):
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
            key = (cfgfield.name if cfgfield.name else cfgfield.fieldLabel)
            mapfields[key] = cfgfield

        # Organise fields within different panels
        # and replace field name by their configuration
        items = [
            {
                "title": T("Period"),
                "collapsible": True,
                "items": [mapfields["Selector_sourceYear_start"],
                          mapfields["Selector_sourceYear_end"]],
                "xtype": "fieldset"
            }, {
                "title": T("Metadata"),
                "collapsible": True,
                "items": [mapfields["Selector_sourceId_events"],
                          mapfields["Selector_sourceId_domains"],
                          mapfields["Selector_sourceId_teams"],
                          mapfields["Selector_sourceId_projects"]],
                "xtype": "fieldset"
            }, {
                "title": T("People"),
                "collapsible": True,
                "collapsed": True,
                "items": [mapfields["Selector_sourceId_people"],
                          mapfields["Selector_sourcePeople_categories_category"],
                          mapfields["Selector_sourceId_people_code"]],
                "xtype": "fieldset"
            }, {
                "title": T("Object"),
                "collapsible": True,
                "collapsed": True,
                "items": [mapfields["Selector_sourceId_objects"],
                          mapfields["Selector_sourceId_object_categories"],
                          mapfields["Selector_sourceId_object_code"]],
                "xtype": "fieldset"
            }, {
                "title": T("Fundings"),
                "collapsible": True,
                "collapsed": True,
                "items": [mapfields["Selector_sourceId_fundings"]],
                "xtype": "fieldset"
            }, {
                "title": T("Query"),
                "collapsible": True,
                "collapsed": False,
                "items": [mapfields["Selector_sourceQuery"]],
                "xtype": "fieldset"
            }
        ]

        # wrap fields within a FieldSet
499
        fieldset1 = FieldSet(
500 501 502 503 504 505 506
            defaults=dict(frame=True, layout="form"),
            items=items,
            layout="anchor",
            plugins=["pFormToolTip"],
            title=T("Select"))

        # configure the PanelWithUrlSelector
507
        selector_panel = PanelWithUrlSelector(
508
            baseUrl=URL("report", "grid"),
509
            selectorItems=map_tabpanel([fieldset1]))
510 511

        return selector_panel