Commit 858374ab authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

Modify the CheckAndFix methods to handle synonyms for collaboration, country and publisher.

parent 3078262b
......@@ -14,13 +14,14 @@ from invenio_tools import (CheckAndFix,
OAI_URL)
from msg import Msg
from msgcollection import MsgCollection
from plugin_dbui import CALLBACK_ERRORS, get_create_id, get_id, UNDEF_ID
from plugin_dbui import CALLBACK_ERRORS, get_id, UNDEF_ID
MSG_NO_CAT = 'Select a "category" !!!'
MSG_NO_PROJECT = 'Select a "project" !!!'
MSG_NO_TEAM = 'Select a "team" !!!'
MSG_TOOMANY_SYNONYM = "Reject too many %s synonyms."
MSG_NSERT_FAIL = "Fail to insert the new record in the database."
MSG_NO_OAI = "Reject no OAI identifier"
MSG_WELL_FORM_OAI = "Reject OAI is not well formed"
......@@ -212,6 +213,59 @@ class Automaton(object):
return True
def _search(self, tablename, fieldname, value, create=False):
"""Get the database identifier for the record having the database field
matching the value. The database field is defined by the argument
*tablename* and *fieldname*.
Note:
The search is also perform using the synonym field
when nothing is found.
Args:
tablename (unicode): name of the database table.
fieldname (unicode): name of the database field.
value (unicode): the search value.
create(bool): create a new entry in the database table when
it is ``True``
Returns:
int:
* the id of the database record.
* UNDEF_ID if value is not defined.
Raise:
ToolException: when more than one synonym is found.
"""
if not value:
return UNDEF_ID
id_rec = get_id(tablename, fieldname=value)
if id_rec is not None:
return id_rec
# nothing found, have a look to the synonyms
db = self.db
table = db[tablename]
query = table.synonyms.contains(value)
setrows = db(query)
# no synonym found, create the entry
ncount = setrows.count()
if ncount == 0 and create:
return table.insert(fieldname=value)
# one synonym found
elif ncount == 1:
return setrows.select(table.id).first().id
# more than one synonyms - don't know how to choose
else:
msg = MSG_TOOMANY_SYNONYM % tablename
raise ToolException(msg)
def _search_parameters(self, collection):
"""Build the keywords to steer the URL search in invenio store.
The main parameter is the collection and the date range defined
......@@ -364,38 +418,6 @@ class Automaton(object):
self.logs[-1].idle(MSG_IN_DB, kwargs["year"])
return (rec_id, 0)
def get_create_collaboration(self, value):
"""Get the database collaboration identifier.
Create it, if it does not exist.
Args:
value (unicode): the name of the collaboration.
Returns:
int: the id of the collaboration.
"""
if not value:
return UNDEF_ID
return get_create_id(self.db.collaborations, collaboration=value)
def get_create_publisher(self, value):
"""Get the database publisher identifier.
Create it, if it does not exit.
Args:
value (unicode): the abbreviation of the publisher name.
Returns:
int: the id of the publisher.
"""
if not value:
return UNDEF_ID
return get_create_id(self.db.publishers, abbreviation=value)
def insert_record(self, record):
"""Insert the record in the database.
......@@ -590,3 +612,55 @@ class Automaton(object):
return dict(collection_logs=self.collection_logs,
controller=self.controller,
logs=self.logs)
def search_collaboration(self, value):
"""Get the database collaboration identifier.
Create it, if it does not exist.
Args:
value (unicode): the name of the collaboration.
Returns:
int:
* the id of the collaboration.
* UNDEF_ID if value is not defined.
Raise:
ToolException: when more than one synonym is found.
"""
return self._search("collaborations", "collaboration", value, True)
def search_country(self, value):
"""Get the database country identifier.
Args:
value (unicode): the name of the country.
Returns:
int:
* the id of the country.
* UNDEF_ID if value is not defined.
Raise:
ToolException: when more than one synonym is found.
"""
return self._search("countries", "country", value)
def search_publisher(self, value):
"""Get the database publisher identifier.
Args:
value (unicode): the abbreviation of the publisher.
Returns:
int:
* the id of the publisher.
* UNDEF_ID if value is not defined.
Raise:
ToolException: when more than one synonym is found.
"""
return self._search("publishers", "abbreviation", value)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment