-
Notifications
You must be signed in to change notification settings - Fork 7
/
__init__.py
124 lines (99 loc) · 3.68 KB
/
__init__.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
"""
Module resposible for communication with datacite.org. Retrives DOI
identificator and posts dataset's description.
Code based on datacite documentation v2 https://mds.datacite.org/static/apidoc/
"""
import httplib2, sys, base64, codecs
from django.conf import settings
from django.template import Context, loader
class DataciteAPIException(Exception):
"""Handles all exceptions which comes from API
at once. Each API command results in status code and message
so the exception is initialized by those two values.
"""
def __init__(self, value, msg):
self.value = value
self.msg = msg
def __str__(self):
return ("%s: %s") % (repr(self.value),self.msg)
def datacite_command(req,method,body_unicode = ''):
"""Call datacite API command
@param req: requested command
@type req: string
@param method: http method ('GET' or 'POST')
@type location: string
@return: response type and message
@rtype: tuple of int and string
"""
h = httplib2.Http(disable_ssl_certificate_validation=True)
auth_string = base64.encodestring(settings.DATACITE_USERNAME + ':' + settings.DATACITE_PASSWORD)
response, content = h.request(settings.DATACITE_API_URL + req,
method,
body = body_unicode.encode('utf-8'),
headers={'Content-Type':'text/plain;charset=UTF-8',
'Authorization':'Basic ' + auth_string})
if (response.status > 201):
raise DataciteAPIException(response.status, content.decode('utf-8'))
return response.status, content.decode('utf-8')
def doi_post(doi, location):
"""Request new doi for given location
@param doi: requested doi
@type doi: string
@param location: URI of the dataset
@type location: string
@return: response type and message
@rtype: tuple of int and string
"""
body_unicode = u"doi=%s\nurl=http://%s%s\n" % (doi,settings.DATACITE_DOMAIN,location)
return datacite_command('/doi','POST',body_unicode)
def metadata_get(doi):
"""Get location for given doi
@param doi: DOI of the dataset to check
@type doi: string
@return: response type and message
@rtype: tuple of int and string
"""
return datacite_command('/doi/' + doi,'GET')
def metadata_post(metadata):
"""Update metadata for given location
@param metadata: metadata in XML format
@type metadata: string
@return: response type and message
@rtype: tuple of int and string
"""
return datacite_command('/metadata','POST',metadata)
def metadata_get(doi):
"""Get metadata for given doi
@param doi: DOI of the dataset to check
@type doi: string
@return: response type and message
@rtype: tuple of int and string
"""
return datacite_command('/metadata/' + doi,'GET')
def get_doi(data):
"""
Generate doi for a given dataset
"""
from datacite.models import DOI
doi, c = DOI.objects.get_or_create(slug=settings.DATACITE_FORMAT % {'slug': data.slug.__str__().upper(),
'version': data.version},
data=data)
return doi
def metadata_xml_string(data):
"""
Show the metadata of the dataset under given DOI.
**Required arguments**
``data``
data object
**Context:**
``data``
data object
**Template:**
datacite/metadata.xml - in current version xml is generated
by filling the template.
"""
context = Context()
data.doi = get_doi(data)
t = loader.get_template('datacite/metadata.xml')
c = Context({'data': data})
return t.render(c)