ui_viewport.py 13.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
# -*- coding: utf-8 -*-
"""Main user interface the viewport

"""
from auth import ADMIN, USER
from gluon import current
from gluon.html import URL

from plugin_dbui import (Node,
                         Panel,
                         to_gridPanel,
LE GAC Renaud's avatar
LE GAC Renaud committed
12
                         to_panelWithUrlSelector,
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
                         ViewportModifier,
                         Window)


ONE_HOUR = 3600000


def to_grid(tablename):
    """helper function to translate a tablename into the grid configuration.

    Args:
        tablename (str)

    Return:
        dict

    """
    return to_gridPanel(current.db[tablename])


class ViewportUi(object):

    @staticmethod
    def configure(db, T, virtdb):
        """Configure the viewport.

        Args:
            db (pyDAL.DAL): database connection
            T (gluon.languages.translator): language translator
            virtdb (pyDAL.DAL):
                virtual database with the definition of selectors

        """
LE GAC Renaud's avatar
LE GAC Renaud committed
46 47
        session = current.globalenv["session"]

48
        nodes = [
LE GAC Renaud's avatar
LE GAC Renaud committed
49 50 51 52 53 54 55 56 57
            ViewportUi.help_node(db, T, session),
            ViewportUi.app_node(db, T, session),
            ViewportUi.wizard_node(db, T, session, virtdb),
            ViewportUi.configure_harvester_node(db, T, session),
            ViewportUi.configure_list_node(db, T, session),
            ViewportUi.configure_metric_node(db, T, session),
            ViewportUi.data_institute_node(db, T, session),
            ViewportUi.data_publication_node(db, T, session),
            ViewportUi.harvester_node(db, T, session, virtdb),
58 59 60 61 62 63 64 65 66 67 68
            ViewportUi.report_node(db, T, virtdb)]

        mdf = ViewportModifier()
        mdf.add_node(*nodes)

        mdf.configure(tabTitleTpl="{1}")

        # NOTE the session.auth only exit when the user is logged
        if current.auth is not None:
            mdf.configure(logged=False, plugins=["pViewportLogin"])

LE GAC Renaud's avatar
LE GAC Renaud committed
69
        session = current.globalenv["session"]
70 71 72 73 74 75 76 77
        if session.role in (ADMIN, USER):
            mdf.configure(logged=True)
            mdf.default_node(T("Data publications"), T("publications"))

        else:
            mdf.default_node(T("Reports"), T("dashboard"))

    @staticmethod
LE GAC Renaud's avatar
LE GAC Renaud committed
78
    def app_node(db, T, session):
79 80 81 82 83
        """To deal with preferences for the application.

        Args:
            db (pyDAL.DAL): database connection
            T (gluon.languages.translator): language translator
LE GAC Renaud's avatar
LE GAC Renaud committed
84
            session (gluon.storage.Storage):
85 86 87 88 89 90 91

        Returns:
            dbui.Node:
                the configuration of a tree node.

        """
        node = None
LE GAC Renaud's avatar
LE GAC Renaud committed
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
        if session.role == ADMIN:

            node = Node(T("Application"))
            add_child = node.add_child

            window = Window(
                closable=True,
                items=[current.app.cfgPreferences],
                layout="fit",
                modal=True,
                title=T("Preferences"))

            add_child(T("preferences"), window)

            if current.auth is not None:
                add_child(T("users"), to_grid("auth_user"))
                add_child(T("groups"), to_grid("auth_group"))

                add_child(
                    T("relation user / groups"),
                    to_grid("auth_membership"))

        return node

    @staticmethod
LE GAC Renaud's avatar
LE GAC Renaud committed
118
    def configure_harvester_node(db, T, session):
119 120 121 122 123
        """To configure harvesters.

        Args:
            db (pyDAL.DAL): database connection
            T (gluon.languages.translator): language translator
LE GAC Renaud's avatar
LE GAC Renaud committed
124
            session (gluon.storage.Storage):
125 126 127 128 129 130 131

        Returns:
            dbui.Node:
                the configuration of a tree node.

        """
        node = None
