model_harvester.py 6.98 KB
Newer Older
LE GAC Renaud's avatar
LE GAC Renaud committed
1 2 3
"""Definitions of the harveser's tables

"""
4 5 6 7
from .callbacks import (INHIBIT_CASCADE_DELETE,
                        INHIBIT_CONTROLLER_INSERT,
                        INHIBIT_CONTROLLER_UPDATE,
                        INHIBIT_HARVESTER)
LE GAC Renaud's avatar
LE GAC Renaud committed
8 9 10 11 12 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 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

from gluon import current

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

from plugin_dbui import (CLEAN_SPACES,
                         INHIBIT_DELETE_UNDEF,
                         INHIBIT_UPDATE_UNDEF,
                         UNDEF_ID)

from pydal import Field


CONTROLLERS = ["articles",
               "notes",
               "preprints",
               "proceedings",
               "reports",
               "talks",
               "theses"]

STORES = ["cds.cern.ch",
          "inspirehep.net"]

TP_CATEGORY = "Publication category associated to the found records."
TP_CONTROLLER = "The name of the harvester."


class Harvester(object):
    """Create the ``harvesters`` 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 harvester's tables

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

        """
        # foreign tables
        Harvester.affilication_keys(db, T)
        Harvester.controllers(db, T)

        # main tables
        Harvester.harvesters(db, T)
        Harvester.my_authors(db, T)

    @staticmethod
    def affilication_keys(db, T):
        """affiliation_keys table

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

        Returns:
            pyDAL.Table

        """
        table = db.define_table(
            "affiliation_keys",
            Field("key_u", "string", length=255, notnull=True),
            Field("key_v", "string", length=255, notnull=False),
            migrate="affiliation_keys.table")

        return table

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

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

        Returns:
            pyDAL.Table

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

            Field("controller",
                  "string",
                  length=255,
                  comment=T(TP_CONTROLLER),
                  label="Harvester",
                  notnull=True),

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

            migrate="controllers.table")

        table._before_insert.append(INHIBIT_CONTROLLER_INSERT)
        table._before_update.append(INHIBIT_CONTROLLER_UPDATE)

        table.controller.filter_in = CLEAN_SPACES

        # NOTE:
        #
        # The list of controllers corresponds to those defined in the
        # harvest_tools. The method get_harvester_tool defines the relation
        # between the controller and the harvester class.
        #
        table.controller.requires = IS_IN_SET(CONTROLLERS)

        return table

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

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

        Returns:
            pyDAL.Table

        """
        tp_collections = \
152 153
            T("the collection to be interrogated, e.g. "
              "'LHCb Papers' or 'find cn lhcb and tc p and not tc c'.")
LE GAC Renaud's avatar
LE GAC Renaud committed
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

        tp_host = \
            T("Address of the invenio store where the search is performed.")

        tp_scan = T("Authorize automatic scan.")

        table = db.define_table(
            "harvesters",
            Field("id_teams", "reference teams", label="Team", notnull=True),

            Field("id_projects",
                  "reference projects",
                  label="Project",
                  notnull=True),

            Field("controller",
                  "string",
                  length=255,
                  comment=T(TP_CONTROLLER),
                  label="Automaton",
                  notnull=True),

            Field("host",
                  "string",
                  length=255,
                  comment=tp_host,
                  default=STORES[0],
                  label="Store",
                  notnull=True),

            Field("collections",
                  "string",
186
                  length=1024,
LE GAC Renaud's avatar
LE GAC Renaud committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
                  comment=tp_collections,
                  notnull=True),

            Field("id_categories",
                  "reference categories",
                  comment=T(TP_CATEGORY),
                  label="Category",
                  notnull=True),

            Field("scan", "boolean", comment=tp_scan, default=False),
            migrate="harvesters.table")

        table._before_insert.append(INHIBIT_HARVESTER)

        table.collections.filter_in = CLEAN_SPACES
        table.controller.filter_in = CLEAN_SPACES
        table.host.filter_in = CLEAN_SPACES

        table.controller.requires = IS_IN_DB(db, "controllers.controller")
206 207 208 209 210

        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")

LE GAC Renaud's avatar
LE GAC Renaud committed
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
        table.host.requires = IS_IN_SET(STORES)

        return table

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

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

        Returns:
            pyDAL.Table

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

        year = current.request.now.year

        table = db.define_table(
            "my_authors",
            Field("year", "integer", default=year, notnull=True),
            Field("id_teams", "reference teams", label="Team", notnull=True),

            Field("id_projects",
                  "reference projects",
                  label="Project",
                  notnull=True),

            Field("authors", "text", comment=tp_authors_cppm, notnull=True),
            migrate="my_authors.table")

246 247 248
        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
249 250 251
        table.year.requires = IS_INT_IN_RANGE(1900, year+1)

        return table