forked from eScholarship/janeway_ezid_plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogic.py
429 lines (335 loc) · 18.1 KB
/
logic.py
1
2
3
4
5
6
7
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
152
153
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
"""
This module contains the logic for the EZID plugin for Janeway
"""
__copyright__ = "Copyright (c) 2020, The Regents of the University of California"
__author__ = "Hardy Pottinger & Mahjabeen Yucekul"
__license__ = "BSD 3-Clause"
__maintainer__ = "California Digital Library"
import re
from urllib.parse import quote
import urllib.request as urlreq
import json
# import pdb # use for debugging
from django.core.validators import URLValidator, ValidationError
from django.conf import settings
from django.utils import timezone
from django.template.loader import render_to_string
from utils.logger import get_logger
from utils import setting_handler
from .models import RepoEZIDSettings
logger = get_logger(__name__)
# disable to too many branches warning for PyLint
# pylint: disable=R0912
def orcid_validation_check(input_string):
''' Determine whether the given input_string is a valid ORCID '''
regex = re.compile('https?://orcid.org/[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{3}[X0-9]{1}$')
match = regex.match(str(input_string))
return bool(match)
def normalize_author_metadata(preprint_authors):
''' returns a list of authors in dictionary format using a list of author objects '''
#example: {"given_name": "Hardy", "surname": "Pottinger", "ORCID": "https://orcid.org/0000-0001-8549-9354"},
author_list = []
for author in preprint_authors:
# build our new_author dictionary
new_author = dict()
contributor = author.account
if contributor is None:
logger.warn('A Preprintauthor.account object is None, this should not be possible... skipping null author.')
else:
if contributor.first_name:
new_author['given_name'] = contributor.first_name
else:
logger.info('EZID: missing author first name encountered, omitting given_name from EZID minting request...')
if contributor.last_name:
new_author['surname'] = contributor.last_name
else:
logger.info('EZID: missing author last name encountered, attempting to use first name as surname in EZID minting request, since surname is mandatory...')
if contributor.first_name:
new_author['surname'] = contributor.first_name
del new_author['given_name']
else:
logger.warning('EZID: no usable name found for author...')
if contributor.orcid:
if contributor.orcid.startswith('http'):
usable_orcid = contributor.orcid
else:
usable_orcid = 'https://orcid.org/' + contributor.orcid
if orcid_validation_check(usable_orcid):
new_author['ORCID'] = usable_orcid
else:
logger.warning('EZID: unsuable ORCID value of "' + usable_orcid + '" encountered, omitting from EZID minting request...')
author_list.append(new_author)
return author_list
class EzidHTTPErrorProcessor(urlreq.HTTPErrorProcessor):
''' Error Processor, required to let 201 responses pass '''
def http_response(self, request, response):
if response.code == 201:
my_return = response
else:
my_return = urlreq.HTTPErrorProcessor.http_response(self, request, response)
return my_return
https_response = http_response
def send_create_request(data, id, username, password, endpoint_url):
''' sends a create request to EZID '''
method = "PUT"
path = 'id/doi:{}'.format(encode(id))
request_url = f"{endpoint_url}/{path}"
opener = urlreq.build_opener(EzidHTTPErrorProcessor())
ezid_handler = urlreq.HTTPBasicAuthHandler()
ezid_handler.add_password("EZID", endpoint_url, username, password)
opener.add_handler(ezid_handler)
request = urlreq.Request(request_url)
request.get_method = lambda: method
request.add_header("Content-Type", "text/plain; charset=UTF-8")
request.data = data.encode("UTF-8")
try:
connection = opener.open(request)
response = connection.read()
return response.decode("UTF-8")
except urlreq.HTTPError as ezid_error:
#print("%d %s\n" % (ezid_error.code, ezid_error.msg))
if ezid_error.fp is not None:
response = ezid_error.fp.read().decode("utf-8")
if not response.endswith("\n"):
response += "\n"
#print(response)
return response
def send_mint_request(data, shoulder, username, password, endpoint_url):
''' sends a mint request to EZID '''
method = "POST"
path = 'shoulder/' + encode(shoulder)
opener = urlreq.build_opener(EzidHTTPErrorProcessor())
ezid_handler = urlreq.HTTPBasicAuthHandler()
ezid_handler.add_password("EZID", endpoint_url, username, password)
opener.add_handler(ezid_handler)
request = urlreq.Request("%s/%s" % (endpoint_url, path))
request.get_method = lambda: method
request.add_header("Content-Type", "text/plain; charset=UTF-8")
request.data = data.encode("UTF-8")
try:
connection = opener.open(request)
response = connection.read()
return response.decode("UTF-8")
except urlreq.HTTPError as ezid_error:
print("%d %s\n" % (ezid_error.code, ezid_error.msg))
if ezid_error.fp is not None:
response = ezid_error.fp.read()
# FIXME: this code throws errors, endswith doesn't work with response
# if not response.endswith("\n"):
# response += "\n"
print(response)
def send_update_request(data, update_id, username, password, endpoint_url):
''' sends an update request to EZID '''
method = "POST"
path = 'id/doi:' + encode(update_id)
# print('path: ' + path)
opener = urlreq.build_opener(EzidHTTPErrorProcessor())
ezid_handler = urlreq.HTTPBasicAuthHandler()
ezid_handler.add_password("EZID", endpoint_url, username, password)
opener.add_handler(ezid_handler)
request = urlreq.Request("%s/%s" % (endpoint_url, path))
request.get_method = lambda: method
request.add_header("Content-Type", "text/plain; charset=UTF-8")
request.data = data.encode("UTF-8")
try:
connection = opener.open(request)
response = connection.read()
return response.decode("UTF-8")
except urlreq.HTTPError as ezid_error:
print("%d %s\n" % (ezid_error.code, ezid_error.msg))
if ezid_error.fp is not None:
response = ezid_error.fp.read().decode("utf-8")
if not response.endswith("\n"):
response += "\n"
print(response)
def encode(txt):
''' encode a text string '''
return quote(txt, ":/")
def mint_doi_via_ezid(ezid_config, ezid_metadata, template):
''' Sends a mint request for the specified config, using the provided data '''
# ezid_config dictionary contains values for the following keys: shoulder, username, password, endpoint_url
# ezid_data dicitionary contains values for the following keys: target_url, group_title, contributors, title, published_date, accepted_date
# add a timestamp to our metadata, we'll need it
ezid_metadata['now'] = timezone.now()
if ezid_metadata.get('published_doi') is not None:
#we cannot trust that the published_doi has been validated, or is usable as a URL, so let's do that now
logger.debug('validating published_doi')
validator = URLValidator()
try:
validator(ezid_metadata.get('published_doi'))
except ValidationError:
logger.error('invalid URL, published_doi: %s for preprint: %s', ezid_metadata.get('published_doi'), ezid_metadata.get('target_url'))
del ezid_metadata['published_doi'] # this is not a permanent deletion
template_context = ezid_metadata
crossref_template = render_to_string(template, template_context)
logger.debug(crossref_template)
metadata = crossref_template.replace('\n', '').replace('\r', '')
# uncomment this to validate the metadata payload
# print('\n\n')
# print('Using this metadata:')
# print('\n\n')
# print(metadata)
# build the payload
payload = 'crossref: ' + metadata + '\n_crossref: yes\n_profile: crossref\n_target: ' + ezid_metadata['target_url'] + '\n_owner: ' + ezid_config['owner']
# print('\n\npayload:\n\n')
# print(payload)
return send_mint_request(payload, ezid_config['shoulder'], ezid_config['username'], ezid_config['password'], ezid_config['endpoint_url'])
def update_doi_via_ezid(ezid_config, ezid_metadata, template):
''' Sends an update request for the specified config, using the provided data '''
# ezid_config dictionary contains values for the following keys: shoulder, username, password, endpoint_url
# ezid_metadata dicitionary contains values for the following keys: update_id, target_url, group_title, contributors, title, published_date, accepted_date
# add a timestamp to our metadata, we'll need it
ezid_metadata['now'] = timezone.now()
if ezid_metadata.get('published_doi') is not None:
#we cannot trust that the published_doi has been validated, or is usable as a URL, so let's do that now
logger.debug('validating published_doi')
validator = URLValidator()
try:
validator(ezid_metadata.get('published_doi'))
except ValidationError:
logger.error('invalid URL, published_doi: %s for preprint: %s', ezid_metadata.get('published_doi'), ezid_metadata.get('target_url'))
del ezid_metadata['published_doi'] # this is not a permanent deletion
template_context = ezid_metadata
crossref_template = render_to_string(template, template_context)
logger.debug(crossref_template)
metadata = crossref_template.replace('\n', '').replace('\r', '')
# uncomment this to validate the metadata payload
# print('\n\n')
# print('Using this metadata:')
# print('\n\n')
# print(metadata)
# build the payload
payload = 'crossref: ' + metadata + '\n_crossref: yes\n_profile: crossref\n_target: ' + ezid_metadata['target_url'] + '\n_owner: ' + ezid_config['owner']
# print('\n\npayload:\n\n')
# print(payload)
return send_update_request(payload, ezid_metadata['update_id'], ezid_config['username'], ezid_config['password'], ezid_config['endpoint_url'])
def create_doi_via_ezid(ezid_config, ezid_metadata, template):
''' Sends a create request for the specified config, using the provided data '''
ezid_metadata['now'] = timezone.now()
template_context = ezid_metadata
crossref_template = render_to_string(template, template_context)
logger.debug(crossref_template)
metadata = crossref_template.replace('\n', '').replace('\r', '')
# uncomment this to validate the metadata payload
# print('\n\n')
# print('Using this metadata:')
# print('\n\n')
# print(metadata)
# build the payload
payload = 'crossref: ' + metadata + '\n_crossref: yes\n_profile: crossref\n_target: ' + ezid_metadata['target_url'] + '\n_owner: ' + ezid_config['owner']
# print('\n\npayload:\n\n')
# print(payload)
return send_create_request(payload, ezid_metadata['doi'], ezid_config['username'], ezid_config['password'], ezid_config['endpoint_url'])
def preprint_publication(**kwargs):
''' hook script for the preprint_publication event '''
logger.debug('>>> preprint_publication called, mint an EZID DOI...')
preprint = kwargs.get('preprint')
request = kwargs.get('request')
# check to see if this preprint already has a DOI, if so, our job is done, do not try to make a new one
if preprint.preprint_doi:
logger.debug('preprint already has a DOI:')
logger.debug(preprint.preprint_doi)
logger.debug('No need to mint a new DOI, skipping.')
return None
# gather metadata required for minting a DOI via EZID
target_url = preprint.url
group_title = preprint.subject.values_list()[0][2]
title = preprint.title.replace('%', '%25')
published_doi = preprint.doi
abstract = preprint.abstract.replace('%', '%25')
accepted_date = {'month':preprint.date_accepted.month, 'day':preprint.date_accepted.day, 'year':preprint.date_accepted.year}
published_date = {'month':preprint.date_published.month, 'day':preprint.date_published.day, 'year':preprint.date_published.year}
contributors = normalize_author_metadata(preprint.preprintauthor_set.all())
#some notes on the metatdata required:
# [x] target_url (direct link to preprint)
# [x] group_title ( preprint.subject.values_list()[0][2] ) grab the first subject
# [x] contributors - needs to be a list, with a dictionary per row:
# "person_name": [{"@sequence": "first", "@contributor_role": "author", "given_name": "Hardy", "surname": "Pottinger", "ORCID": "https://orcid.org/0000-0001-8549-9354"},]
# (preprint.preprintauthor_set is an object ref, work with it, preprintauthor_set.all() would get you a list of all authors)
# [x] title (preprint.title)
# [x] posted_date (preprint.date_published, is a datetime object)
# [x] acceptance_date (preprint.date_accepted, is a datetime object)
logger.debug("preprint url: " + target_url)
logger.debug("title: " + title)
logger.debug("group_title: " + group_title)
logger.debug("contributors: " + json.dumps(contributors))
logger.debug("accepted_date: " + json.dumps(accepted_date))
logger.debug("published_date: " + json.dumps(published_date))
logger.debug('BEGIN MINTING REQUEST...')
# prepare two dictionaries to feed into the mint_doi_via_ezid function
ezid_settings = RepoEZIDSettings.objects.get(repo=preprint.repository)
ezid_config = {'shoulder': ezid_settings.ezid_shoulder,
'username': ezid_settings.ezid_username,
'password': ezid_settings.ezid_password,
'endpoint_url': ezid_settings.ezid_endpoint_url,
'owner': ezid_settings.ezid_owner}
ezid_metadata = {'target_url': target_url, 'group_title': group_title, 'contributors': contributors, 'title': title, 'published_date': published_date, 'accepted_date': accepted_date, 'published_doi': published_doi, 'abstract': abstract}
logger.debug('ezid_config: ' + json.dumps(ezid_config))
logger.debug('ezid_metadata: '+ json.dumps(ezid_metadata))
ezid_result = mint_doi_via_ezid(ezid_config, ezid_metadata, 'ezid/posted_content.xml')
# if the ezid_result is a string, it's probably a success, check to be sure
if isinstance(ezid_result, str):
if ezid_result.startswith('success:'):
new_doi = re.search("doi:([0-9A-Z./]+)", ezid_result).group(1)
logger.debug('DOI successfully created: ' + new_doi)
preprint.preprint_doi = new_doi
preprint.save()
logger.debug('DOI added to preprint Janeway object and saved. A preprint is born!')
else:
logger.error('EZID DOI creation failed for preprint.pk: ' + preprint.pk + ' ...')
logger.error('ezid_result: ' + ezid_result)
else:
logger.error('EZID DOI creation failed for preprint.pk: ' + preprint.pk + ' ...')
logger.error(ezid_result.msg)
# TODO: PUBD-118 fire a metadata update when a new preprint version is created
# def preprint_version_update(**kwargs):
# ''' hook script for the preprint_version_update event '''
# logger.debug('>>> preprint_version_update called, update DOI metadata via EZID...')
# preprint = kwargs.get('preprint')
# request = kwargs.get('request')
# logger.debug("preprint.id = " + preprint.id)
# logger.debug("request: " + request)
def get_setting(name, journal):
return setting_handler.get_setting('plugin:ezid', name, journal).processed_value
def get_journal_metadata(article):
target_url = article.remote_url
ezid_config = { 'username': get_setting('ezid_plugin_username', article.journal),
'password': get_setting('ezid_plugin_password', article.journal),
'endpoint_url': get_setting('ezid_plugin_endpoint_url', article.journal),
'owner': setting_handler.get_setting('Identifiers', 'crossref_registrant', article.journal).processed_value,}
ezid_metadata = {'target_url': target_url,
'article': article,
'doi': article.get_doi(),
'depositor_name': setting_handler.get_setting('Identifiers', 'crossref_name', article.journal).processed_value,
'depositor_email': setting_handler.get_setting('Identifiers', 'crossref_email', article.journal).processed_value,
'registrant': setting_handler.get_setting('Identifiers', 'crossref_registrant', article.journal).processed_value,}
return ezid_config, ezid_metadata
def process_ezid_result(article, action, ezid_result):
if isinstance(ezid_result, str):
if ezid_result.startswith('success:'):
doi = re.search("doi:([0-9A-Z./]+)", ezid_result).group(1)
logger.debug('DOI {} success: {}'.format(action, doi))
return True, ezid_result
else:
logger.error('EZID DOI {} failed for article.pk: {}...'.format(action, article.pk))
logger.error('ezid_result: ' + ezid_result)
else:
logger.error('EZID DOI {} failed for article.pk: {}...'.format(action, article.pk))
if ezid_result != None:
logger.error(ezid_result.msg)
return True, False, ezid_result
def update_journal_doi(article):
if get_setting('ezid_plugin_username', article.journal):
ezid_config, ezid_metadata = get_journal_metadata(article)
ezid_metadata['update_id'] = article.get_doi()
ezid_result = update_doi_via_ezid(ezid_config, ezid_metadata, 'ezid/journal_content.xml')
return process_ezid_result(article, "update", ezid_result)
else:
return False, False, ""
def register_journal_doi(article):
if get_setting('ezid_plugin_username', article.journal) == 'on':
ezid_config, ezid_metadata = get_journal_metadata(article)
ezid_result = create_doi_via_ezid(ezid_config, ezid_metadata, 'ezid/journal_content.xml')
return process_ezid_result(article, "creation", ezid_result)
else:
return False, False, ""