model_core.py 17.5 KB
Newer Older
LE GAC Renaud's avatar
LE GAC Renaud committed
1 2 3
"""Definitions of the core tables

"""
4 5 6 7
from .callbacks import (INHIBIT_CASCADE_DELETE,
                        INHIBIT_DUPLICATE_PUBLICATION,
                        INHIBIT_PUBLICATION_DELETE_ON_OK,
                        INHIBIT_PUBLICATION_UPDATE_ON_OK)
LE GAC Renaud's avatar
LE GAC Renaud committed
8

9 10 11
from .filters import (CLEAN_COLLABORATION,
                      CLEAN_COLLABORATION_SYNONYM,
                      CLEAN_THESIS_DEFENSE)
LE GAC Renaud's avatar
LE GAC Renaud committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

from gluon import current
from gluon.validators import (IS_EMPTY_OR,
                              IS_IN_DB,
                              IS_INT_IN_RANGE,
                              IS_LENGTH,
                              IS_MATCH,
                              IS_URL)

from plugin_dbui import (CLEAN_COMMA,
                         CLEAN_SPACES,
                         INHIBIT_DELETE_UNDEF,
                         INHIBIT_UPDATE_UNDEF,
                         IS_IN_USET,
                         UNDEF,
                         UNDEF_ID)

from pydal import Field
30 31 32 33 34
from .regex import (REG_COLLABORATION,
                    REG_CONF_DATES,
                    REG_DEFENSE,
                    REG_SUBMITTED,
                    REG_VALID_ORIGIN)
LE GAC Renaud's avatar
LE GAC Renaud committed
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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 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 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 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467

CAT_USUAL = ["article",
             "book",
             "patent",
             "poster",
             "proceeding",
             "report",
             "talk",
             "thesis",
             UNDEF]

TP_SYNONYMS = "List of synonyms, one entry per row."


