-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
305 lines (266 loc) · 9.12 KB
/
utils.py
File metadata and controls
305 lines (266 loc) · 9.12 KB
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
import requests
from requests.auth import HTTPBasicAuth
from django.utils.html import strip_tags
from django.utils import timezone
from plugins.datacite import plugin_settings
from identifiers import models as ident_models
from utils import setting_handler
def prep_data(
article,
doi,
event=None,
):
series_information = article.journal.name
if article.issue:
series_information = "{}, {}({})".format(
article.journal.name,
article.issue.volume,
article.issue.issue,
)
if article.page_range:
series_information = "{}, {}".format(
series_information,
article.page_range,
)
elif article.page_range:
series_information = "{}, {}".format(
series_information,
article.page_range,
)
keywords = []
for keyword in article.keywords.all():
keywords.append(
{
'subject': keyword.word,
}
)
formats = []
if article.pdfs:
formats.append("application/pdf")
if article.xml_galleys:
formats.append("application/xml")
publicationYear = (
article.date_published.year
if article.date_published
else timezone.now().year
)
article_data = {
"data": {
"id": doi,
"type": "dois",
"attributes": {
"doi": doi,
"creators": [
{
'name': author.full_name(),
'nameType': 'Personal',
'givenName': author.first_name,
'familyName': author.last_name,
'affiliation': [
{
'name': author.affiliation(),
}
],
}
for author in article.frozen_authors()
],
"titles": [
{
"title": article.title,
}
],
"publisher": article.journal.publisher,
"publicationYear": publicationYear,
"types": {
"resourceTypeGeneral": "JournalArticle",
},
"descriptions": [
{
"descriptionType": "Abstract",
"description": strip_tags(article.abstract) if article.abstract else '',
},
{
"descriptionType": "SeriesInformation",
"description": series_information,
},
],
"subjects": keywords,
"formats": formats,
"url": article.url,
"schemaVersion": "http://datacite.org/schema/kernel-4.4",
"dates": [
{
"dateType": "Available",
"date": str(article.date_published.date())
if article.date_published
else '',
}
],
},
}
}
related_item = {
"relationType": "IsPublishedIn",
"titles": f"{article.journal.name}",
"publisher": f"{article.journal.publisher}",
"publicationYear": f"{article.date_published.year if article.date_published else ''}",
"relatedItemType": "Journal",
}
if article.issue:
related_item.update(
{
"issue": f"{article.issue.issue}",
"volume": f"{article.issue.volume}",
}
)
if article.first_page or article.last_page:
related_item.update(
{
"firstPage": f"{article.first_page or ''}",
"lastPage": f"{article.last_page or ''}",
}
)
article_data["data"]["attributes"]["relatedItems"] = [related_item]
if article.license:
article_data["data"]["attributes"]["rightsList"] = [
{
"rights": article.license.name,
"rightsUri": article.license.url,
}
]
if article.journal.issn:
article_data["data"]["attributes"]["relatedIdentifiers"] = [
{
"relatedIdentifier": article.journal.issn,
"relatedIdentifierType": "ISSN",
"relationType": "IsPublishedIn",
"resourceTypeGeneral": "Journal",
}
]
article_data["data"]["attributes"]["relatedItems"][0][
"relatedItemIdentifier"
] = {
"relatedItemIdentifier": f"{article.journal.issn}",
"relatedItemIdentifierType": "ISSN",
}
if event:
article_data["data"]["attributes"]["event"] = event
return article_data
def mint_datacite_doi(
article,
doi,
event=None,
):
headers = {"Content-Type": "application/vnd.api+json"}
data = prep_data(article, doi, event)
if event == 'publish' and article.get_doi():
url = '{}/{}'.format(
plugin_settings.DATACITE_API_URL,
article.get_doi(),
)
response = requests.put(
url=url,
json=data,
headers=headers,
auth=HTTPBasicAuth(
plugin_settings.DATACITE_USERNAME,
plugin_settings.DATACITE_PASSWORD,
),
)
# If the DOI doesn't exist for some reason (failed at accept) POST it
if response.status_code == 404:
response = requests.post(
url=plugin_settings.DATACITE_API_URL,
json=data,
headers=headers,
auth=HTTPBasicAuth(
plugin_settings.DATACITE_USERNAME,
plugin_settings.DATACITE_PASSWORD,
),
)
else:
response = requests.post(
url=plugin_settings.DATACITE_API_URL,
json=data,
headers=headers,
auth=HTTPBasicAuth(
plugin_settings.DATACITE_USERNAME,
plugin_settings.DATACITE_PASSWORD,
),
)
if response.status_code in [200, 201]:
return True, 'Okay'
else:
return False, response.content
def register_doi_automatically(**kwargs):
"""
Function called thru events framework.
"""
article = kwargs.get('article')
if auto_deposit_enabled(article.journal, article.section):
doi = "{prefix}/{journal_code}.{article_id}".format(
prefix=plugin_settings.DATACITE_PREFIX,
journal_code=article.journal.code if plugin_settings.JOURNAL_PREFIX else '',
article_id=article.pk
)
success, text = mint_datacite_doi(article, doi, event='register')
if success:
ident_models.Identifier.objects.get_or_create(
id_type='doi',
identifier=doi,
article=article,
)
def publish_doi_automatically(**kwargs):
article = kwargs.get('article')
if auto_deposit_enabled(article.journal, article.section):
doi = "{prefix}/{journal_code}.{article_id}".format(
prefix=plugin_settings.DATACITE_PREFIX,
journal_code=article.journal.code if plugin_settings.JOURNAL_PREFIX else '',
article_id=article.pk
)
success, text = mint_datacite_doi(article, doi, event='publish')
if success:
ident_models.Identifier.objects.get_or_create(
id_type='doi',
identifier=doi,
article=article,
)
def get_settings(journal):
settings = [
{
'name': 'enable_datacite_auto',
'object': setting_handler.get_setting(
'plugin:datacite',
'enable_datacite_auto',
journal
),
}
]
return settings
def auto_deposit_enabled(journal, section=None):
"""
Check if auto-deposit is enabled for a given journal and optionally for
a specific section.
:param journal: The journal for which auto-deposit setting is checked.
:param section: Optional. The specific section to check for auto-deposit.
Defaults to None.
:return: True if auto-deposit is enabled for the journal
(and section if provided), False otherwise.
"""
if journal:
# Check if the auto-deposit setting is enabled for the journal
is_enabled = setting_handler.get_setting(
setting_group_name='plugin:datacite',
setting_name='enable_datacite_auto',
journal=journal,
).processed_value
if not is_enabled:
return False
# Check if the journal has an associated SectionMint
if section and hasattr(journal, 'sectionmint'):
# If a section is provided, check if minting is enabled for the
# section
return section in journal.sectionmint.sections.all()
# If no SectionMint is found or no section is provided, return True
return True
return False