1
|
|
|
import re |
2
|
|
|
|
3
|
|
|
from atramhasis.errors import ConceptSchemeNotFoundException |
4
|
|
|
|
5
|
|
|
""" |
6
|
|
|
Thid module is used when blocking operations on a certain Concept or Collection |
7
|
|
|
that might be used in external applications. |
8
|
|
|
|
9
|
|
|
:versionadded: 0.4.0 |
10
|
|
|
""" |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class ProtectedResourceEvent: |
14
|
|
|
""" |
15
|
|
|
Event triggered when calling a protected operation on a resource |
16
|
|
|
""" |
17
|
|
|
|
18
|
|
|
def __init__(self, uri, request): |
19
|
|
|
self.uri = uri |
20
|
|
|
self.request = request |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
def protected_operation(fn): |
24
|
|
|
""" |
25
|
|
|
use this decorator to prevent an operation from being executed |
26
|
|
|
when the related resource is still in use |
27
|
|
|
""" |
28
|
|
|
|
29
|
|
|
def advice(parent_object, *args, **kw): |
30
|
|
|
request = parent_object.request |
31
|
|
|
url = request.path |
32
|
|
|
match = re.compile(r'/conceptschemes/(\w+)/c/(\w+)').match(url) |
33
|
|
|
scheme_id = match.group(1) |
34
|
|
|
c_id = match.group(2) |
35
|
|
|
provider = request.skos_registry.get_provider(scheme_id) |
36
|
|
|
if not provider: |
37
|
|
|
raise ConceptSchemeNotFoundException(scheme_id) |
38
|
|
|
uri = provider.uri_generator.generate(id=c_id) |
39
|
|
|
event = ProtectedResourceEvent(uri, request) |
40
|
|
|
parent_object.request.registry.notify(event) |
41
|
|
|
return fn(parent_object, *args, **kw) |
42
|
|
|
|
43
|
|
|
return advice |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
class ProtectedResourceException(Exception): |
47
|
|
|
""" |
48
|
|
|
raise this exception when the resource is still used somewhere |
49
|
|
|
|
50
|
|
|
referenced_in should contain locations where the resource is still referenced |
51
|
|
|
""" |
52
|
|
|
|
53
|
|
|
def __init__(self, value, referenced_in): |
54
|
|
|
self.value = value |
55
|
|
|
self.referenced_in = referenced_in |
56
|
|
|
|
57
|
|
|
def __str__(self): |
58
|
|
|
return repr(self.value) |
59
|
|
|
|