|
27 | 27 | import shutil |
28 | 28 |
|
29 | 29 | from urllib import urlencode |
| 30 | +from collections import defaultdict |
30 | 31 |
|
| 32 | +from invenio.bibrecord import create_record |
31 | 33 | from invenio.config import \ |
32 | 34 | CFG_ACCESS_CONTROL_LEVEL_SITE, \ |
33 | 35 | CFG_SITE_LANG, \ |
|
37 | 39 | CFG_WEBSUBMIT_STORAGEDIR, \ |
38 | 40 | CFG_PREFIX, \ |
39 | 41 | CFG_CERN_SITE |
| 42 | +from invenio.crossrefutils import get_marcxml_for_doi, CrossrefError |
40 | 43 | from invenio import webinterface_handler_config as apache |
41 | 44 | from invenio.dbquery import run_sql |
42 | 45 | from invenio.access_control_engine import acc_authorize_action |
|
67 | 70 | class WebInterfaceSubmitPages(WebInterfaceDirectory): |
68 | 71 |
|
69 | 72 | _exports = ['summary', 'sub', 'direct', '', 'attachfile', 'uploadfile', \ |
70 | | - 'getuploadedfile', 'upload_video', ('continue', 'continue_')] |
| 73 | + 'getuploadedfile', 'upload_video', ('continue', 'continue_'), \ |
| 74 | + 'doilookup'] |
71 | 75 |
|
72 | 76 | def uploadfile(self, req, form): |
73 | 77 | """ |
@@ -824,7 +828,6 @@ def sub(self, req, form): |
824 | 828 | url = "%s/submit/direct?%s" % (CFG_SITE_SECURE_URL, urlencode(params, doseq=True)) |
825 | 829 | redirect_to_url(req, url) |
826 | 830 |
|
827 | | - |
828 | 831 | def summary(self, req, form): |
829 | 832 | args = wash_urlargd(form, { |
830 | 833 | 'doctype': (str, ''), |
@@ -877,6 +880,38 @@ def summary(self, req, form): |
877 | 880 | values = values, |
878 | 881 | ) |
879 | 882 |
|
| 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 | + |
880 | 915 | def index(self, req, form): |
881 | 916 |
|
882 | 917 | args = wash_urlargd(form, { |
|
0 commit comments