LE GAC Renaud's avatar
LE GAC Renaud committed
132

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
        if session.role in (ADMIN, USER):

            node = Node(T("Configure harvesters"))

            add_child = node.add_child
            add_child(T("harvesters"), to_grid("harvesters"))

            add_child(
                T("relation harvesters / categories"),
                to_grid("controllers"))

            add_child(T("author's rescue list"), to_grid("my_authors"))

        return node

    @staticmethod
LE GAC Renaud's avatar
LE GAC Renaud committed
149
    def configure_list_node(db, T, session):
150 151 152 153 154
        """To configure list.

        Args:
            db (pyDAL.DAL): database connection
            T (gluon.languages.translator): language translator
LE GAC Renaud's avatar
LE GAC Renaud committed
155
            session (gluon.storage.Storage):
156 157 158 159 160 161 162

        Returns:
            dbui.Node:
                the configuration of a tree node.

        """
        node = None
LE GAC Renaud's avatar
LE GAC Renaud committed
163

164 165 166 167 168 169 170 171 172 173 174 175 176 177
        if session.role == ADMIN:

            node = Node(T("Configure lists"))
            add_child = node.add_child

            # NOTE: hide axes table which contains static data
            # add_child("axes", to_grid("axes"))
            add_child(T("renderers"), to_grid("renderers"))
            add_child(T("sections"), to_grid("sections"))
            add_child(T("lists"), to_grid("lists"))

        return node

    @staticmethod
LE GAC Renaud's avatar
LE GAC Renaud committed
178
    def configure_metric_node(db, T, session):
179 180 181 182 183
        """To configure metrics.

        Args:
            db (pyDAL.DAL): database connection
            T (gluon.languages.translator): language translator
LE GAC Renaud's avatar
LE GAC Renaud committed
184
            session (gluon.storage.Storage):
185 186 187 188 189 190 191

        Returns:
            dbui.Node:
                the configuration of a tree node.

        """
        node = None
LE GAC Renaud's avatar
LE GAC Renaud committed
192

193 194 195 196 197 198 199 200 201 202 203
        if session.role == ADMIN:

            node = Node(T("Configure graphs and metrics"))
            add_child = node.add_child

            add_child(T("graphs"), to_grid("graphs"))
            add_child(T("metrics"), to_grid("metrics"))

        return node

    @staticmethod
LE GAC Renaud's avatar
LE GAC Renaud committed
204
    def data_institute_node(db, T, session):
205 206 207 208 209 210 211
        """To define data for the institute, affiliation_keys, projects
        and teams.

        Args:
            db (pyDAL.DAL): database connection
            T (gluon.languages.translator): language translator
                configuration of the selector panel.
LE GAC Renaud's avatar
LE GAC Renaud committed
212
            session (gluon.storage.Storage):
213 214 215 216 217 218 219

        Returns:
            dbui.Node:
                the configuration of a tree node.

        """
        node = None
LE GAC Renaud's avatar
LE GAC Renaud committed
220

221 222 223 224 225 226 227 228 229 230 231 232 233 234
        if session.role == ADMIN:

            node = Node(T("Data institute"))
            leaves = ("affiliation_keys", "projects", "teams")

            node.add_children(leaves, func=to_grid)

            node.add_child(
                T("Relation team / projects"),
                to_grid("organisation"))

        return node

    @staticmethod
LE GAC Renaud's avatar
LE GAC Renaud committed
235
    def data_publication_node(db, T, session):
236 237 238 239 240
        """To define data for publications.

        Args:
            db (pyDAL.DAL): database connection
            T (gluon.languages.translator): language translator
LE GAC Renaud's avatar
LE GAC Renaud committed
241
            session (gluon.storage.Storage):
242 243 244 245 246 247 248

        Returns:
            dbui.Node:
                the configuration of a tree node.

        """
        node = None
LE GAC Renaud's avatar
LE GAC Renaud committed
249

