Skip to content

Commit 83d1d96

Browse files
committed
Merge pull request #56 from switowski/websubmit_prefill_from_doi
WebSubmit: doilookup function in webinterface
2 parents 7d3ad99 + 049b6a2 commit 83d1d96

1 file changed

Lines changed: 37 additions & 2 deletions

File tree

modules/websubmit/lib/websubmit_webinterface.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
import shutil
2828

2929
from urllib import urlencode
30+
from collections import defaultdict
3031

32+
from invenio.bibrecord import create_record
3133
from invenio.config import \
3234
CFG_ACCESS_CONTROL_LEVEL_SITE, \
3335
CFG_SITE_LANG, \
@@ -37,6 +39,7 @@
3739
CFG_WEBSUBMIT_STORAGEDIR, \
3840
CFG_PREFIX, \
3941
CFG_CERN_SITE
42+
from invenio.crossrefutils import get_marcxml_for_doi, CrossrefError
4043
from invenio import webinterface_handler_config as apache
4144
from invenio.dbquery import run_sql
4245
from invenio.access_control_engine import acc_authorize_action
@@ -67,7 +70,8 @@
6770
class WebInterfaceSubmitPages(WebInterfaceDirectory):
6871

6972
_exports = ['summary', 'sub', 'direct', '', 'attachfile', 'uploadfile', \
70-
'getuploadedfile', 'upload_video', ('continue', 'continue_')]
73+
'getuploadedfile', 'upload_video', ('continue', 'continue_'), \
74+
'doilookup']
7175

7276
def uploadfile(self, req, form):
7377
"""
@@ -824,7 +828,6 @@ def sub(self, req, form):
824828
url = "%s/submit/direct?%s" % (CFG_SITE_SECURE_URL, urlencode(params, doseq=True))
825829
redirect_to_url(req, url)
826830

827-
828831
def summary(self, req, form):
829832
args = wash_urlargd(form, {
830833
'doctype': (str, ''),
@@ -877,6 +880,38 @@ def summary(self, req, form):
877880
values = values,
878881
)
879882

883+
def doilookup(self, req, form):
884+
"""
885+
Returns the metadata from the crossref website based on the DOI.
886+
"""
887+
args = wash_urlargd(form, {
888+
'doi': (str, '')})
889+
response = defaultdict(list)
890+
if args['doi']:
891+
doi = args['doi']
892+
try:
893+
marcxml_template = get_marcxml_for_doi(doi)
894+
except CrossrefError:
895+
# Just ignore Crossref errors
896+
pass
897+
else:
898+
record = create_record(marcxml_template)[0]
899+
if record:
900+
# We need to convert this record structure to a simple dictionary
901+
for key, value in record.items(): # key, value = (773, [([('0', 'PER:64142'), ...], ' ', ' ', '', 47)])
902+
for val in value: # val = ([('0', 'PER:64142'), ...], ' ', ' ', '', 47)
903+
ind1 = val[1].replace(" ", "_")
904+
ind2 = val[2].replace(" ", "_")
905+
for (k, v) in val[0]: # k, v = ('0', 'PER:5409')
906+
response[key+ind1+ind2+k].append(v)
907+
# The output dictionary is something like:
908+
# {"100__a": ['Smith, J.'],
909+
# "700__a": ['Anderson, J.', 'Someoneelse, E.'],
910+
# "700__u": ['University1', 'University2']}
911+
912+
# return dictionary as JSON
913+
return json.dumps(response)
914+
880915
def index(self, req, form):
881916

882917
args = wash_urlargd(form, {

0 commit comments

Comments
 (0)