1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
|
3
|
|
|
import six |
4
|
|
|
|
5
|
|
|
from bika.lims import api |
6
|
|
|
from persistent.dict import PersistentDict |
7
|
|
|
from persistent.list import PersistentList |
8
|
|
|
from senaite.core import logger |
9
|
|
|
from senaite.core.schema.fields import BaseField |
10
|
|
|
from senaite.core.schema.interfaces import IUIDReferenceField |
11
|
|
|
from zope.annotation.interfaces import IAnnotations |
12
|
|
|
from zope.interface import implementer |
13
|
|
|
from zope.schema import ASCIILine |
14
|
|
|
from Acquisition import aq_base |
15
|
|
|
from zope.schema import List |
16
|
|
|
|
17
|
|
|
BACKREFS_STORAGE = "senaite.core.schema.uidreferencefield.backreferences" |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
def get_brefs(context, relationship, as_objects=False): |
21
|
|
|
"""Return backreferences of the context |
22
|
|
|
|
23
|
|
|
:returns: List of UIDs that are linked by the relationship |
24
|
|
|
""" |
25
|
|
|
context = aq_base(context) |
26
|
|
|
# get the bref annotation storage of the context |
27
|
|
|
brefs = get_bref_storage(context) |
28
|
|
|
# get the referenced UIDs |
29
|
|
|
bref_uids = list(brefs.get(relationship, [])) |
30
|
|
|
|
31
|
|
|
if not bref_uids: |
32
|
|
|
return [] |
33
|
|
|
|
34
|
|
|
if as_objects is True: |
35
|
|
|
return [api.get_object(uid) for uid in bref_uids] |
36
|
|
|
|
37
|
|
|
return bref_uids |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
def get_bref_storage(context): |
41
|
|
|
"""Get the annotation storage for backreferences of the context |
42
|
|
|
""" |
43
|
|
|
annotation = IAnnotations(context) |
44
|
|
|
if annotation.get(BACKREFS_STORAGE) is None: |
|
|
|
|
45
|
|
|
annotation[BACKREFS_STORAGE] = PersistentDict() |
46
|
|
|
return annotation[BACKREFS_STORAGE] |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
@implementer(IUIDReferenceField) |
50
|
|
|
class UIDReferenceField(List, BaseField): |
51
|
|
|
"""Stores UID references to other objects |
52
|
|
|
""" |
53
|
|
|
|
54
|
|
|
value_type = ASCIILine(title=u"UID") |
55
|
|
|
|
56
|
|
|
def __init__(self, allowed_types=None, multi_valued=True, **kw): |
57
|
|
|
if allowed_types is None: |
58
|
|
|
allowed_types = () |
59
|
|
|
self.allowed_types = allowed_types |
60
|
|
|
self.multi_valued = multi_valued |
61
|
|
|
super(UIDReferenceField, self).__init__(**kw) |
62
|
|
|
|
63
|
|
|
def get_relationship_key(self, context): |
64
|
|
|
"""Relationship key used for backreferences |
65
|
|
|
|
66
|
|
|
The key used for the annotation storage on the referenced object to |
67
|
|
|
remember the current object UID. |
68
|
|
|
|
69
|
|
|
:returns: storage key to lookup back references |
70
|
|
|
""" |
71
|
|
|
portal_type = api.get_portal_type(context) |
72
|
|
|
return "%s.%s" % (portal_type, self.__name__) |
73
|
|
|
|
74
|
|
|
def get_uid(self, value): |
75
|
|
|
"""Value -> UID |
76
|
|
|
|
77
|
|
|
:parm value: object/UID/SuperModel |
78
|
|
|
:returns: UID |
79
|
|
|
""" |
80
|
|
|
try: |
81
|
|
|
return api.get_uid(value) |
82
|
|
|
except api.APIError: |
83
|
|
|
return None |
84
|
|
|
|
85
|
|
|
def get_object(self, value): |
86
|
|
|
"""Value -> object |
87
|
|
|
|
88
|
|
|
:returns: Object or None |
89
|
|
|
""" |
90
|
|
|
try: |
91
|
|
|
return api.get_object(value) |
92
|
|
|
except api.APIError: |
93
|
|
|
return None |
94
|
|
|
|
95
|
|
|
def get_allowed_types(self): |
96
|
|
|
"""Returns the allowed reference types |
97
|
|
|
|
98
|
|
|
:returns: tuple of allowed_types |
99
|
|
|
""" |
100
|
|
|
allowed_types = self.allowed_types |
101
|
|
|
if not allowed_types: |
102
|
|
|
allowed_types = () |
103
|
|
|
elif isinstance(allowed_types, six.string_types): |
104
|
|
|
allowed_types = (allowed_types, ) |
105
|
|
|
return allowed_types |
106
|
|
|
|
107
|
|
|
def set(self, object, value): |
108
|
|
|
"""Set UID reference |
109
|
|
|
|
110
|
|
|
:param object: the instance of the field |
111
|
|
|
:param value: object/UID/SuperModel |
112
|
|
|
:type value: list/tuple/str |
113
|
|
|
""" |
114
|
|
|
|
115
|
|
|
# always handle all values internally as a list |
116
|
|
|
if isinstance(value, six.string_types): |
117
|
|
|
value = [value] |
118
|
|
|
elif api.is_object(value): |
119
|
|
|
value = [value] |
120
|
|
|
|
121
|
|
|
# convert to UIDs |
122
|
|
|
uids = [] |
123
|
|
|
for v in value: |
124
|
|
|
uid = self.get_uid(v) |
125
|
|
|
if uid is None: |
126
|
|
|
continue |
127
|
|
|
uids.append(uid) |
128
|
|
|
|
129
|
|
|
# current set UIDs |
130
|
|
|
existing = self.get_raw(object) |
131
|
|
|
|
132
|
|
|
# filter out new/removed UIDs |
133
|
|
|
added_uids = [u for u in uids if u not in existing] |
134
|
|
|
added_objs = filter(None, map(self.get_object, added_uids)) |
135
|
|
|
|
136
|
|
|
removed_uids = [u for u in existing if u not in uids] |
137
|
|
|
removed_objs = filter(None, map(self.get_object, removed_uids)) |
138
|
|
|
|
139
|
|
|
# link backreferences of new uids |
140
|
|
|
for added_obj in added_objs: |
141
|
|
|
self.link_bref(added_obj, object) |
142
|
|
|
|
143
|
|
|
# unlink backreferences of removed UIDs |
144
|
|
|
for removed_obj in removed_objs: |
145
|
|
|
self.unlink_bref(removed_obj, object) |
146
|
|
|
|
147
|
|
|
super(UIDReferenceField, self).set(object, uids) |
148
|
|
|
|
149
|
|
|
def unlink_bref(self, source, target): |
150
|
|
|
"""Remove backreference from the source to the target |
151
|
|
|
|
152
|
|
|
:param source: the object where the bref is stored (our reference) |
153
|
|
|
:param target: the object where the bref points to (our object) |
154
|
|
|
:returns: True when the bref was removed, False otherwise |
155
|
|
|
""" |
156
|
|
|
target_uid = self.get_uid(target) |
157
|
|
|
# get the storage key |
158
|
|
|
key = self.get_relationship_key(target) |
159
|
|
|
# get all backreferences from the source |
160
|
|
|
brefs = get_bref_storage(source) |
161
|
|
|
if key not in brefs: |
162
|
|
|
logger.warn( |
163
|
|
|
"Referenced object {} has no backreferences for the key {}" |
164
|
|
|
.format(repr(source), key)) |
165
|
|
|
return False |
166
|
|
|
if target_uid not in brefs[key]: |
167
|
|
|
logger.warn("Target {} was not linked by {}" |
168
|
|
|
.format(repr(target), repr(source))) |
169
|
|
|
return False |
170
|
|
|
brefs[key].remove(target_uid) |
171
|
|
|
return True |
172
|
|
|
|
173
|
|
|
def link_bref(self, source, target): |
174
|
|
|
"""Add backreference from the source to the target |
175
|
|
|
|
176
|
|
|
:param source: the object where the bref is stored (our reference) |
177
|
|
|
:param target: the object where the bref points to (our object) |
178
|
|
|
:returns: True when the bref was written |
179
|
|
|
""" |
180
|
|
|
target_uid = api.get_uid(target) |
181
|
|
|
# get the annotation storage key |
182
|
|
|
key = self.get_relationship_key(target) |
183
|
|
|
# get all backreferences |
184
|
|
|
brefs = get_bref_storage(source) |
185
|
|
|
if key not in brefs: |
186
|
|
|
brefs[key] = PersistentList() |
187
|
|
|
if target_uid not in brefs[key]: |
188
|
|
|
brefs[key].append(target_uid) |
189
|
|
|
return True |
190
|
|
|
|
191
|
|
|
def get(self, object): |
192
|
|
|
"""Get referenced objects |
193
|
|
|
|
194
|
|
|
:param object: instance of the field |
195
|
|
|
:returns: list of referenced objects |
196
|
|
|
""" |
197
|
|
|
return self._get(object, as_objects=True) |
198
|
|
|
|
199
|
|
|
def get_raw(self, object): |
200
|
|
|
"""Get referenced UIDs |
201
|
|
|
|
202
|
|
|
:param object: instance of the field |
203
|
|
|
:returns: list of referenced UIDs |
204
|
|
|
""" |
205
|
|
|
return self._get(object, as_objects=False) |
206
|
|
|
|
207
|
|
|
def _get(self, object, as_objects=False): |
208
|
|
|
"""Returns single/multi value |
209
|
|
|
|
210
|
|
|
:param object: instance of the field |
211
|
|
|
:param as_objects: Flag for UID/object returns |
212
|
|
|
:returns: list of referenced UIDs |
213
|
|
|
""" |
214
|
|
|
uids = super(UIDReferenceField, self).get(object) |
215
|
|
|
|
216
|
|
|
if not uids: |
217
|
|
|
uids = [] |
218
|
|
|
|
219
|
|
|
if as_objects is True: |
220
|
|
|
uids = filter(None, map(self.get_object, uids)) |
221
|
|
|
|
222
|
|
|
if self.multi_valued: |
223
|
|
|
return uids |
224
|
|
|
if len(uids) == 0: |
225
|
|
|
return None |
226
|
|
|
return uids[0] |
227
|
|
|
|
228
|
|
|
def _validate(self, value): |
229
|
|
|
""" |
230
|
|
|
""" |
231
|
|
|
super(UIDReferenceField, self)._validate(value) |
232
|
|
|
# check if the fields accepts single values only |
233
|
|
|
if not self.multi_valued and len(value) > 1: |
234
|
|
|
raise ValueError("Single valued field accepts at most 1 value") |
235
|
|
|
|
236
|
|
|
# check for valid UIDs |
237
|
|
|
for uid in value: |
238
|
|
|
if not api.is_uid(uid): |
239
|
|
|
raise ValueError("Invalid UID: '%s'" % uid) |
240
|
|
|
|
241
|
|
|
# check if the type is allowed |
242
|
|
|
allowed_types = self.get_allowed_types() |
243
|
|
|
if allowed_types: |
244
|
|
|
objs = filter(None, map(self.get_object, value)) |
245
|
|
|
types = set(map(api.get_portal_type, objs)) |
246
|
|
|
if not types.issubset(allowed_types): |
247
|
|
|
raise ValueError("Only the following types are allowed: %s" |
248
|
|
|
% ",".join(allowed_types)) |
249
|
|
|
|