dservercore 0.20.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dservercore/__init__.py +335 -0
- dservercore/annotations_routes.py +49 -0
- dservercore/base_uri_routes.py +153 -0
- dservercore/blueprint.py +15 -0
- dservercore/cli.py +268 -0
- dservercore/config.py +98 -0
- dservercore/config_routes.py +47 -0
- dservercore/date_utils.py +22 -0
- dservercore/extensions.py +8 -0
- dservercore/manifest_routes.py +49 -0
- dservercore/me_routes.py +50 -0
- dservercore/readme_routes.py +48 -0
- dservercore/schemas.py +83 -0
- dservercore/sort.py +260 -0
- dservercore/sql_models.py +173 -0
- dservercore/tags_routes.py +49 -0
- dservercore/templates/index.html +9 -0
- dservercore/uri_routes.py +197 -0
- dservercore/user_routes.py +169 -0
- dservercore/utils.py +1050 -0
- dservercore/utils_auth.py +74 -0
- dservercore/uuid_routes.py +44 -0
- dservercore/version.py +16 -0
- dservercore-0.20.0.dist-info/LICENSE +21 -0
- dservercore-0.20.0.dist-info/METADATA +857 -0
- dservercore-0.20.0.dist-info/RECORD +29 -0
- dservercore-0.20.0.dist-info/WHEEL +5 -0
- dservercore-0.20.0.dist-info/entry_points.txt +5 -0
- dservercore-0.20.0.dist-info/top_level.txt +1 -0
dservercore/utils.py
ADDED
|
@@ -0,0 +1,1050 @@
|
|
|
1
|
+
"""Utility functions."""
|
|
2
|
+
|
|
3
|
+
from datetime import datetime, date
|
|
4
|
+
import importlib
|
|
5
|
+
import json
|
|
6
|
+
import logging
|
|
7
|
+
from pkg_resources import iter_entry_points
|
|
8
|
+
|
|
9
|
+
from flask import current_app
|
|
10
|
+
from flask_smorest.pagination import PaginationParameters
|
|
11
|
+
from sqlalchemy.sql import exists
|
|
12
|
+
|
|
13
|
+
import dtoolcore.utils
|
|
14
|
+
|
|
15
|
+
from dservercore import (
|
|
16
|
+
sql_db,
|
|
17
|
+
AuthenticationError,
|
|
18
|
+
AuthorizationError,
|
|
19
|
+
ValidationError,
|
|
20
|
+
UnknownBaseURIError,
|
|
21
|
+
UnknownURIError,
|
|
22
|
+
__version__
|
|
23
|
+
)
|
|
24
|
+
from dservercore.sql_models import (
|
|
25
|
+
User,
|
|
26
|
+
BaseURI,
|
|
27
|
+
Dataset,
|
|
28
|
+
)
|
|
29
|
+
from dservercore.sort import SortParameters, ASCENDING, DESCENDING
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
from dservercore.date_utils import (
|
|
33
|
+
extract_created_at_as_datetime,
|
|
34
|
+
extract_frozen_at_as_datetime,
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
DATASET_INFO_REQUIRED_KEYS = (
|
|
39
|
+
"uuid",
|
|
40
|
+
"base_uri",
|
|
41
|
+
"uri",
|
|
42
|
+
"name",
|
|
43
|
+
"type",
|
|
44
|
+
"readme",
|
|
45
|
+
"manifest",
|
|
46
|
+
"creator_username",
|
|
47
|
+
"frozen_at",
|
|
48
|
+
"annotations",
|
|
49
|
+
"tags",
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
DATASET_SORT_FIELDS = [
|
|
54
|
+
"base_uri",
|
|
55
|
+
"created_at",
|
|
56
|
+
"creator_username",
|
|
57
|
+
"frozen_at",
|
|
58
|
+
"name",
|
|
59
|
+
"uri",
|
|
60
|
+
"uuid"
|
|
61
|
+
]
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
# These entrypoints might point to plugin modules with
|
|
65
|
+
# config objects to be serialized as part of the global server config:
|
|
66
|
+
DSERVER_PLUGIN_ENTRYPOINTS = ['extension', 'retrieve', 'search']
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
logger = logging.getLogger(__name__)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
#############################################################################
|
|
73
|
+
# Private helper functions.
|
|
74
|
+
#############################################################################
|
|
75
|
+
|
|
76
|
+
def _serializable(obj):
|
|
77
|
+
"""Return string representation of object if object itself not json-serializable."""
|
|
78
|
+
try:
|
|
79
|
+
json.dumps(obj)
|
|
80
|
+
except TypeError:
|
|
81
|
+
return str(obj)
|
|
82
|
+
else:
|
|
83
|
+
return obj
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def _json_serial(obj):
|
|
87
|
+
if isinstance(obj, (datetime, date)):
|
|
88
|
+
return obj.isoformat()
|
|
89
|
+
raise TypeError("Type {} not serializable".format(type(obj)))
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def _get_user_obj(username):
|
|
93
|
+
return User.query.filter_by(username=username).first()
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def _get_base_uri_obj(base_uri):
|
|
97
|
+
return BaseURI.query.filter_by(base_uri=base_uri).first()
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def _get_dataset_obj(uri):
|
|
101
|
+
return Dataset.query.filter_by(uri=uri).first()
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
#############################################################################
|
|
105
|
+
# Public helper functions.
|
|
106
|
+
#############################################################################
|
|
107
|
+
|
|
108
|
+
def obj_to_dict(obj, exclusions=[]):
|
|
109
|
+
"""Convert all-upper-case entries in dict-like object to dict and exclude
|
|
110
|
+
certain keys. """
|
|
111
|
+
d = dict()
|
|
112
|
+
for k, v in obj.items():
|
|
113
|
+
# select only capitalized fields
|
|
114
|
+
if k.upper() == k and k not in exclusions:
|
|
115
|
+
d[k] = _serializable(v)
|
|
116
|
+
elif k.upper() == k and k in exclusions:
|
|
117
|
+
d[k] = "***" # obfuscate secret
|
|
118
|
+
return d
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def obj_to_lowercase_key_dict(obj, exclusions=[]):
|
|
122
|
+
"""Convert all-upper-case keys in dict-like object to all-lower-case keys
|
|
123
|
+
dict and exclude certain keys. """
|
|
124
|
+
d = obj_to_dict(obj, exclusions=exclusions)
|
|
125
|
+
return {k.lower(): v for k, v in d.items()}
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def versions_to_dict():
|
|
129
|
+
"""Dumps installed components and their versions to dictionary, i.e.
|
|
130
|
+
|
|
131
|
+
{
|
|
132
|
+
'dservercore': '0.17.2',
|
|
133
|
+
'dserver_retrieve_plugin_mongo': '0.1.0',
|
|
134
|
+
'dserver_search_plugin_mongo': '0.1.0'
|
|
135
|
+
}
|
|
136
|
+
"""
|
|
137
|
+
|
|
138
|
+
versions_dict = {'dservercore': __version__}
|
|
139
|
+
for ep_group in DSERVER_PLUGIN_ENTRYPOINTS:
|
|
140
|
+
for ep in iter_entry_points("dservercore.{}".format(ep_group)):
|
|
141
|
+
module_name = ep.module_name.split(".")[0]
|
|
142
|
+
|
|
143
|
+
# import module
|
|
144
|
+
try:
|
|
145
|
+
plugin_module = importlib.import_module(module_name)
|
|
146
|
+
except ImportError as exc:
|
|
147
|
+
# plugin import failed, this should not happen
|
|
148
|
+
continue
|
|
149
|
+
|
|
150
|
+
versions_dict[module_name] = getattr(plugin_module, '__version__', None)
|
|
151
|
+
|
|
152
|
+
return versions_dict
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
#############################################################################
|
|
156
|
+
# Generally useful dtool helper functions.
|
|
157
|
+
#############################################################################
|
|
158
|
+
|
|
159
|
+
def generate_dataset_info(dataset, base_uri):
|
|
160
|
+
"""Return dictionary with dataset info."""
|
|
161
|
+
dataset_info = dataset._admin_metadata
|
|
162
|
+
dataset_info["uri"] = dataset.uri
|
|
163
|
+
dataset_info["base_uri"] = base_uri
|
|
164
|
+
|
|
165
|
+
# Add the readme info.
|
|
166
|
+
dataset_info["readme"] = dataset.get_readme_content()
|
|
167
|
+
|
|
168
|
+
# Add the manifest.
|
|
169
|
+
dataset_info["manifest"] = dataset._manifest
|
|
170
|
+
|
|
171
|
+
# Add the annotations.
|
|
172
|
+
annotations = {}
|
|
173
|
+
for annotation_name in dataset.list_annotation_names():
|
|
174
|
+
annotations[annotation_name] = dataset.get_annotation(annotation_name)
|
|
175
|
+
dataset_info["annotations"] = annotations
|
|
176
|
+
|
|
177
|
+
# Add the tags.
|
|
178
|
+
tags = dataset.list_tags()
|
|
179
|
+
dataset_info["tags"] = tags
|
|
180
|
+
|
|
181
|
+
# Clean up datetime.data.
|
|
182
|
+
dataset_info_json_str = json.dumps(dataset_info, default=_json_serial)
|
|
183
|
+
dataset_info = json.loads(dataset_info_json_str)
|
|
184
|
+
|
|
185
|
+
# Set total number if items
|
|
186
|
+
dataset_info["number_of_items"] = len(dataset.identifiers)
|
|
187
|
+
|
|
188
|
+
# Compute size of dataset
|
|
189
|
+
dataset_info["size_in_bytes"] = \
|
|
190
|
+
sum([dataset.item_properties(i)["size_in_bytes"]
|
|
191
|
+
for i in dataset.identifiers])
|
|
192
|
+
|
|
193
|
+
return dataset_info
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
#############################################################################
|
|
197
|
+
# User helper functions
|
|
198
|
+
#############################################################################
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def user_exists(username):
|
|
202
|
+
"""Check whether user is registered in the system."""
|
|
203
|
+
if _get_user_obj(username) is None:
|
|
204
|
+
return False
|
|
205
|
+
return True
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def get_user_obj(username):
|
|
209
|
+
"""Retrieve User object from username."""
|
|
210
|
+
user = _get_user_obj(username)
|
|
211
|
+
if user is None:
|
|
212
|
+
raise (AuthenticationError())
|
|
213
|
+
return user
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
def register_user(username, data):
|
|
217
|
+
"""Register or update a single user in the system by replacing entry. Idempotent.
|
|
218
|
+
|
|
219
|
+
Example input structure::
|
|
220
|
+
|
|
221
|
+
{"is_admin": True},
|
|
222
|
+
|
|
223
|
+
If a user is missing in the system it is skipped. The ``is_admin`` status
|
|
224
|
+
defaults to False.
|
|
225
|
+
"""
|
|
226
|
+
is_admin = data.get("is_admin", False)
|
|
227
|
+
|
|
228
|
+
# User already exists, update
|
|
229
|
+
if sql_db.session.query(exists().where(User.username == username)).scalar():
|
|
230
|
+
for sqlalch_user_obj in (
|
|
231
|
+
sql_db.session.query(User).filter_by(username=username).all()
|
|
232
|
+
):
|
|
233
|
+
sqlalch_user_obj.is_admin = is_admin
|
|
234
|
+
else: # user does not exist yet, create
|
|
235
|
+
user = User(username=username, is_admin=is_admin)
|
|
236
|
+
sql_db.session.add(user)
|
|
237
|
+
|
|
238
|
+
sql_db.session.commit()
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
def register_users(users):
|
|
242
|
+
"""Register a list of users in the system.
|
|
243
|
+
|
|
244
|
+
Example input structure::
|
|
245
|
+
|
|
246
|
+
[
|
|
247
|
+
{"username": "magic.mirror", "is_admin": True},
|
|
248
|
+
{"username": "snow.white", "is_admin": False},
|
|
249
|
+
{"username": "dopey"},
|
|
250
|
+
{"username": "sleepy"},
|
|
251
|
+
]
|
|
252
|
+
|
|
253
|
+
If a user is already registered in the system it is skipped. To change the
|
|
254
|
+
``is_admin`` status of an existing user use the :func:`.update_users``
|
|
255
|
+
function. The ``is_admin`` status defaults to False.
|
|
256
|
+
"""
|
|
257
|
+
|
|
258
|
+
for user in users:
|
|
259
|
+
username = user["username"]
|
|
260
|
+
is_admin = user.get("is_admin", False)
|
|
261
|
+
|
|
262
|
+
# Skip existing users.
|
|
263
|
+
if sql_db.session.query(exists().where(User.username == username)).scalar(): # NOQA
|
|
264
|
+
continue
|
|
265
|
+
|
|
266
|
+
user = User(username=username, is_admin=is_admin)
|
|
267
|
+
sql_db.session.add(user)
|
|
268
|
+
|
|
269
|
+
sql_db.session.commit()
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
def list_users():
|
|
273
|
+
"""Return list of users."""
|
|
274
|
+
users = []
|
|
275
|
+
for u in User.query.all():
|
|
276
|
+
users.append(u.as_dict())
|
|
277
|
+
return users
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
def delete_users(users):
|
|
281
|
+
"""Delete a list of users in the system.
|
|
282
|
+
|
|
283
|
+
Example input structure::
|
|
284
|
+
|
|
285
|
+
[
|
|
286
|
+
{"username": "magic.mirror", "is_admin": True},
|
|
287
|
+
{"username": "snow.white", "is_admin": False},
|
|
288
|
+
{"username": "dopey"},
|
|
289
|
+
{"username": "sleepy"},
|
|
290
|
+
]
|
|
291
|
+
|
|
292
|
+
If a user is missing in the system it is skipped. Only the "username" key
|
|
293
|
+
is used to identify a user, any other keys, such as "is_admin", are
|
|
294
|
+
ignored. The list of dictionary input argument is used to be consistent
|
|
295
|
+
with the :func:`.list_users`` and the :func:`register_users` functions.
|
|
296
|
+
"""
|
|
297
|
+
for user in users:
|
|
298
|
+
username = user["username"]
|
|
299
|
+
|
|
300
|
+
for sqlalch_user_obj in (
|
|
301
|
+
sql_db.session.query(User).filter_by(username=username).all()
|
|
302
|
+
): # NOQA
|
|
303
|
+
sql_db.session.delete(sqlalch_user_obj)
|
|
304
|
+
|
|
305
|
+
sql_db.session.commit()
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
def delete_user(username):
|
|
309
|
+
"""Delete a single user from the system."""
|
|
310
|
+
for sqlalch_user_obj in (
|
|
311
|
+
sql_db.session.query(User).filter_by(username=username).all()
|
|
312
|
+
): # NOQA
|
|
313
|
+
sql_db.session.delete(sqlalch_user_obj)
|
|
314
|
+
|
|
315
|
+
sql_db.session.commit()
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
def update_users(users):
|
|
319
|
+
"""Update a list of users in the system.
|
|
320
|
+
|
|
321
|
+
Example input structure::
|
|
322
|
+
|
|
323
|
+
[
|
|
324
|
+
{"username": "magic.mirror", "is_admin": True},
|
|
325
|
+
{"username": "snow.white", "is_admin": False},
|
|
326
|
+
{"username": "dopey"},
|
|
327
|
+
{"username": "sleepy"},
|
|
328
|
+
]
|
|
329
|
+
|
|
330
|
+
If a user is missing in the system it is skipped. The ``is_admin`` status
|
|
331
|
+
defaults to False.
|
|
332
|
+
"""
|
|
333
|
+
for user in users:
|
|
334
|
+
username = user["username"]
|
|
335
|
+
is_admin = user.get("is_admin", False)
|
|
336
|
+
|
|
337
|
+
for sqlalch_user_obj in (
|
|
338
|
+
sql_db.session.query(User).filter_by(username=username).all()
|
|
339
|
+
): # NOQA
|
|
340
|
+
sqlalch_user_obj.is_admin = is_admin
|
|
341
|
+
|
|
342
|
+
sql_db.session.commit()
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
def get_user_info(username):
|
|
346
|
+
"""Return information about a user as a dictionary.
|
|
347
|
+
|
|
348
|
+
Return None if the user does not exist.
|
|
349
|
+
"""
|
|
350
|
+
user = User.query.filter_by(username=username).first()
|
|
351
|
+
|
|
352
|
+
if user is None:
|
|
353
|
+
return None
|
|
354
|
+
|
|
355
|
+
return user.as_dict()
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
#############################################################################
|
|
359
|
+
# Dataset list/search/lookup helper functions.
|
|
360
|
+
#############################################################################
|
|
361
|
+
|
|
362
|
+
def _dataset_order_by_args(sort_parameters):
|
|
363
|
+
"""Convert SortParameters to SQLAlchemy sort argument for Dataset model."""
|
|
364
|
+
|
|
365
|
+
order_by_args = []
|
|
366
|
+
for field, order in sort_parameters.order.items():
|
|
367
|
+
if not hasattr(Dataset, field):
|
|
368
|
+
continue
|
|
369
|
+
# special treatment for base_uri:
|
|
370
|
+
# we want to sort by the string field BaseURI.base_uri, not by
|
|
371
|
+
# the relationship field Dataset.base_uri
|
|
372
|
+
if field in ['base_uri']:
|
|
373
|
+
model = BaseURI
|
|
374
|
+
else:
|
|
375
|
+
model = Dataset
|
|
376
|
+
if order == DESCENDING:
|
|
377
|
+
order_by_args.append(getattr(model, field).desc())
|
|
378
|
+
else: # ascending
|
|
379
|
+
order_by_args.append(getattr(model, field))
|
|
380
|
+
return order_by_args
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
def list_datasets_by_user(username,
|
|
384
|
+
pagination_parameters: PaginationParameters = None,
|
|
385
|
+
sort_parameters: SortParameters = None):
|
|
386
|
+
"""List the datasets the user has access to.
|
|
387
|
+
|
|
388
|
+
:param pagination_parameters: flask_smorest.pagination.PaginationParameters object, optional
|
|
389
|
+
:param sort_parameters: dservercore.sort.SortParameters object, optional
|
|
390
|
+
|
|
391
|
+
Returns list of dicts if user is valid and has access to datasets.
|
|
392
|
+
Returns empty list if user is valid but has not got access to any datasets.
|
|
393
|
+
Raises AuthenticationError if user is invalid.
|
|
394
|
+
"""
|
|
395
|
+
user = get_user_obj(username) # raises AuthenticationError
|
|
396
|
+
|
|
397
|
+
query = (
|
|
398
|
+
sql_db.session.query(Dataset, User)
|
|
399
|
+
.join(User.search_base_uris)
|
|
400
|
+
.filter(User.username == username)
|
|
401
|
+
.filter(BaseURI.id == Dataset.base_uri_id)
|
|
402
|
+
)
|
|
403
|
+
|
|
404
|
+
if sort_parameters is not None:
|
|
405
|
+
order_by_args = _dataset_order_by_args(sort_parameters)
|
|
406
|
+
query = query.order_by(*order_by_args)
|
|
407
|
+
|
|
408
|
+
if pagination_parameters is not None:
|
|
409
|
+
pagination_parameters.item_count = query.count()
|
|
410
|
+
queried_items = query.paginate(
|
|
411
|
+
page=pagination_parameters.page,
|
|
412
|
+
per_page=pagination_parameters.page_size,
|
|
413
|
+
error_out=True).items
|
|
414
|
+
else:
|
|
415
|
+
queried_items = query.all()
|
|
416
|
+
|
|
417
|
+
datasets = [ds for ds, user in queried_items]
|
|
418
|
+
|
|
419
|
+
return datasets
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
def summary_of_datasets_by_user(username):
|
|
423
|
+
"""Return summary information of datasets the user has access to.
|
|
424
|
+
|
|
425
|
+
Return dictionary of summary information.
|
|
426
|
+
Raises AuthenticationError if user is invalid.
|
|
427
|
+
"""
|
|
428
|
+
user = get_user_obj(username) # raises AuthenticationError
|
|
429
|
+
|
|
430
|
+
# Get all the datasets the user has access to.
|
|
431
|
+
datasets = search_datasets_by_user(username, query={})
|
|
432
|
+
|
|
433
|
+
datasets_per_creator = {}
|
|
434
|
+
datasets_per_base_uri = {}
|
|
435
|
+
datasets_per_tag = {}
|
|
436
|
+
|
|
437
|
+
for ds in datasets:
|
|
438
|
+
user = ds["creator_username"]
|
|
439
|
+
uri = ds["base_uri"]
|
|
440
|
+
datasets_per_creator[user] = datasets_per_creator.get(user, 0) + 1
|
|
441
|
+
datasets_per_base_uri[uri] = datasets_per_base_uri.get(uri, 0) + 1
|
|
442
|
+
|
|
443
|
+
# All datasets should have the "tags" key. However, it could be the
|
|
444
|
+
# case that a dataset in the database prior to version 0.14.0 fails
|
|
445
|
+
# to be re-indexed. In this case it would be left without having tags
|
|
446
|
+
# added to it. In the below we therefore check to make sure that it is
|
|
447
|
+
# present before we try to use it.
|
|
448
|
+
if "tags" in ds:
|
|
449
|
+
for tag in ds["tags"]:
|
|
450
|
+
datasets_per_tag[tag] = datasets_per_tag.get(tag, 0) + 1
|
|
451
|
+
|
|
452
|
+
summary = {
|
|
453
|
+
"number_of_datasets": len(datasets),
|
|
454
|
+
"creator_usernames": sorted(datasets_per_creator.keys()),
|
|
455
|
+
"base_uris": sorted(datasets_per_base_uri.keys()),
|
|
456
|
+
"datasets_per_creator": datasets_per_creator,
|
|
457
|
+
"datasets_per_base_uri": datasets_per_base_uri,
|
|
458
|
+
"tags": sorted(datasets_per_tag.keys()),
|
|
459
|
+
"datasets_per_tag": datasets_per_tag,
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
return summary
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
def lookup_datasets_by_user_and_uuid(username, uuid,
|
|
466
|
+
pagination_parameters: PaginationParameters = None,
|
|
467
|
+
sort_parameters: SortParameters = None):
|
|
468
|
+
"""Return list of dataset with matching uuid.
|
|
469
|
+
|
|
470
|
+
Returns list of dicts if user is valid and has access to datasets.
|
|
471
|
+
Returns empty list if user is valid but has not got access to any datasets.
|
|
472
|
+
Raises AuthenticationError if user is invalid.
|
|
473
|
+
"""
|
|
474
|
+
user = get_user_obj(username) # raises AuthenticationError
|
|
475
|
+
|
|
476
|
+
query = (
|
|
477
|
+
sql_db.session.query(Dataset, User)
|
|
478
|
+
.join(User.search_base_uris)
|
|
479
|
+
.filter(Dataset.uuid == uuid)
|
|
480
|
+
.filter(User.username == username)
|
|
481
|
+
.filter(BaseURI.id == Dataset.base_uri_id)
|
|
482
|
+
)
|
|
483
|
+
|
|
484
|
+
if sort_parameters is not None:
|
|
485
|
+
order_by_args = _dataset_order_by_args(sort_parameters)
|
|
486
|
+
query = query.order_by(*order_by_args)
|
|
487
|
+
|
|
488
|
+
if pagination_parameters is not None:
|
|
489
|
+
pagination_parameters.item_count = query.count()
|
|
490
|
+
queried_items = query.paginate(
|
|
491
|
+
page=pagination_parameters.page,
|
|
492
|
+
per_page=pagination_parameters.page_size,
|
|
493
|
+
error_out=True).items
|
|
494
|
+
else:
|
|
495
|
+
queried_items = query.all()
|
|
496
|
+
|
|
497
|
+
datasets = [ds for ds, user in queried_items]
|
|
498
|
+
|
|
499
|
+
return datasets
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
def get_dataset_by_user_and_uri(username, uri):
|
|
503
|
+
"""Return single dataset with matching uri if user has rights to see it.
|
|
504
|
+
|
|
505
|
+
Returns single Dataset if user is valid and has access to datasets.
|
|
506
|
+
Returns None if user is valid but has not got access to the dataset.
|
|
507
|
+
Raises AuthenticationError if user is invalid.
|
|
508
|
+
"""
|
|
509
|
+
|
|
510
|
+
# datasets = []
|
|
511
|
+
query = (
|
|
512
|
+
sql_db.session.query(Dataset, User)
|
|
513
|
+
.join(User.search_base_uris)
|
|
514
|
+
.filter(Dataset.uri == uri)
|
|
515
|
+
.filter(User.username == username)
|
|
516
|
+
.filter(BaseURI.id == Dataset.base_uri_id)
|
|
517
|
+
.all()
|
|
518
|
+
)
|
|
519
|
+
|
|
520
|
+
datasets = [ds for ds, user in query]
|
|
521
|
+
|
|
522
|
+
if len(datasets) > 0:
|
|
523
|
+
ret = datasets[0]
|
|
524
|
+
else:
|
|
525
|
+
ret = None
|
|
526
|
+
|
|
527
|
+
return ret
|
|
528
|
+
|
|
529
|
+
|
|
530
|
+
#############################################################################
|
|
531
|
+
# Search plugin interface
|
|
532
|
+
#############################################################################
|
|
533
|
+
|
|
534
|
+
def _preprocess_privileges(username, query):
|
|
535
|
+
"""Preprocess a query dict according to per-user privileges."""
|
|
536
|
+
user = get_user_obj(username)
|
|
537
|
+
|
|
538
|
+
# Deal with base URIs. If not specified on the query add the ones that the
|
|
539
|
+
# user has search privileges on. If specified filter out any that the user
|
|
540
|
+
# does not have search privileges on.
|
|
541
|
+
allowed_uris = [bu.base_uri for bu in user.search_base_uris]
|
|
542
|
+
if "base_uris" not in query:
|
|
543
|
+
query["base_uris"] = allowed_uris
|
|
544
|
+
else:
|
|
545
|
+
selected_uris = [str(bu) for bu in query["base_uris"] if bu in allowed_uris] # NOQA
|
|
546
|
+
query["base_uris"] = selected_uris
|
|
547
|
+
|
|
548
|
+
return query
|
|
549
|
+
|
|
550
|
+
|
|
551
|
+
def preprocess_query_base_uris(username, query):
|
|
552
|
+
"""Return query with appropriate base URIs.
|
|
553
|
+
|
|
554
|
+
If no base URIs are in the query add all the allowed ones.
|
|
555
|
+
If base URIs are provided only include the ones allowed.
|
|
556
|
+
"""
|
|
557
|
+
return _preprocess_privileges(username, query)
|
|
558
|
+
|
|
559
|
+
|
|
560
|
+
def search_datasets_by_user(username, query,
|
|
561
|
+
pagination_parameters: PaginationParameters = None,
|
|
562
|
+
sort_parameters: SortParameters = None):
|
|
563
|
+
"""Search the datasets the user has access to.
|
|
564
|
+
|
|
565
|
+
Valid keys for the query are: creator_usernames, base_uris, free_text. If
|
|
566
|
+
the query dictionary is empty all datasets, that a user has access to, are
|
|
567
|
+
returned.
|
|
568
|
+
|
|
569
|
+
:param username: username
|
|
570
|
+
:param query: dictionary specifying query
|
|
571
|
+
:param pagination_parameters: flask_smorest.pagination.PaginationParameters object, optional
|
|
572
|
+
:param sort_parameters: dservercore.sort.SortParameters object, optional
|
|
573
|
+
:returns: List of dicts if user is valid and has access to datasets.
|
|
574
|
+
Empty list if user is valid but has not got access to any
|
|
575
|
+
datasets.
|
|
576
|
+
:raises: AuthenticationError if user is invalid.
|
|
577
|
+
"""
|
|
578
|
+
|
|
579
|
+
query = preprocess_query_base_uris(username, query)
|
|
580
|
+
# If there are no base URIs at this point it means that the user is not
|
|
581
|
+
# allowed to search for anything.
|
|
582
|
+
if len(query["base_uris"]) == 0:
|
|
583
|
+
return []
|
|
584
|
+
|
|
585
|
+
return current_app.search.search(query,
|
|
586
|
+
pagination_parameters=pagination_parameters,
|
|
587
|
+
sort_parameters=sort_parameters)
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
#############################################################################
|
|
591
|
+
# Base URI helper functions
|
|
592
|
+
#############################################################################
|
|
593
|
+
|
|
594
|
+
|
|
595
|
+
def url_suffix_to_uri(url_suffix):
|
|
596
|
+
"""Translates a URL suffix like
|
|
597
|
+
|
|
598
|
+
s3/bucket
|
|
599
|
+
|
|
600
|
+
to a valid URI, e.g.
|
|
601
|
+
|
|
602
|
+
s3://bucket
|
|
603
|
+
"""
|
|
604
|
+
|
|
605
|
+
# first, make sure url_suffix isn't already a valid URI
|
|
606
|
+
uri = url_suffix
|
|
607
|
+
index = uri.find('://')
|
|
608
|
+
if index == -1: # not found
|
|
609
|
+
uri = uri.replace('/', '://', 1)
|
|
610
|
+
|
|
611
|
+
uri = dtoolcore.utils.sanitise_uri(uri)
|
|
612
|
+
return uri
|
|
613
|
+
|
|
614
|
+
|
|
615
|
+
def uri_to_url_suffix(uri):
|
|
616
|
+
"""Translates a URI like
|
|
617
|
+
|
|
618
|
+
s3://bucket
|
|
619
|
+
|
|
620
|
+
to a URL suffix like
|
|
621
|
+
|
|
622
|
+
s3/bucket
|
|
623
|
+
"""
|
|
624
|
+
url_suffix = dtoolcore.utils.sanitise_uri(uri)
|
|
625
|
+
|
|
626
|
+
# first, make sure uri has the :// separator, then replace with simple /
|
|
627
|
+
index = url_suffix.find('://')
|
|
628
|
+
if index != -1: # not found
|
|
629
|
+
url_suffix = url_suffix.replace('://', '/', 1)
|
|
630
|
+
|
|
631
|
+
return url_suffix
|
|
632
|
+
|
|
633
|
+
|
|
634
|
+
def base_uri_exists(base_uri):
|
|
635
|
+
"""Return True if the base URI has been registered."""
|
|
636
|
+
if _get_base_uri_obj(base_uri) is None:
|
|
637
|
+
return False
|
|
638
|
+
return True
|
|
639
|
+
|
|
640
|
+
|
|
641
|
+
def get_base_uri_obj(base_uri):
|
|
642
|
+
"""Return SQLAlchemy BaseURI object."""
|
|
643
|
+
base_uri_obj = _get_base_uri_obj(base_uri)
|
|
644
|
+
if base_uri_obj is None:
|
|
645
|
+
raise (UnknownBaseURIError("Base URI {} not registered".format(base_uri)))
|
|
646
|
+
return base_uri_obj
|
|
647
|
+
|
|
648
|
+
|
|
649
|
+
def register_base_uri(base_uri):
|
|
650
|
+
"""Register a base URI in dserver."""
|
|
651
|
+
base_uri = dtoolcore.utils.sanitise_uri(base_uri)
|
|
652
|
+
base_uri = BaseURI(base_uri=base_uri)
|
|
653
|
+
|
|
654
|
+
sql_db.session.add(base_uri)
|
|
655
|
+
sql_db.session.commit()
|
|
656
|
+
|
|
657
|
+
|
|
658
|
+
def delete_base_uri(base_uri):
|
|
659
|
+
"""Delete a base URI from dserver."""
|
|
660
|
+
base_uri = dtoolcore.utils.sanitise_uri(base_uri)
|
|
661
|
+
|
|
662
|
+
for sqlalch_base_uri_obj in (
|
|
663
|
+
sql_db.session.query(BaseURI).filter_by(base_uri=base_uri).all()
|
|
664
|
+
): # NOQA
|
|
665
|
+
sql_db.session.delete(sqlalch_base_uri_obj)
|
|
666
|
+
|
|
667
|
+
sql_db.session.commit()
|
|
668
|
+
|
|
669
|
+
|
|
670
|
+
def list_base_uris():
|
|
671
|
+
"""List the base URIs in dserver."""
|
|
672
|
+
base_uris = []
|
|
673
|
+
for bu in BaseURI.query.all():
|
|
674
|
+
base_uris.append(bu.as_dict())
|
|
675
|
+
return base_uris
|
|
676
|
+
|
|
677
|
+
|
|
678
|
+
#############################################################################
|
|
679
|
+
# Permission helper functions
|
|
680
|
+
#############################################################################
|
|
681
|
+
|
|
682
|
+
|
|
683
|
+
def get_permission_info(base_uri_str):
|
|
684
|
+
"""Return the permissions on a base URI as a dictionary."""
|
|
685
|
+
base_uri = get_base_uri_obj(base_uri_str)
|
|
686
|
+
return base_uri.as_dict()
|
|
687
|
+
|
|
688
|
+
|
|
689
|
+
def register_permissions(base_uri, permissions):
|
|
690
|
+
"""Register or update permissions on base_uri. Idempotent."""
|
|
691
|
+
base_uri = get_base_uri_obj(base_uri)
|
|
692
|
+
|
|
693
|
+
# Clear all the existing permissions.
|
|
694
|
+
base_uri.search_users = []
|
|
695
|
+
base_uri.register_users = []
|
|
696
|
+
|
|
697
|
+
if "users_with_search_permissions" in permissions:
|
|
698
|
+
for username in permissions["users_with_search_permissions"]:
|
|
699
|
+
if user_exists(username):
|
|
700
|
+
user = get_user_obj(username)
|
|
701
|
+
base_uri.search_users.append(user)
|
|
702
|
+
|
|
703
|
+
if "users_with_register_permissions" in permissions:
|
|
704
|
+
for username in permissions["users_with_register_permissions"]:
|
|
705
|
+
if user_exists(username):
|
|
706
|
+
user = get_user_obj(username)
|
|
707
|
+
base_uri.register_users.append(user)
|
|
708
|
+
|
|
709
|
+
sql_db.session.commit()
|
|
710
|
+
|
|
711
|
+
|
|
712
|
+
#############################################################################
|
|
713
|
+
# Register dataset helper functions
|
|
714
|
+
#############################################################################
|
|
715
|
+
|
|
716
|
+
|
|
717
|
+
def dataset_uri_exists(uri):
|
|
718
|
+
"""Return True if the dataset URI has been registered."""
|
|
719
|
+
if _get_dataset_obj(uri) is None:
|
|
720
|
+
return False
|
|
721
|
+
return True
|
|
722
|
+
|
|
723
|
+
|
|
724
|
+
def get_dataset_obj(uri):
|
|
725
|
+
"""Return SQLAlchemy Dataset object."""
|
|
726
|
+
dataset_obj = _get_dataset_obj(uri)
|
|
727
|
+
if dataset_obj is None:
|
|
728
|
+
raise (UnknownURIError("URI {} not registered".format(uri)))
|
|
729
|
+
return dataset_obj
|
|
730
|
+
|
|
731
|
+
|
|
732
|
+
def dataset_info_is_valid(dataset_info):
|
|
733
|
+
"""Return True if the dataset info is valid."""
|
|
734
|
+
|
|
735
|
+
# Ensure that all the required keys are present.
|
|
736
|
+
for key in DATASET_INFO_REQUIRED_KEYS:
|
|
737
|
+
if key not in dataset_info:
|
|
738
|
+
return False
|
|
739
|
+
|
|
740
|
+
# Ensure that it is a "dataset" and not a "protodataset".
|
|
741
|
+
if dataset_info["type"] != "dataset":
|
|
742
|
+
return False
|
|
743
|
+
|
|
744
|
+
# Ensure that the UUID has the correct number of characters.
|
|
745
|
+
if len(str(dataset_info["uuid"])) != 36:
|
|
746
|
+
return False
|
|
747
|
+
|
|
748
|
+
# Ensure that the base URI has had any trailing slash removed.
|
|
749
|
+
if dataset_info["base_uri"].endswith("/"):
|
|
750
|
+
return False
|
|
751
|
+
|
|
752
|
+
return True
|
|
753
|
+
|
|
754
|
+
|
|
755
|
+
def create_dataset_obj_from_admin_metadata(admin_metadata):
|
|
756
|
+
"""Create an object adhering to the Dataset model from a dict-like object admin_metadata."""
|
|
757
|
+
|
|
758
|
+
base_uri = get_base_uri_obj(admin_metadata["base_uri"])
|
|
759
|
+
|
|
760
|
+
frozen_at = extract_frozen_at_as_datetime(admin_metadata)
|
|
761
|
+
created_at = extract_created_at_as_datetime(admin_metadata)
|
|
762
|
+
|
|
763
|
+
try:
|
|
764
|
+
number_of_items = admin_metadata["number_of_items"]
|
|
765
|
+
except KeyError:
|
|
766
|
+
number_of_items = None
|
|
767
|
+
|
|
768
|
+
try:
|
|
769
|
+
size_in_bytes = admin_metadata["size_in_bytes"]
|
|
770
|
+
except KeyError:
|
|
771
|
+
size_in_bytes = None
|
|
772
|
+
|
|
773
|
+
dataset = Dataset(
|
|
774
|
+
uri=admin_metadata["uri"],
|
|
775
|
+
base_uri_id=base_uri.id,
|
|
776
|
+
uuid=str(admin_metadata["uuid"]),
|
|
777
|
+
name=admin_metadata["name"],
|
|
778
|
+
creator_username=admin_metadata["creator_username"],
|
|
779
|
+
frozen_at=frozen_at,
|
|
780
|
+
created_at=created_at,
|
|
781
|
+
number_of_items=number_of_items,
|
|
782
|
+
size_in_bytes=size_in_bytes,
|
|
783
|
+
)
|
|
784
|
+
return dataset
|
|
785
|
+
|
|
786
|
+
|
|
787
|
+
def register_dataset_admin_metadata(admin_metadata):
|
|
788
|
+
"""Update the admin metadata in the dataset SQL table by replacing a possibly existing dataset entry."""
|
|
789
|
+
|
|
790
|
+
# first, validate dataset info
|
|
791
|
+
new_dataset_entry = create_dataset_obj_from_admin_metadata(admin_metadata)
|
|
792
|
+
|
|
793
|
+
uri = admin_metadata["uri"]
|
|
794
|
+
|
|
795
|
+
# there must only be one object, as URI is the unique index
|
|
796
|
+
sql_db.session.query(Dataset).filter_by(uri=uri).delete()
|
|
797
|
+
|
|
798
|
+
sql_db.session.add(new_dataset_entry)
|
|
799
|
+
sql_db.session.commit()
|
|
800
|
+
|
|
801
|
+
return new_dataset_entry.uri
|
|
802
|
+
|
|
803
|
+
|
|
804
|
+
def delete_dataset_admin_metadata(uri):
|
|
805
|
+
"""Delete the admin metadata from the dataset SQL table.
|
|
806
|
+
|
|
807
|
+
:param uri: URI of dataset entry to delete.
|
|
808
|
+
:returns: URI of successfully deleted dataset entry"""
|
|
809
|
+
|
|
810
|
+
# there must only be one object, as URI is the unique index
|
|
811
|
+
for old_dataset_entry in (
|
|
812
|
+
sql_db.session.query(Dataset).filter_by(uri=uri).all()
|
|
813
|
+
):
|
|
814
|
+
sql_db.session.delete(old_dataset_entry)
|
|
815
|
+
|
|
816
|
+
sql_db.session.commit()
|
|
817
|
+
|
|
818
|
+
return uri
|
|
819
|
+
|
|
820
|
+
|
|
821
|
+
def register_dataset(dataset_info):
|
|
822
|
+
"""Put-update a dataset in the lookup server. Put is idempotent."""
|
|
823
|
+
|
|
824
|
+
if not dataset_info_is_valid(dataset_info):
|
|
825
|
+
raise (ValidationError("Dataset info not valid: {}".format(dataset_info))) # NOQA
|
|
826
|
+
|
|
827
|
+
base_uri = dataset_info["base_uri"]
|
|
828
|
+
if not base_uri_exists(base_uri):
|
|
829
|
+
raise (ValidationError("Base URI is not registered: {}".format(base_uri))) # NOQA
|
|
830
|
+
|
|
831
|
+
# Take a copy as register_dataset_descriptive_metadata makes
|
|
832
|
+
# changes to the dictionary, in particular it changes the
|
|
833
|
+
# types of the dates to datetime objects.
|
|
834
|
+
# On the core search and retrieve plugins, we are strict. We expect them
|
|
835
|
+
# to raise ValidationError on failure, we log those, and reraise
|
|
836
|
+
try:
|
|
837
|
+
if hasattr(current_app.search, "register_dataset"):
|
|
838
|
+
current_app.search.register_dataset(dataset_info.copy())
|
|
839
|
+
else:
|
|
840
|
+
logger.warning("Search plugin has no method 'register_dataset'")
|
|
841
|
+
logger.warning("Applied modifications to dataset entry '%s' will not take effect in search database.'",
|
|
842
|
+
dataset_info["uri"])
|
|
843
|
+
except ValidationError as message:
|
|
844
|
+
# Instead of reporting the error with the logging module, we will want
|
|
845
|
+
# to do some bookkeeping on registration errors per URI in the
|
|
846
|
+
# core SQL db
|
|
847
|
+
logger.error(message)
|
|
848
|
+
raise
|
|
849
|
+
|
|
850
|
+
try:
|
|
851
|
+
if hasattr(current_app.retrieve, "register_dataset"):
|
|
852
|
+
current_app.retrieve.register_dataset(dataset_info.copy())
|
|
853
|
+
else:
|
|
854
|
+
logger.warning("Retrieve plugin has no method 'register_dataset'")
|
|
855
|
+
logger.warning("Applied modifications to dataset entry '%s' will not take effect in retrieve database.'",
|
|
856
|
+
dataset_info["uri"])
|
|
857
|
+
except ValidationError as message:
|
|
858
|
+
logger.error(message)
|
|
859
|
+
raise
|
|
860
|
+
|
|
861
|
+
# On any other optional extension, we want to be lenient. We still
|
|
862
|
+
# want to keep track of any error, but the registration should not fail.
|
|
863
|
+
for ex in current_app.custom_extensions:
|
|
864
|
+
try:
|
|
865
|
+
ex.register_dataset(dataset_info.copy())
|
|
866
|
+
except Exception as message:
|
|
867
|
+
logger.warning(message)
|
|
868
|
+
|
|
869
|
+
# this is double bookkeeping, next to delegating dataset registration to
|
|
870
|
+
# the search plugin, we also store metadata in an sql table
|
|
871
|
+
register_dataset_admin_metadata(dataset_info)
|
|
872
|
+
|
|
873
|
+
return dataset_info["uri"]
|
|
874
|
+
|
|
875
|
+
|
|
876
|
+
def delete_dataset(uri):
|
|
877
|
+
"""Delete a dataset in the lookup server. Idempotent.
|
|
878
|
+
|
|
879
|
+
:param uri: URI of dataset entry to remove from dserver.
|
|
880
|
+
:returns: URI of successfully removed dataset entry.
|
|
881
|
+
|
|
882
|
+
:raises ValidationError: if deletion fails within dserver core, or within search and retrieve plugin."""
|
|
883
|
+
|
|
884
|
+
# On the core search and retrieve plugins, we are strict. We expect them
|
|
885
|
+
# to raise ValidationError on failure, we log those, and reraise
|
|
886
|
+
try:
|
|
887
|
+
if hasattr(current_app.search, "delete_dataset"):
|
|
888
|
+
current_app.search.delete_dataset(uri)
|
|
889
|
+
else:
|
|
890
|
+
logger.warning("Search plugin has no method 'delete_dataset'")
|
|
891
|
+
logger.warning("Deletion of dataset entry '%s' will not take effect in search database.'", uri)
|
|
892
|
+
except ValidationError as message:
|
|
893
|
+
# Instead of reporting the error with the logging module, we will want
|
|
894
|
+
# to do some bookkeeping on registration errors per URI in the
|
|
895
|
+
# core SQL db
|
|
896
|
+
logger.error(message)
|
|
897
|
+
raise
|
|
898
|
+
|
|
899
|
+
try:
|
|
900
|
+
if hasattr(current_app.retrieve, "delete_dataset"):
|
|
901
|
+
current_app.retrieve.delete_dataset(uri)
|
|
902
|
+
else:
|
|
903
|
+
logger.warning("Retrieve plugin has no method 'delete_dataset'")
|
|
904
|
+
logger.warning("Deletion of dataset entry '%s' will not take effect in retrieve database.'", uri)
|
|
905
|
+
except ValidationError as message:
|
|
906
|
+
logger.error(message)
|
|
907
|
+
raise
|
|
908
|
+
|
|
909
|
+
# On any other optional extension, we want to be lenient. We still
|
|
910
|
+
# want to keep track of any error, but the registration should not fail.
|
|
911
|
+
for ex in current_app.custom_extensions:
|
|
912
|
+
try:
|
|
913
|
+
ex.delete_dataset(uri)
|
|
914
|
+
except Exception as message:
|
|
915
|
+
logger.warning(message)
|
|
916
|
+
|
|
917
|
+
# this is double bookkeeping, next to delegating dataset registration to
|
|
918
|
+
# the search plugin, we also store metadata in an sql table
|
|
919
|
+
delete_dataset_admin_metadata(uri)
|
|
920
|
+
|
|
921
|
+
return uri
|
|
922
|
+
|
|
923
|
+
#############################################################################
|
|
924
|
+
# Dataset information retrieval helper functions
|
|
925
|
+
#############################################################################
|
|
926
|
+
|
|
927
|
+
|
|
928
|
+
def get_admin_metadata_from_uri(uri):
|
|
929
|
+
"""Return the dataset SQL table row as dictionary."""
|
|
930
|
+
dataset = Dataset.query.filter_by(uri=uri).first()
|
|
931
|
+
|
|
932
|
+
if dataset is None:
|
|
933
|
+
return None
|
|
934
|
+
|
|
935
|
+
return dataset.as_dict()
|
|
936
|
+
|
|
937
|
+
|
|
938
|
+
def list_admin_metadata_in_base_uri(base_uri_str):
|
|
939
|
+
"""Return list of dictionaries with admin metadata from dataset SQL table.""" # NOQA
|
|
940
|
+
base_uri = get_base_uri_obj(base_uri_str)
|
|
941
|
+
|
|
942
|
+
if base_uri is None:
|
|
943
|
+
return None
|
|
944
|
+
|
|
945
|
+
return [ds.as_dict() for ds in base_uri.datasets]
|
|
946
|
+
|
|
947
|
+
|
|
948
|
+
#############################################################################
|
|
949
|
+
# Retrieve plugin interface
|
|
950
|
+
#############################################################################
|
|
951
|
+
|
|
952
|
+
|
|
953
|
+
def get_readme_from_uri_by_user(username, uri):
|
|
954
|
+
"""Return the readme.
|
|
955
|
+
|
|
956
|
+
:param username: username
|
|
957
|
+
:param uri: dataset URI
|
|
958
|
+
:returns: dataset readme
|
|
959
|
+
:raises: AuthenticationError if user is invalid.
|
|
960
|
+
AuthorizationError if the user has not got permissions to read
|
|
961
|
+
content in the base URI
|
|
962
|
+
UnknownBaseURIError if the base URI has not been registered.
|
|
963
|
+
UnknownURIError if the URI is not available to the user.
|
|
964
|
+
"""
|
|
965
|
+
user = get_user_obj(username)
|
|
966
|
+
|
|
967
|
+
base_uri_str = uri.rsplit("/", 1)[0]
|
|
968
|
+
base_uri = _get_base_uri_obj(base_uri_str)
|
|
969
|
+
if base_uri is None:
|
|
970
|
+
raise (UnknownBaseURIError())
|
|
971
|
+
|
|
972
|
+
if base_uri not in user.search_base_uris:
|
|
973
|
+
raise (AuthorizationError())
|
|
974
|
+
|
|
975
|
+
return current_app.retrieve.get_readme(uri)
|
|
976
|
+
|
|
977
|
+
|
|
978
|
+
def get_manifest_from_uri_by_user(username, uri):
|
|
979
|
+
"""Return the manifest.
|
|
980
|
+
|
|
981
|
+
:param username: username
|
|
982
|
+
:param uri: dataset URI
|
|
983
|
+
:returns: dataset manifest
|
|
984
|
+
:raises: AuthenticationError if user is invalid.
|
|
985
|
+
AuthorizationError if the user has not got permissions to read
|
|
986
|
+
content in the base URI
|
|
987
|
+
UnknownBaseURIError if the base URI has not been registered.
|
|
988
|
+
UnknownURIError if the URI is not available to the user.
|
|
989
|
+
"""
|
|
990
|
+
user = get_user_obj(username)
|
|
991
|
+
|
|
992
|
+
base_uri_str = uri.rsplit("/", 1)[0]
|
|
993
|
+
base_uri = _get_base_uri_obj(base_uri_str)
|
|
994
|
+
if base_uri is None:
|
|
995
|
+
raise (UnknownBaseURIError())
|
|
996
|
+
|
|
997
|
+
if base_uri not in user.search_base_uris:
|
|
998
|
+
raise (AuthorizationError())
|
|
999
|
+
|
|
1000
|
+
return current_app.retrieve.get_manifest(uri)
|
|
1001
|
+
|
|
1002
|
+
|
|
1003
|
+
def get_tags_from_uri_by_user(username, uri):
|
|
1004
|
+
"""Return tags.
|
|
1005
|
+
|
|
1006
|
+
:param username: username
|
|
1007
|
+
:param uri: dataset URI
|
|
1008
|
+
:returns: dataset tags
|
|
1009
|
+
:raises: AuthenticationError if user is invalid.
|
|
1010
|
+
AuthorizationError if the user has not got permissions to read
|
|
1011
|
+
content in the base URI
|
|
1012
|
+
UnknownBaseURIError if the base URI has not been registered.
|
|
1013
|
+
UnknownURIError if the URI is not available to the user.
|
|
1014
|
+
"""
|
|
1015
|
+
user = get_user_obj(username)
|
|
1016
|
+
|
|
1017
|
+
base_uri_str = uri.rsplit("/", 1)[0]
|
|
1018
|
+
base_uri = _get_base_uri_obj(base_uri_str)
|
|
1019
|
+
if base_uri is None:
|
|
1020
|
+
raise (UnknownBaseURIError())
|
|
1021
|
+
|
|
1022
|
+
if base_uri not in user.search_base_uris:
|
|
1023
|
+
raise (AuthorizationError())
|
|
1024
|
+
|
|
1025
|
+
return current_app.retrieve.get_tags(uri)
|
|
1026
|
+
|
|
1027
|
+
|
|
1028
|
+
def get_annotations_from_uri_by_user(username, uri):
|
|
1029
|
+
"""Return the annotations.
|
|
1030
|
+
|
|
1031
|
+
:param username: username
|
|
1032
|
+
:param uri: dataset URI
|
|
1033
|
+
:returns: dataset annotations
|
|
1034
|
+
:raises: AuthenticationError if user is invalid.
|
|
1035
|
+
AuthorizationError if the user has not got permissions to read
|
|
1036
|
+
content in the base URI
|
|
1037
|
+
UnknownBaseURIError if the base URI has not been registered.
|
|
1038
|
+
UnknownURIError if the URI is not available to the user.
|
|
1039
|
+
"""
|
|
1040
|
+
user = get_user_obj(username)
|
|
1041
|
+
|
|
1042
|
+
base_uri_str = uri.rsplit("/", 1)[0]
|
|
1043
|
+
base_uri = _get_base_uri_obj(base_uri_str)
|
|
1044
|
+
if base_uri is None:
|
|
1045
|
+
raise (UnknownBaseURIError())
|
|
1046
|
+
|
|
1047
|
+
if base_uri not in user.search_base_uris:
|
|
1048
|
+
raise (AuthorizationError())
|
|
1049
|
+
|
|
1050
|
+
return current_app.retrieve.get_annotations(uri)
|