250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
        if session.role in (ADMIN, USER):

            node = Node(T("Data publications"))

            leaves = (
                "categories",
                "collaborations",
                "countries",
                "publications",
                "publishers",
                "status")

            if session.role == USER:
                leaves = (
                    "categories",
                    "publications")

            node.add_children(leaves, func=to_grid)

        return node

    @staticmethod
LE GAC Renaud's avatar
LE GAC Renaud committed
272
    def harvester_node(db, T, session, virtdb):
273 274 275 276 277
        """To run harvester.

        Args:
            db (pyDAL.DAL): database connection
            T (gluon.languages.translator): language translator
LE GAC Renaud's avatar
LE GAC Renaud committed
278
            session (gluon.storage.Storage):
279 280 281 282 283 284 285 286 287
            virtdb (pyDAL.DAL):
                virtual database with the definition of selectors

        Returns:
            dbui.Node:
                the configuration of a tree node.

        """
        node = None
LE GAC Renaud's avatar
LE GAC Renaud committed
288

289 290 291 292
        if session.role in (ADMIN, USER):

            node = Node(T("Harvest"))

LE GAC Renaud's avatar
LE GAC Renaud committed
293
            free_panel = to_panelWithUrlSelector(
294 295 296 297 298
                virtdb.free_harvester_selector,
                baseUrl=URL("harvest", "free_run"),
                selectorTitle="Fill all fields",
                timeout=ONE_HOUR)

LE GAC Renaud's avatar
LE GAC Renaud committed
299
            harvester_panel = to_panelWithUrlSelector(
300 301 302 303 304
                virtdb.harvester_selector,
                baseUrl=URL("harvest", "run"),
                extField="format",
                timeout=ONE_HOUR)

LE GAC Renaud's avatar
LE GAC Renaud committed
305
            run_all_panel = to_panelWithUrlSelector(
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
                virtdb.run_all_harvesters_selector,
                baseUrl=URL("harvest", "run_all"),
                extField="format",
                timeout=ONE_HOUR)

            add_child = node.add_child
            add_child(T("run a free harvester"), free_panel)
            add_child(T("run a harvester"), harvester_panel)
            add_child(T("run all harvesters"), run_all_panel)

            node.sort_children()

        return node

    @staticmethod
LE GAC Renaud's avatar
LE GAC Renaud committed
321
    def help_node(db, T, session):
322 323 324 325 326
        """To deal with documentation and version.

        Args:
            db (pyDAL.DAL): database connection
            T (gluon.languages.translator): language translator
LE GAC Renaud's avatar
LE GAC Renaud committed
327
            session (gluon.storage.Storage):
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 366 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

        Returns:
            dbui.Node:
                the configuration of a tree node.

        """
        node = Node(T("Help"))

        loader = dict(
            autoLoad=True,
            renderer="html",
            url=URL("plugin_dbui", "about"))

        about_panel = Panel(loader=loader, autoScroll=True)

        loader = dict(
            autoLoad=True,
            renderer="html",
            url=URL("plugin_dbui", "documentations_table"))

        doc_panel = Panel(
            loader=loader,
            plugins=["pPanelLoaderException"],
            autoScroll=True)

        loader = dict(
            autoLoad=True,
            renderer="html",
            scripts=True,
            url=URL("default", "guides"))

        guides_panel = Panel(
            loader=loader,
            plugins=["pPanelLoaderException"],
            autoScroll=True)

        loader = dict(
            autoLoad=True,
            renderer="html",
            scripts=True,
            url=URL("plugin_dbui", "versions"))

        version_panel = Panel(loader=loader, plugins=["pPanelLoaderException"])

        # NOTE
        # Immediate translation is required by node.short_children

        add_child = node.add_child
        add_child(T("about", lazy=False), about_panel)
        add_child(T("guides", lazy=False), guides_panel)
        add_child(T("versions", lazy=False), version_panel)

        if session.role == ADMIN:
            node.add_child(T("documentations", lazy=False), doc_panel)

        node.sort_children()

        return node

    @staticmethod
    def report_node(db, T, virtdb):
        """To run report like lists or metrics.

        Args:
            db (pyDAL.DAL): database connection
            T (gluon.languages.translator): language translator
            virtdb (pyDAL.DAL):
                virtual database with the definition of selectors

        Returns:
            dbui.Node:
                the configuration of a tree node.

        """
        node = Node(T("Reports"))

        loader = dict(
            autoLoad=True,
            renderer="html",
            scripts=True,
            url=URL("graphs", "dashboard"))

        dashboard_panel = Panel(
            loader=loader,
            plugins=["pPanelLoaderException"],
            autoScroll=True)

