Commit ed8affdc authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

Migrate controller related to affiliations.

parent 6e9f0f9f
......@@ -24,11 +24,14 @@ MSG_NO_AUTHORS = "<br><br>Removing affiliation failed.<br>"\
"Use INSPIRES instead with the tool 'insert MARCXML'"
MSG_EXISTING_KEY = "Keys already exist!"
MSG_NO_AFFILIATION = "Affiliation is not defined for the selected author."
MSG_NO_AUTHOR = "Author not found!"
MSG_NO_KEYS = "Affiliation keys are not defined!"
MSG_NO_INSTITUTE = "Institute not found in the inspirehep database!"
MSG_NO_PUBLICATION = "Publication not found!"
MSG_NO_SERVER = "Server is not reachable or respond badly!"
MSG_TO_MANY_AFFILIATION = "More than one affiliation for the selected author!"
MSG_TO0_MANY_AFFILIATION = "More than one affiliation for the selected author!"
MSG_TO0_MANY_AUTHOR = "More than one author found!"
def affiliation_institute():
......@@ -49,9 +52,21 @@ def affiliation_institute():
raise HTTP(500, T(MSG_NO_INSTITUTE))
# extract keys defining the affiliation
# u and v are the main keys use in inspirehep and cds
# b is uses by some note in Atlas
keys = (record["110"][k] for k in ("u", "t", "b") if k in record["110"])
# subfields are identifier and futur_identifier
# they are not part of the standard JSON record but add by the factory
if u"corporate_note" not in record:
raise HTTP(500, T(MSG_NO_KEYS))
di = record[u"corporate_note"]
keys = [di[k] for k in di]
# some time the name of the institute is used (ATLAS NOte, ...)
obj = record[u"corporate_name"]
di = (obj[0] if isinstance(obj, list) else obj)
if u"name" in di:
keys.append(di[u"name"])
keys = (dict(key_u=key, key_v="") for key in keys)
# check that the rules does not exist
......@@ -89,36 +104,33 @@ def affiliation_publication():
if record is None:
raise HTTP(500, T(MSG_NO_PUBLICATION))
# find the author
# test all possible patterns (John, Doe; Doe, John; J. Doe; Doe, J. etc)
# for simplicity ignore composed name, e.g. Jean-Pierre
pattern = "^%s, *%s$" % (family_name, first_name)
pattern = "%s|^%s, *%s$" % (pattern, first_name, family_name)
pattern = "%s|^%s, *%s\.?$" % (pattern, family_name, first_name[0])
pattern = "%s|^%s\.?, *%s$" % (pattern, first_name[0], family_name)
reg = re.compile(pattern, re.IGNORECASE)
# find the author and its affiliation
df = record[u"authors"]
key = None
if "700" in record and isinstance(record["700"], list):
for di in record["700"]:
author = di["a"]
query = \
(df.last_name.str.lower() == family_name.lower()) & \
(df.first_name.str.lower() == first_name.lower())
if reg.match(author):
df = df[query]
if isinstance(di["u"], list):
raise HTTP(500, T(MSG_TO_MANY_AFFILIATION))
if len(df) == 0:
raise HTTP(500, T(MSG_NO_AUTHOR))
if "v" in di:
key = dict(key_u=di["u"], key_v=di["v"])
else:
key = dict(key_u=di["u"], key_v="")
break
elif len(df) > 1:
raise HTTP(500, T(MSG_TO0_MANY_AUTHOR))
if key is None:
raise HTTP(500, T(MSG_NO_AUTHOR))
affiliation = df.affiliation.iloc[0]
# reject author with several affiliations
if "|" in affiliation:
raise HTTP(500, MSG_TO0_MANY_AFFILIATION)
elif len(affiliation) == 0:
raise HTTP(500, T(MSG_NO_AFFILIATION))
# check that the rules does not exist
# load new rules
key = dict(key_u=affiliation, key_v="")
if get_id(db.affiliation_keys, **key) is None:
db.affiliation_keys[0] = key
......
......@@ -64,7 +64,7 @@ class InvenioStore(object):
URL string, *e.g.*::
* ``http://cds.cern.ch/record/123456/of=recjson``
* ``http://cds.cern.ch/search?od=id&....
* ``http://cds.cern.ch/search?of=id&....
Keyword Args:
......
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