ui_report.py 12.9 KB
Newer Older
1 2 3 4 5 6 7
# -*- coding: utf-8 -*-
"""User Interface for the report tables

"""
import plugin_dbui as dbui


8 9 10
fieldsModifier = dbui.FieldsModifier
formModifier = dbui.FormModifier
gridModifier = dbui.GridModifier
11 12
storeModifier = dbui.StoreModifier

13 14 15 16 17 18 19 20 21 22 23 24 25 26
COLUMN_GUIDE = "All columns will be created when the field is empty"

EVAL_GUIDE = """
It is possible to transform the source by creating new columns from existing
 ones using arithmetical expressions. The expression is written in natural
 language with one assignment per line. For more details have a look to the
 function pandas.eval and to the method pandas.DataFrame.eval.
"""

QUERY_GUIDE = """
It is possible to select data by applying a query on fields of the source.
 The query is written in natural language using and / or operator.
 For more details have a look to the method pandas.DataFrame.query.
"""
27

28
class ReportUi(object):
29

30 31 32 33
    @staticmethod
    def define(db, T):
        """Tune the user interface for the report tables: lists, metrics1d,
        metrics2d and graphs.
34

35 36 37
        Args:
            db (pyDAL.DAL): database connection
            T (gluon.languages.translator): language translator
38

39 40 41
        """
        ReportUi.graphs(db, T)
        ReportUi.lists(db, T)
LE GAC Renaud's avatar
LE GAC Renaud committed
42
        ReportUi.lists2(db, T)
43 44
        ReportUi.metrics1d(db, T)
        ReportUi.metrics2d(db, T)
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
    @staticmethod
    def graphs(db, T):
        """UI for the graphs table

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

        """
        #.....................................................................
        #
        # Fields
        #
        mdf = fieldsModifier("graphs")

        mdf.configure_field("definition", height=110)
        mdf.configure_field("plot", height=240, hideLabel=True)

        # the field report type and name are linked ComboBox
        store = dbui.Store(data=[], fields=[])
        store.fields.append(dict(name="report_type", type="string"))
        store.fields.append(dict(name="report_name", type="string"))

        for tablename in ("lists", "metrics1d", "metrics2d"):
            for row in db().select(db[tablename].name):
                store.data.append(
                    dict(report_type=tablename, report_name=row.name))

        mdf.configure_field("report_type",
                            displayField="report_type",
                            emptyText= T("select..."),
                            itemId="mastercombo_report_type",
                            refStore=store,
                            valueField="report_type",
                            xtype="xcomboboxmaster")

        mdf.configure_field("report_name",
                            displayField="report_name",
                            emptyText=T("select..."),
                            masterItemId="mastercombo_report_type",
                            masterValueField="report_type",
                            refStore=store,
                            valueField="report_name",
                            xtype="xcomboboxslave")

        #.....................................................................
        #
        # Form
        #
        mdf = formModifier("graphs")

        mdf.merge_fields("name",
                         "title",
                         "report_type",
                         "report_name",
                         "definition",
                         title=T("General"))

        mdf.merge_fields("plot", title=T("Configure"))

        mdf.set_mapper(dbui.map_tabpanel)

        mdf.configure(fieldDefaults={"labelWidth": 135}, width=450)

        #.....................................................................
        #
        # Grid
        #
        mdf = gridModifier("graphs")
        mdf.configure_column("name", width=30)
        mdf.configure_column("report_type", width=20)
        mdf.hide_columns("plot",
                         "report_name",
                         "title")

        #.....................................................................
        #
        # Store
        #
        storeModifier("graphs").orderby(db.graphs.name)

    @staticmethod
    def lists(db, T):
        """UI for the lists table

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

        """
        #.....................................................................
        #
        # Fields
        #
        mdf = fieldsModifier("lists")

        mdf.configure_field("columns",
                            editorHeight=240,
                            hideLabel=True,
                            language="json",
                            xtype="xaceeditorfield")

        mdf.configure_field("definition", height=100)

        mdf.configure_field("features",
                            editorHeight=240,
                            hideLabel=True,
                            language="json",
                            xtype="xaceeditorfield")

        mdf.configure_field("sorters",
                            height=210,
                            hideHeader=True,
                            minimumRows=10)

        #.....................................................................
        #
        # Form
        #
        mdf = formModifier("lists")

        mdf.merge_fields("name",
                         "title",
                         "definition",
                         "conditions",
                         title=T("General"))

        mdf.merge_fields("group_field",
                         "sorters",
                         title=T("Group"))

        mdf.merge_fields("columns",
                         title=T("Columns"))

        mdf.merge_fields("features",
                         title=T("Summary"))

        mdf.set_mapper(dbui.map_tabpanel)
        mdf.configure(width=450)

        #.....................................................................
        #
        # Grid
        #
        mdf = gridModifier("lists")
        mdf.configure_column("name", width=30)
        mdf.hide_columns("title",
                         "columns",
                         "conditions",
                         "features",
                         "group_field",
                         "sorters")

        #.....................................................................
        #
        # Store
        #
        storeModifier("lists").orderby(db.lists.name)

