model_selector.py 16.1 KB
Newer Older
LE GAC Renaud's avatar
LE GAC Renaud committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
# -*- coding: utf-8 -*-
"""Definitions of the selector(s)

"""
from gluon import current

from gluon.validators import (IS_IN_DB,
                              IS_LENGTH,
                              IS_INT_IN_RANGE,
                              IS_IN_SET,
                              IS_MATCH)

from harvest_tools import DRY_RUN
from model_harvester import CONTROLLERS, STORES
from pydal import Field


FORMATS = ["bib", "html", "odt", "pdf", "tex"]
MODE_CHANGE_STATUS = "change status"
MODE_LOAD_IN_DB = "load in the database"

22 23 24 25
MONTH_SUBMIT = "month of submission"
YEAR_PUBLISH = "year of publication"
YEAR_SUBMIT = "year of submission"

LE GAC Renaud's avatar
LE GAC Renaud committed
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 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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226

class Selector(object):
    """Create virtual tables used to generate selector interfaces.

    """

    @staticmethod
    def define_tables(virtdb, db, T):
        """Define tables use for selectors.

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

        """
        Selector.authors_selector(virtdb, db, T)
        Selector.check_selector(virtdb, db, T)
        Selector.edit_insert_selector(virtdb, db, T)
        Selector.free_harvester_selector(virtdb, db, T)
        Selector.graph_selector(virtdb, db, T)
        Selector.harvester_selector(virtdb, db, T)
        Selector.list_selector(virtdb, db, T)
        Selector.marc12_selector(virtdb, db, T)
        Selector.metric_selector(virtdb, db, T)
        Selector.run_all_harvesters_selector(virtdb, db, T)

    @staticmethod
    def authors_selector(virtdb, db, T):
        """authors selector.

        Note:
            selector table are store in a virtual database.

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

        Returns:
            pyDAL.Table

        """
        tp_authors = \
            T("String containing the author names with their "
              "institutes number.")

        tp_affiliation = \
            T("Institute number associated to CPPM authors")

        table = virtdb.define_table(
            "authors_selector",

            Field("authors",
                  "text",
                  comment=tp_authors,
                  requires=IS_LENGTH(65535)),

            Field("affiliation", "string", comment=tp_affiliation))

        return table

    @staticmethod
    def check_selector(virtdb, db, T):
        """check selector.

        Note:
            selector table are store in a virtual database.

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

        Returns:
            pyDAL.Table

        """
        mode_dry_run = T(DRY_RUN)

        table = virtdb.define_table(
            "check_selector",
            Field("year", "integer", default=current.request.now.year),
            Field("id_teams", "reference teams", label="Team"),
            Field("id_projects", "reference projects", label="Project"),
            Field("id_categories", "reference categories", label="Category"),
            Field("mode", "string", default=mode_dry_run),
            Field("format", "string", default="html", label="Report format"))

        table.id_categories.requires = IS_IN_DB(db, "categories.code")
        table.format.requires = IS_IN_SET(["html", "pdf", "tex"])
        table.id_projects.requires = IS_IN_DB(db, "projects.project")
        table.id_teams.requires = IS_IN_DB(db, "teams.team")

        table.mode.requires = \
            IS_IN_SET([mode_dry_run, T(MODE_CHANGE_STATUS)])

        return table

    @staticmethod
    def edit_insert_selector(virtdb, db, T):
        """edit and insert selector.

        Note:
            selector table are store in a virtual database.

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

        Returns:
            pyDAL.Table

        """
        tp_record_id = T("The identifier of the record in the invenio store")

        table = virtdb.define_table(
            "edit_insert_selector",
            Field("id_teams", "reference teams", label="Team"),
            Field("id_projects", "reference projects", label="Project"),
            Field("host", "string", default=STORES[0], label="Store"),
            Field("record_id", "integer", comment=tp_record_id),
            Field("controller", "string", label="Automaton"),
            Field("id_categories", "reference categories", label="Category"))

        table.controller.requires = IS_IN_SET(CONTROLLERS)
        table.host.requires = IS_IN_SET(STORES)
        table.id_categories.requires = IS_IN_DB(db, "categories.code")
        table.id_projects.requires = IS_IN_DB(db, "projects.project")
        table.id_teams.requires = IS_IN_DB(db, "teams.team")

        return table

    @staticmethod
    def free_harvester_selector(virtdb, db, T):
        """free harvester selector.

        Note:
            selector table are store in a virtual database.

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

        Returns:
            pyDAL.Table

        """
        mode_dry_run = T(DRY_RUN)
        year = current.request.now.year

        table = virtdb.define_table(
            "free_harvester_selector",
            Field("year_start", "integer", default=year),
            Field("year_end", "integer"),
            Field("id_teams", "reference teams", label="Team"),
            Field("id_projects", "reference projects", label="Project"),
            Field("host", "string", default=STORES[1], label="Store"),
            Field("collections", "string", default="find ..."),
            Field("ratio", "double", default=1.0),
            Field("controller", "string", label="Automaton"),
            Field("id_categories", "reference categories", label="Category"),
            Field("mode", "string", default=mode_dry_run))

        table.host.requires = IS_IN_SET(STORES)
        table.mode.requires = IS_IN_SET([mode_dry_run, MODE_LOAD_IN_DB])

        table.id_categories.requires = IS_IN_DB(db, "categories.code")
        table.id_projects.requires = IS_IN_DB(db, "projects.project")
        table.id_teams.requires = IS_IN_DB(db, "teams.team")

        harvester_start_year = current.app.havester_start_year
        if harvester_start_year:

            table.year_start.requires = \
                IS_INT_IN_RANGE(harvester_start_year, year)

            table.year_end.requires = \
                IS_INT_IN_RANGE(harvester_start_year, year)

        return table

    @staticmethod
    def graph_selector(virtdb, db, T):
        """graph selector.

        Note:
            selector table are store in a virtual database.

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

        Returns:
            pyDAL.Table

        """
        year = current.request.now.year