LE GAC Renaud's avatar
LE GAC Renaud committed
415
        graph_panel = to_panelWithUrlSelector(
416 417 418 419
            virtdb.graph_selector,
            baseUrl=URL("graphs", "index"),
            extField="format")

LE GAC Renaud's avatar
LE GAC Renaud committed
420
        list_panel = to_panelWithUrlSelector(
421 422 423 424 425
            virtdb.list_selector,
            baseUrl=URL("lists", "index"),
            extField="format",
            plugins=["pMathJax"])

LE GAC Renaud's avatar
LE GAC Renaud committed
426
        metric_panel = to_panelWithUrlSelector(
427 428 429 430 431 432 433 434 435 436 437 438 439
            virtdb.metric_selector,
            baseUrl=URL("metrics", "index"),
            extField="format")

        add_child = node.add_child
        add_child(T("dashboard"), dashboard_panel)
        add_child(T("lists"), list_panel)
        add_child(T("metrics"), metric_panel)
        add_child(T("graphs"), graph_panel)

        return node

    @staticmethod
LE GAC Renaud's avatar
LE GAC Renaud committed
440
    def wizard_node(db, T, session, virtdb):
441 442 443 444 445
        """To run wizards.

        Args:
            db (pyDAL.DAL): database connection
            T (gluon.languages.translator): language translator
LE GAC Renaud's avatar
LE GAC Renaud committed
446
            session (gluon.storage.Storage):
447 448 449 450 451 452 453 454 455
            virtdb (pyDAL.DAL):
                virtual database with the definition of selectors

        Returns:
            dbui.Node:
                the configuration of a tree node.
        """
        node = None

LE GAC Renaud's avatar
LE GAC Renaud committed
456
        session = current.globalenv["session"]
457 458 459 460
        if session.role in (ADMIN, USER):

            node = Node(T("Wizards"))

LE GAC Renaud's avatar
LE GAC Renaud committed
461
            author_panel = to_panelWithUrlSelector(
462 463 464
                virtdb.authors_selector,
                baseUrl=URL("wizards", "extract_authors"))

LE GAC Renaud's avatar
LE GAC Renaud committed
465
            check_panel = to_panelWithUrlSelector(
466 467 468 469 470
                virtdb.check_selector,
                baseUrl=URL("wizards", "check_validate"),
                extField="format",
                timeout=ONE_HOUR)

LE GAC Renaud's avatar
LE GAC Renaud committed
471
            edit_panel = to_panelWithUrlSelector(
472 473 474 475
                virtdb.edit_insert_selector,
                baseUrl=URL("harvest", "edit_insert"),
                selectorTitle="Fill all fields")

LE GAC Renaud's avatar
LE GAC Renaud committed
476
            marc12_panel = to_panelWithUrlSelector(
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
                virtdb.marc12_selector,
                selectorTitle="Fill all fields",
                baseUrl=URL("harvest", "insert_marcxml"))

            add_child = node.add_child
            add_child(T("extract authors"), author_panel)

            if session.role == ADMIN:
                add_child(T("check and validate"), check_panel)
            else:
                add_child(T("check"), check_panel)

            add_child(T("edit and insert"), edit_panel)
            add_child(T("insert MARCXML"), marc12_panel)

            node.sort_children()

        return node