class Core(object):
    """Create the ``publications`` table and its foreign tables.

    """
    @staticmethod
    def _protect(table):
        """protect the content of the table.

            * Record containing the UNDEF string can not be update or delete.
            * Inhibit cascade delte for foreign elements

        Args:
            table (pyDAL.Table): database table

        """
        table._before_delete.append(INHIBIT_CASCADE_DELETE)
        table._before_delete.append(INHIBIT_DELETE_UNDEF)
        table._before_update.append(INHIBIT_UPDATE_UNDEF)

    @staticmethod
    def define_tables(db, T):
        """define the core tables

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

        """
        # foreign tables
        Core.authors_roles(db, T)
        Core.categories(db, T)
        Core.collaborations(db, T)
        Core.countries(db, T)
        Core.projects(db, T)
        Core.publishers(db, T)
        Core.reports(db, T)
        Core.status(db, T)
        Core.teams(db, T)

        # main tables
        Core.organisation(db, T)
        Core.publications(db, T)

    @staticmethod
    def authors_roles(db, T):
        """authors_roles table

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

        Returns:
            pyDAL.Table

        """
        table = db.define_table(
            "authors_roles",
            Field("role", "string", length=255, default="", notnull=True),
            migrate="authors_roles.table")

        Core._protect(table)

        return table

    @staticmethod
    def categories(db, T):
        """categories table

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

        Returns:
            pyDAL.Table

        """
        table = db.define_table(
            "categories",
            Field("code", "string", length=255, notnull=True, unique=True),
            Field("usual", "string", length=255, default=""),
            Field("definition", "text", notnull=True),
            migrate="categories.table")

        Core._protect(table)

        cat_usual = sorted([T(el) for el in CAT_USUAL])
        table.usual.requires = IS_IN_USET(cat_usual)

        table.code.filter_in = CLEAN_SPACES
        table.usual.filter_in = CLEAN_SPACES

    @staticmethod
    def collaborations(db, T):
        """collaborations table

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

        Returns:
            pyDAL.Table

        """
        tp_collaboration = \
            T("Collaboration(s) signing the publication: "
              "CMS Collaboration or CMS and LHCb Collaborations or "
              "ATLAS Collaboration, CMS Collaboration or "
              "Heavy Flavour Averaging Group or "
              "CTA Consortium.")

        table = db.define_table(
            "collaborations",

            Field("collaboration",
                  "string",
                  length=255,
                  comment=tp_collaboration,
                  notnull=True,
                  unique=True),

            Field("synonyms", "list:string", comment=T(TP_SYNONYMS)),
            migrate="collaborations.table")

        Core._protect(table)

        table.collaboration.filter_in = CLEAN_COLLABORATION
        table.synonyms.filter_in = CLEAN_COLLABORATION_SYNONYM

        table.collaboration.requires = IS_MATCH(REG_COLLABORATION)

        return table

    @staticmethod
    def countries(db, T):
        """countries table

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

        Returns:
            pyDAL.Table

        """
        table = db.define_table(
            "countries",
            Field("country", "string", length=255, notnull=True, unique=True),
            Field("synonyms", "list:string", comment=T(TP_SYNONYMS)),
            migrate="countries.table")

        Core._protect(table)

        table.country.filter_in = CLEAN_SPACES

        return table

    @staticmethod
    def organisation(db, T):
        """organisation table

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

        Returns:
            pyDAL.Table

        """
        table = db.define_table(
            "organisation",

            Field("id_teams",
                  "reference teams",
                  default=UNDEF_ID,
                  label="Team"),

            Field("id_projects",
                  "reference projects",
                  default=UNDEF_ID,
                  label="Project"),

            migrate="organisation.table")

        return table

    @staticmethod
    def projects(db, T):
        """projects table

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

        Returns:
            pyDAL.Table

        """
        table = db.define_table(
            "projects",
            Field("project", "string", length=255, notnull=True, unique=True),
            Field("agencies", "string", length=255, default=""),
            migrate="projects.table")

        Core._protect(table)

        table.project.filter_in = CLEAN_SPACES

        return table

    @staticmethod
    def publications(db, T):
        """publications table

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

        Returns:
            pyDAL.Table

        """
        tp_authors = \
            T("List of authors separated by comma: J. Doe, P.-Y. Smith")

        tp_authors_institute = \
            T("List of authors belonging to your institute, "
              "separated by comma: P.-Y. Doe, J. Smith")

        tp_conference_dates = \
            T("Dates of the conference: 9-10 Oct 2012 "
              "or 29 Oct - 3 Nov 2012 or 3 Dec 2012")

        tp_conference_title = \
            T("The title of the conference. "
              "Use the latex syntax for symbol: $\\alpha$")

        tp_conference_url = \
            T("The URL of the conference: http://....")

        tp_directors = \
            T("List of directors separated by comma: J. Doe, P.-Y. Smith")

        tp_defense = T("Date of defense: 13 Dec 2011")
        tp_first_author = T("The name of the first author: J. Doe")

        tp_origin = \
            T("The URL of the associated record in cds or inspirehep: "
              "http://cds.cern.ch/record/1669557")

        tp_pages = T("Number for the first pages or a range 69-80")

        tp_preprint = \
            T("Preprint identifier separated by comma: arXiv:0906.1516")

        tp_report_numbers = \
            T("Report identifier separated by comma: LHCb-PROC-2008-04")

        tp_submitted = \
            T("Date of submission to a publisher: 2011-12-13 or 2011-12")

        tp_speaker = \
            T("The name of the speaker: P.-Y. Smith")

        tp_title = \
            T("The title of the publication. "
              "Use the latex syntax for symbol: $\\alpha$")

        tp_universities = T("List of university separated by comma")

        tp_url = \
            T("The URL pointing to the pdf file of the the publication "
              "(open access): http://arxiv.org/pdf/1103.2465")

        tp_volume = T("The volume number: 123 or 64-65")
        tp_year = T("Year of the publication")

        table = db.define_table(
            "publications",
            Field("title", "text", comment=tp_title, notnull=True),

            Field("first_author",
                  "string",
                  length=255,
                  comment=tp_first_author,
                  notnull=True),

            Field("authors", "text", comment=tp_authors, notnull=True),

            Field("id_collaborations",
                  "reference collaborations",
                  default=UNDEF_ID,
                  label="Collaboration"),

            Field("id_publishers",
                  "reference publishers",
                  default=UNDEF_ID,
                  label="Revue"),

            Field("year", "integer", comment=tp_year, notnull=True),

            Field("volume",
                  "string",
                  length=255,
                  comment=tp_volume,
                  default=""),

            Field("pages", "string", length=255, comment=tp_pages, default=""),

            Field("submitted",
                  "string",
                  length=255,
                  comment=tp_submitted,
                  notnull=True),

            Field("preprint",
                  "string",
                  length=255,
                  comment=tp_preprint,
                  default=""),

            Field("publication_url",
                  "string",
                  length=255,
                  comment=tp_url,
                  default="",
                  label="PDF file url"),

            Field("conference_title",
                  "text",
                  comment=tp_conference_title,
                  default="",
                  label="Title"),

            Field("conference_url",
                  "string",
                  length=255,
                  comment=tp_conference_url,
                  default="",
                  label="url"),

            Field("conference_dates",
                  "string",
                  length=255,
                  comment=tp_conference_dates,
                  default="",
                  label="Dates"),

            Field("conference_town",
                  "string",
                  length=255,
                  default="",
                  label="Town"),

            Field("id_countries",
                  "reference countries",
                  default=UNDEF_ID,
                  label="Country"),

            Field("conference_speaker",
                  "string",
                  length=255,
                  comment=tp_speaker,
                  default="",
                  label="Speaker"),

            Field("report_numbers",
                  "string",
                  length=255,
                  comment=tp_report_numbers,
                  default="",
                  label="Number(s)"),

            Field("id_reports",
                  "reference reports",
                  default=UNDEF_ID,
                  label="Type"),

            Field("universities",
                  "string",
                  length=255,
                  comment=tp_universities,
                  default=""),

            Field("directors",
                  "string",
                  length=255,
                  comment=tp_directors,
                  default=""),

            Field("defense",
                  "string",
                  length=255,
                  comment=tp_defense,
                  default=""),

            Field("book_isbn", "string", length=255, label="ISBN", default=""),
            Field("book_issn", "string", length=255, label="ISSN", default=""),

            Field("book_chapters",
                  "string",
                  length=255,
                  default="",
                  label="Chapter(s)"),

            Field("authors_institute",
                  "text",
                  comment=tp_authors_institute,
                  notnull=True,
                  label="Authors"),

            Field("id_authors_roles",
                  "reference authors_roles",
                  default=UNDEF_ID,
                  label="Role"),

            Field("id_teams", "reference teams", label="Team", notnull=True),

            Field("id_projects",
                  "reference projects",
468
                  default=UNDEF_ID,
LE GAC Renaud's avatar
LE GAC Renaud committed
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 499 500 501 502 503 504 505 506 507 508 509 510
                  label="Project",
                  notnull=True),

            Field("id_categories",
                  "reference categories",
                  default=UNDEF_ID,
                  label="Category"),

            Field("id_status",
                  "reference status",
                  default=UNDEF_ID,
                  label="Status"),

            Field("origin",
                  "string",
                  length=255,
                  comment=tp_origin,
                  writable=False),

            migrate="publications.table")

        table._before_delete.append(INHIBIT_PUBLICATION_DELETE_ON_OK)
        table._before_insert.append(INHIBIT_DUPLICATE_PUBLICATION)
        table._before_update.append(INHIBIT_PUBLICATION_UPDATE_ON_OK)

        table.authors.filter_in = CLEAN_COMMA
        table.authors_institute.filter_in = CLEAN_COMMA
        table.conference_title.filter_in = CLEAN_SPACES
        table.conference_town.filter_in = CLEAN_SPACES
        table.defense.filter_in = CLEAN_THESIS_DEFENSE
        table.first_author.filter_in = CLEAN_COMMA
        table.title.filter_in = CLEAN_SPACES

        table.authors.requires = IS_LENGTH(65535)

        # rule: 30 Dec 2012
        table.defense.requires = IS_EMPTY_OR(IS_MATCH(REG_DEFENSE))

        # rule: 10-14 Dec 2012 or 28 Jun - 4 Jul 2012
        table.conference_dates.requires = IS_EMPTY_OR(IS_MATCH(REG_CONF_DATES))

        table.conference_url.requires = IS_EMPTY_OR(IS_URL())