227
        month_submit = T(MONTH_SUBMIT)
LE GAC Renaud's avatar
LE GAC Renaud committed
228 229 230 231 232 233 234 235 236 237 238 239

        table = virtdb.define_table(
            "graph_selector",
            Field("year_start", "integer", default=year),
            Field("year_end", "integer"),
            Field("id_teams", "reference teams", label="Team"),
            Field("id_projects", "reference projects", label="Project"),
            Field("id_categories", "reference categories", label="Category"),
            Field("author", "string"),
            Field("id_authors_roles", "reference authors_roles", label="Role"),
            Field("id_graphs", "reference graphs", label="Graph"),
            Field("cumulative", "boolean", default=True),
240
            Field("time", "string", default=month_submit),
LE GAC Renaud's avatar
LE GAC Renaud committed
241 242 243 244 245 246 247 248 249
            Field("format", "string", default="html", label="Format"))

        table.id_authors_roles.requires = IS_IN_DB(db, "authors_roles.role")
        table.id_categories.requires = IS_IN_DB(db, "categories.code")
        table.id_graphs.requires = IS_IN_DB(db, "graphs.graph")
        table.id_projects.requires = IS_IN_DB(db, "projects.project")
        table.id_teams.requires = IS_IN_DB(db, "teams.team")

        table.format.requires = IS_IN_SET(["html", "pdf", "png"])
250 251 252

        table.time.requires = \
            IS_IN_SET([month_submit, T(YEAR_SUBMIT), T(YEAR_PUBLISH)])
LE GAC Renaud's avatar
LE GAC Renaud committed
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272

        return table

    @staticmethod
    def harvester_selector(virtdb, db, T):
        """harvester selector.

        Note:
            selector table are store in a virtual database.

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

        Returns:
            pyDAL.Table

        """
        mode_dry_run = T(DRY_RUN)
273

LE GAC Renaud's avatar
LE GAC Renaud committed
274 275 276 277 278 279 280 281 282 283 284 285 286
        year = current.request.now.year

        table = virtdb.define_table(
            "harvester_selector",
            Field("year_start", "integer", default=year),
            Field("year_end", "integer"),
            Field("id_teams", "reference teams", label="Team"),
            Field("id_projects", "reference projects", label="Project"),
            Field("controller", "string", label="Automaton"),
            Field("mode", "string", default=mode_dry_run),
            Field("format", "string", default="html", label="Report format"))

        table.format.requires = IS_IN_SET(["html", "pdf", "tex"])
287
        table.mode.requires = IS_IN_SET([mode_dry_run, T(MODE_LOAD_IN_DB)])