LE GAC Renaud's avatar
LE GAC Renaud committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
    @staticmethod
    def lists2(db, T):
        """UI for the lists table

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

        """
        #.....................................................................
        #
        # Fields
        #
        mdf = fieldsModifier("lists2")

        mdf.configure_field("columns",
                            editorHeight=240,
222 223 224
                            fieldLabel=COLUMN_GUIDE,
                            hideLabel=False,
                            labelAlign="top",
LE GAC Renaud's avatar
LE GAC Renaud committed
225 226 227 228 229
                            language="json",
                            xtype="xaceeditorfield")

        mdf.configure_field("definition", height=100)

230 231 232 233 234
        mdf.configure_field("eval",
                            emptyText=EVAL_GUIDE,
                            height=120,
                            hideLabel=True)

LE GAC Renaud's avatar
LE GAC Renaud committed
235 236 237 238 239 240
        mdf.configure_field("features",
                            editorHeight=240,
                            hideLabel=True,
                            language="json",
                            xtype="xaceeditorfield")

241 242
        mdf.configure_field("id_events", emptyText=" ")

243 244 245 246 247
        mdf.configure_field("query",
                            emptyText=QUERY_GUIDE,
                            height=120,
                            hideLabel=True)

LE GAC Renaud's avatar
LE GAC Renaud committed
248 249 250 251 252 253 254 255 256 257 258 259 260
        mdf.configure_field("sorters",
                            height=210,
                            hideHeader=True,
                            minimumRows=10)

        #.....................................................................
        #
        # Form
        #
        mdf = formModifier("lists2")

        mdf.merge_fields("name",
                         "title",
261 262
                         "source",
                         "id_events",
LE GAC Renaud's avatar
LE GAC Renaud committed
263 264 265 266 267 268 269
                         "definition",
                         title=T("General"))

        mdf.merge_fields("group_field",
                         "sorters",
                         title=T("Group"))

270 271 272 273
        mdf.merge_fields("eval",
                         "query",
                         title=T("Transform"))

LE GAC Renaud's avatar
LE GAC Renaud committed
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
        mdf.merge_fields("columns",
                         title=T("Columns"))

        mdf.merge_fields("features",
                         title=T("Summary"))

        mdf.set_mapper(dbui.map_tabpanel)
        mdf.configure(width=450)

        #.....................................................................
        #
        # Grid
        #
        mdf = gridModifier("lists2")
        mdf.configure_column("name", width=30)
289
        mdf.hide_columns("columns",
LE GAC Renaud's avatar
LE GAC Renaud committed
290
                         "conditions",
291
                         "eval",
LE GAC Renaud's avatar
LE GAC Renaud committed
292 293
                         "features",
                         "group_field",
294
                         "id_events",
LE GAC Renaud's avatar
LE GAC Renaud committed
295
                         "kwargs",
296
                         "query",
LE GAC Renaud's avatar
LE GAC Renaud committed
297 298
                         "sorters",
                         "source",
299
                         "title")
LE GAC Renaud's avatar
LE GAC Renaud committed
300 301 302 303 304 305 306

        #.....................................................................
        #
        # Store
        #
        storeModifier("lists2").orderby(db.lists2.name)

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 362 363 364 365
    @staticmethod
    def metrics1d(db, T):
        """UI for the metrics1d table

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

        """
        #.....................................................................
        #
        # Fields
        #
        mdf = fieldsModifier("metrics1d")

        mdf.configure_field("columns",
                            editorHeight=240,
                            hideLabel=True,
                            language="json",
                            xtype="xaceeditorfield")

        mdf.configure_field("definition", height=100)

        #.....................................................................
        #
        # Form
        #
        mdf = formModifier("metrics1d")

        mdf.merge_fields("name",
                         "title",
                         "definition",
                         "conditions",
                         title=T("General"))

        mdf.merge_fields("group_field",
                         title=T("Group"))

        mdf.merge_fields("columns",
                         title=T("Columns"))

        mdf.set_mapper(dbui.map_tabpanel)
        mdf.configure(width=450)

        #.....................................................................
        #
        # Grid
        #
        mdf = gridModifier("metrics1d")
        mdf.configure_column("name", width=30)
        mdf.hide_columns("title",
                         "columns",
                         "conditions",
                         "group_field")

        #.....................................................................
        #
        # Store
        #
366
        storeModifier("metrics1d").orderby(db.metrics1d.name)
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 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

    @staticmethod
    def metrics2d(db, T):
        """UI for the metrics2d table

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

        """
        #.....................................................................
        #
        # Fields
        #
        mdf = fieldsModifier("metrics2d")

        mdf.configure_field("conditions", height=110)
        mdf.configure_field("definition", height=110)

        #.....................................................................
        #
        # Form
        #
        mdf = formModifier("metrics2d")

        mdf.merge_fields("name",
                         "title",
                         "definition",
                         "conditions",
                         title=T("General"))

        mdf.merge_fields("group_field_x",
                         "group_field_y",
                         "metric_field_z",
                         "aggregation_z",
                         title=T("Aggregate"))

        mdf.set_mapper(dbui.map_tabpanel)
        mdf.configure(width=450)

        #.....................................................................
        #
        # Grid
        #
        mdf = gridModifier("metrics2d")
        mdf.configure_column("name", width=30)
        mdf.hide_columns("aggregation_z",
                         "conditions",
                         "group_field_x",
                         "group_field_y",
                         "metric_field_z",
                         "title")

        #.....................................................................
        #
        # Store
        #
424
        storeModifier("metrics2d").orderby(db.metrics2d.name)