511 512 513 514 515

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

LE GAC Renaud's avatar
LE GAC Renaud committed
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
        table.origin.requires = IS_EMPTY_OR(IS_MATCH(REG_VALID_ORIGIN))
        table.publication_url.requires = IS_EMPTY_OR(IS_URL())

        # rule: 2012-12 or 2012-12-31
        table.submitted.requires = IS_MATCH(REG_SUBMITTED)

        table.year.requires = IS_INT_IN_RANGE(1900, current.request.now.year+1)

        return table

    @staticmethod
    def publishers(db, T):
        """publishers table

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

        Returns:
            pyDAL.Table

        """
        table = db.define_table(
            "publishers",

            Field("publisher",
                  "string",
                  length=255,
                  default="",
                  label="Review"),

            Field("abbreviation",
                  "string",
                  length=255,
                  notnull=True,
                  unique=True),

            Field("synonyms", "list:string", comment=T(TP_SYNONYMS)),
            migrate="publishers.table")

        Core._protect(table)

        table.publisher.filter_in = CLEAN_SPACES
        table.abbreviation.filter_in = CLEAN_SPACES

        return table

    @staticmethod
    def reports(db, T):
        """reports table

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

        Returns:
            pyDAL.Table

        """
        table = db.define_table(
            "reports",
            Field("type", "string", length=255, notnull=True, unique=True),
            migrate="reports.table")

        Core._protect(table)

        return table

    @staticmethod
    def status(db, T):
        """status table

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

        Returns:
            pyDAL.Table

        """
        table = db.define_table(
            "status",
            Field("code", "string", length=255, notnull=True, unique=True),
            Field("definition", "text", notnull=True),
            migrate="status.table")

        Core._protect(table)

        return table

    @staticmethod
    def teams(db, T):
        """teams table

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

        Returns:
            pyDAL.Table

        """
        table = db.define_table(
            "teams",
            Field("team", "string", length=255, notnull=True, unique=True),
            Field("domain", "string", length=255, default=""),
            migrate="teams.table")

        Core._protect(table)

        table.team.filter_in = CLEAN_SPACES
        table.domain.filter_in = CLEAN_SPACES

        return table