LE GAC Renaud's avatar
LE GAC Renaud committed
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 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 362 363 364 365 366 367 368 369 370 371 372 373 374

        table.id_projects.requires = IS_IN_DB(db, "projects.project")
        table.id_teams.requires = IS_IN_DB(db, "teams.team")

        harvester_start_year = current.app.havester_start_year
        if harvester_start_year:
            table.year_start.requires = \
                IS_INT_IN_RANGE(harvester_start_year, year)

            table.year_end.requires = \
                IS_INT_IN_RANGE(harvester_start_year, year)

        return table

    @staticmethod
    def list_selector(virtdb, db, T):
        """list selector.

        Note:
            selector table are store in a virtual database.

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

        Returns:
            pyDAL.Table

        """
        year = current.request.now.year

        table = virtdb.define_table(
            "list_selector",
            Field("year_start", "integer", default=year),
            Field("year_end", "integer"),
            Field("id_teams", "reference teams", label="Team"),
            Field("id_projects", "reference projects", label="Project"),
            Field("id_categories", "reference categories", label="Category"),
            Field("author", "string"),
            Field("id_authors_roles", "reference authors_roles", label="Role"),
            Field("id_lists", "reference lists", label="List"),
            Field("format", "string", default="html"))

        table.id_authors_roles.requires = IS_IN_DB(db, "authors_roles.role")
        table.id_categories.requires = IS_IN_DB(db, "categories.code")
        table.id_lists.requires = IS_IN_DB(db, "lists.list")
        table.id_projects.requires = IS_IN_DB(db, "projects.project")
        table.id_teams.requires = IS_IN_DB(db, "teams.team")
        table.format.requires = IS_IN_SET(FORMATS)

        return table

    @staticmethod
    def marc12_selector(virtdb, db, T):
        """marc12 selector.

        Note:
            selector table are store in a virtual database.

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

        Returns:
            pyDAL.Table

        """
        mode_dry_run = T(DRY_RUN)

        tp_xml = T("The XML string describing the MARC12 record. "
                   'It Starts with "<?xml" and ends with "</collection>"')

        table = virtdb.define_table(
            "marc12_selector",
            Field("id_teams", "reference teams", label="Team"),
            Field("id_projects", "reference projects", label="Project"),
            Field("xml", "text", comment=tp_xml, label="MARCXML"),
            Field("controller", "string", label="Automaton"),
            Field("id_categories", "reference categories", label="Category"),
            Field("mode", "string", default=mode_dry_run))

        table.controller.requires = IS_IN_SET(CONTROLLERS)
        table.id_categories.requires = IS_IN_DB(db, "categories.code")
        table.id_projects.requires = IS_IN_DB(db, "projects.project")
        table.id_teams.requires = IS_IN_DB(db, "teams.team")
375
        table.mode.requires = IS_IN_SET([mode_dry_run, T(MODE_LOAD_IN_DB)])
LE GAC Renaud's avatar
LE GAC Renaud committed
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 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
        table.xml.requires = IS_MATCH("<\?xml*")

        return table

    @staticmethod
    def metric_selector(virtdb, db, T):
        """metric selector.

        Note:
            selector table are store in a virtual database.

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

        Returns:
            pyDAL.Table

        """
        year = current.request.now.year

        table = virtdb.define_table(
            "metric_selector",
            Field("year_start", "integer", default=year),
            Field("year_end", "integer"),
            Field("id_teams", "reference teams", label="Team"),
            Field("id_projects", "reference projects", label="Project"),
            Field("id_categories", "reference categories", label="Category"),
            Field("author", "string"),
            Field("id_authors_roles", "reference authors_roles", label="Role"),
            Field("id_metrics", "reference metrics", label="Metric"))

        table.id_authors_roles.requires = IS_IN_DB(db, "authors_roles.role")
        table.id_categories.requires = IS_IN_DB(db, "categories.code")
        table.id_metrics.requires = IS_IN_DB(db, "metrics.metric")
        table.id_projects.requires = IS_IN_DB(db, "projects.project")
        table.id_teams.requires = IS_IN_DB(db, "teams.team")

        return table

    @staticmethod
    def run_all_harvesters_selector(virtdb, db, T):
        """run all harvesters selector.

        Note:
            selector table are store in a virtual database.

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

        Returns:
            pyDAL.Table

        """
        mode_dry_run = T(DRY_RUN)
        year = current.request.now.year

        table = virtdb.define_table(
            "run_all_harvesters_selector",
            Field("year_start", "integer", default=year),
            Field("year_end", "integer"),
            Field("id_teams", "reference teams", label="Team"),
            Field("id_projects", "reference projects", label="Project"),
            Field("mode", "string", default=mode_dry_run),
            Field("format", "string", default="html", label="Report format"))

        table.format.requires = IS_IN_SET(["html", "pdf", "tex"])
446
        table.mode.requires = IS_IN_SET([mode_dry_run, T(MODE_LOAD_IN_DB)])
LE GAC Renaud's avatar
LE GAC Renaud committed
447 448 449 450 451 452 453 454 455 456 457 458 459 460

        table.id_projects.requires = IS_IN_DB(db, "projects.project")
        table.id_teams.requires = IS_IN_DB(db, "teams.team")

        harvester_start_year = current.app.harvester_start_year
        if harvester_start_year:

            table.year_start.requires = \
                IS_INT_IN_RANGE(harvester_start_year, year)

            table.year_end.requires = \
                IS_INT_IN_RANGE(harvester_start_year, year)

        return table