|
@@ -16,6 +16,7 @@ class FieldValidationError(Exception):
|
16
|
16
|
pass
|
17
|
17
|
|
18
|
18
|
##@brief Base class for all data handlers
|
|
19
|
+#@ingroup lodel2_datahandlers
|
19
|
20
|
class DataHandler(object):
|
20
|
21
|
|
21
|
22
|
_HANDLERS_MODULES = ('datas_base', 'datas', 'references')
|
|
@@ -75,7 +76,10 @@ class DataHandler(object):
|
75
|
76
|
return self.internal is not False
|
76
|
77
|
|
77
|
78
|
##@brief calls the data_field defined _check_data_value() method
|
78
|
|
- # @return tuple (value, error|None)
|
|
79
|
+ #@ingroup lodel2_dh_checks
|
|
80
|
+ #@warning DO NOT REIMPLEMENT THIS METHOD IN A CUSTOM DATAHANDLER (see
|
|
81
|
+ #@ref _construct_data() and @ref lodel2_dh_check_impl )
|
|
82
|
+ #@return tuple (value, error|None)
|
79
|
83
|
def check_data_value(self, value):
|
80
|
84
|
if value is None:
|
81
|
85
|
if not self.nullable:
|
|
@@ -83,6 +87,10 @@ class DataHandler(object):
|
83
|
87
|
|
84
|
88
|
return None, None
|
85
|
89
|
return self._check_data_value(value)
|
|
90
|
+
|
|
91
|
+ ##@brief Designed to be implemented in child classes
|
|
92
|
+ def _check_data_value(self, value):
|
|
93
|
+ return value, None
|
86
|
94
|
|
87
|
95
|
##@brief checks if this class can override the given data handler
|
88
|
96
|
# @param data_handler DataHandler
|
|
@@ -93,35 +101,66 @@ class DataHandler(object):
|
93
|
101
|
return True
|
94
|
102
|
|
95
|
103
|
##@brief Build field value
|
96
|
|
- # @param emcomponent EmComponent : An EmComponent child class instance
|
97
|
|
- # @param fname str : The field name
|
98
|
|
- # @param datas dict : dict storing fields values (from the component)
|
99
|
|
- # @param cur_value : the value from the current field (identified by fieldname)
|
100
|
|
- # @return the value
|
101
|
|
- # @throw RunTimeError if data construction fails
|
|
104
|
+ #@ingroup lodel2_dh_checks
|
|
105
|
+ #@warning DO NOT REIMPLEMENT THIS METHOD IN A CUSTOM DATAHANDLER (see
|
|
106
|
+ #@ref _construct_data() and @ref lodel2_dh_check_impl )
|
|
107
|
+ #@param emcomponent EmComponent : An EmComponent child class instance
|
|
108
|
+ #@param fname str : The field name
|
|
109
|
+ #@param datas dict : dict storing fields values (from the component)
|
|
110
|
+ #@param cur_value : the value from the current field (identified by fieldname)
|
|
111
|
+ #@return the value
|
|
112
|
+ #@throw RunTimeError if data construction fails
|
|
113
|
+ #@todo raise something else
|
102
|
114
|
def construct_data(self, emcomponent, fname, datas, cur_value):
|
103
|
115
|
emcomponent_fields = emcomponent.fields()
|
104
|
116
|
data_handler = None
|
105
|
117
|
if fname in emcomponent_fields:
|
106
|
118
|
data_handler = emcomponent_fields[fname]
|
107
|
|
-
|
|
119
|
+
|
|
120
|
+ new_val = cur_value
|
108
|
121
|
if fname in datas.keys():
|
109
|
|
- return cur_value
|
|
122
|
+ pass
|
110
|
123
|
elif data_handler is not None and hasattr(data_handler, 'default'):
|
111
|
|
- return data_handler.default
|
|
124
|
+ new_val = data_handler.default
|
112
|
125
|
elif data_handler is not None and data_handler.nullable:
|
113
|
|
- return None
|
|
126
|
+ new_val = None
|
|
127
|
+ return self._construct_data(emcomponent, fname, datas, new_val)
|
|
128
|
+
|
|
129
|
+ ##@brief Designed to be reimplemented by child classes
|
|
130
|
+ #@param emcomponent EmComponent : An EmComponent child class instance
|
|
131
|
+ #@param fname str : The field name
|
|
132
|
+ #@param datas dict : dict storing fields values (from the component)
|
|
133
|
+ #@param cur_value : the value from the current field (identified by fieldname)
|
|
134
|
+ #@return the value
|
|
135
|
+ #@see construct_data() lodel2_dh_check_impl
|
|
136
|
+ def _construct_data(self, empcomponent, fname, datas, cur_value):
|
114
|
137
|
return cur_value
|
|
138
|
+
|
115
|
139
|
|
116
|
140
|
##@brief Check datas consistency
|
117
|
|
- # @param emcomponent EmComponent : An EmComponent child class instance
|
118
|
|
- # @param fname : the field name
|
119
|
|
- # @param datas dict : dict storing fields values
|
120
|
|
- # @return an Exception instance if fails else True
|
121
|
|
- # @todo A implémenter
|
|
141
|
+ #@ingroup lodel2_dh_checks
|
|
142
|
+ #@warning DO NOT REIMPLEMENT THIS METHOD IN A CUSTOM DATAHANDLER (see
|
|
143
|
+ #@ref _construct_data() and @ref lodel2_dh_check_impl )
|
|
144
|
+ #@warning the datas argument looks like a dict but is not a dict
|
|
145
|
+ #see @ref base_classes.DatasConstructor "DatasConstructor" and
|
|
146
|
+ #@ref lodel2_dh_datas_construction "Datas construction section"
|
|
147
|
+ #@param emcomponent EmComponent : An EmComponent child class instance
|
|
148
|
+ #@param fname : the field name
|
|
149
|
+ #@param datas dict : dict storing fields values
|
|
150
|
+ #@return an Exception instance if fails else True
|
|
151
|
+ #@todo A implémenter
|
122
|
152
|
def check_data_consistency(self, emcomponent, fname, datas):
|
|
153
|
+ return self._check_data_consistency(emcomponent, fname, datas)
|
|
154
|
+
|
|
155
|
+ ##@brief Designed to be reimplemented by child classes
|
|
156
|
+ #@param emcomponent EmComponent : An EmComponent child class instance
|
|
157
|
+ #@param fname : the field name
|
|
158
|
+ #@param datas dict : dict storing fields values
|
|
159
|
+ #@return an Exception instance if fails else True
|
|
160
|
+ #@see check_data_consistency() lodel2_dh_check_impl
|
|
161
|
+ def _check_data_consistency(self, emcomponent, fname, datas):
|
123
|
162
|
return True
|
124
|
|
-
|
|
163
|
+
|
125
|
164
|
##@brief make consistency after a query
|
126
|
165
|
# @param emcomponent EmComponent : An EmComponent child class instance
|
127
|
166
|
# @param fname : the field name
|
|
@@ -139,7 +178,8 @@ class DataHandler(object):
|
139
|
178
|
if not issubclass(data_handler, DataHandler):
|
140
|
179
|
raise ValueError("A data handler HAS TO be a child class of DataHandler")
|
141
|
180
|
cls.__custom_handlers[name] = data_handler
|
142
|
|
-
|
|
181
|
+
|
|
182
|
+ ##@brief Load all datahandlers
|
143
|
183
|
@classmethod
|
144
|
184
|
def load_base_handlers(cls):
|
145
|
185
|
if cls._base_handlers is None:
|
|
@@ -185,13 +225,23 @@ class DataHandler(object):
|
185
|
225
|
return hash(tuple(hash_dats))
|
186
|
226
|
|
187
|
227
|
##@brief Base class for datas data handler (by opposition with references)
|
|
228
|
+#@ingroup lodel2_datahandlers
|
188
|
229
|
class DataField(DataHandler):
|
189
|
230
|
pass
|
190
|
231
|
|
191
|
232
|
##@brief Abstract class for all references
|
|
233
|
+#@ingroup lodel2_datahandlers
|
192
|
234
|
#
|
193
|
235
|
# References are fields that stores a reference to another
|
194
|
236
|
# editorial object
|
|
237
|
+#
|
|
238
|
+#
|
|
239
|
+#@todo Check data implementation : check_data = is value an UID or an
|
|
240
|
+#LeObject child instance
|
|
241
|
+#@todo Construct data implementation : transform the data into a LeObject
|
|
242
|
+#instance
|
|
243
|
+#@todo Check data consistency implementation : check that LeObject instance
|
|
244
|
+#is from an allowed class
|
195
|
245
|
class Reference(DataHandler):
|
196
|
246
|
base_type="ref"
|
197
|
247
|
|
|
@@ -210,8 +260,6 @@ class Reference(DataHandler):
|
210
|
260
|
#if not issubclass(lodel.leapi.leobject.LeObject, back_reference[0]) or not isinstance(back_reference[1], str):
|
211
|
261
|
# raise TypeError("Back reference was expected to be a tuple(<class LeObject>, str) but got : (%s, %s)" % (back_reference[0], back_reference[1]))
|
212
|
262
|
self.__back_reference = back_reference
|
213
|
|
- self.back_ref = back_reference
|
214
|
|
-
|
215
|
263
|
super().__init__(internal=internal, **kwargs)
|
216
|
264
|
|
217
|
265
|
@property
|
|
@@ -227,11 +275,15 @@ class Reference(DataHandler):
|
227
|
275
|
self.__back_reference = back_reference
|
228
|
276
|
|
229
|
277
|
##@brief Check value
|
230
|
|
- # @param value *
|
231
|
|
- # @return tuple(value, exception)
|
232
|
|
- # @todo implement the check when we have LeObject to check value
|
233
|
|
- def _check_data_value(self, value):
|
234
|
|
- return value, None
|
|
278
|
+ #@param value *
|
|
279
|
+ #@return tuple(value, exception)
|
|
280
|
+ #@todo implement the check when we have LeObject to check value
|
|
281
|
+ def check_data_value(self, value):
|
|
282
|
+ print('REFERENCE check_data value')
|
|
283
|
+ return super().check_data_value(value)
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
|
235
|
287
|
if isinstance(value, lodel.editorial_model.components.EmClass):
|
236
|
288
|
value = [value]
|
237
|
289
|
for elt in value:
|
|
@@ -243,29 +295,36 @@ class Reference(DataHandler):
|
243
|
295
|
return value
|
244
|
296
|
|
245
|
297
|
##@brief Check datas consistency
|
246
|
|
- # @param emcomponent EmComponent : An EmComponent child class instance
|
247
|
|
- # @param fname : the field name
|
248
|
|
- # @param datas dict : dict storing fields values
|
249
|
|
- # @return an Exception instance if fails else True
|
250
|
|
- # @todo A implémenter
|
|
298
|
+ #@param emcomponent EmComponent : An EmComponent child class instance
|
|
299
|
+ #@param fname : the field name
|
|
300
|
+ #@param datas dict : dict storing fields values
|
|
301
|
+ #@return an Exception instance if fails else True
|
|
302
|
+ #@todo check for performance issue and check logics
|
|
303
|
+ #@todo Implements consistency checking on value : Check that the given value
|
|
304
|
+ #points onto an allowed class
|
|
305
|
+ #@warning composed uid capabilities broken here
|
251
|
306
|
def check_data_consistency(self, emcomponent, fname, datas):
|
|
307
|
+ rep = super().check_data_consistency(emcomponent, fname, datas)
|
|
308
|
+ if isinstance(rep, Exception):
|
|
309
|
+ return rep
|
|
310
|
+ if self.back_reference is None:
|
|
311
|
+ return True
|
|
312
|
+ #Checking back reference consistency
|
|
313
|
+
|
|
314
|
+ # !! Reimplement instance fetching in construct data !!
|
252
|
315
|
dh = emcomponent.field(fname)
|
253
|
|
- logger.debug(dh)
|
254
|
|
- logger.info('Warning : multiple uid capabilities are broken here')
|
255
|
|
- uid = datas[emcomponent.uid_fieldname()[0]]
|
256
|
|
- target_class = self.back_ref[0]
|
257
|
|
- target_field = self.back_ref[1]
|
258
|
|
- target_uidfield = traget_class.uid_fieldname()[0]
|
259
|
|
- value = datas[emcomponent.data(fname)]
|
|
316
|
+ uid = datas[emcomponent.uid_fieldname()[0]] #multi uid broken here
|
|
317
|
+ target_class = self.back_reference[0]
|
|
318
|
+ target_field = self.back_reference[1]
|
|
319
|
+ target_uidfield = target_class.uid_fieldname()[0] #multi uid broken here
|
|
320
|
+ value = datas[fname]
|
260
|
321
|
|
261
|
|
- obj = target_class.get((target_uidfield , '=', value))
|
|
322
|
+ obj = target_class.get([(target_uidfield , '=', value)])
|
262
|
323
|
|
263
|
324
|
if len(obj) == 0:
|
264
|
325
|
logger.warning('Object referenced does not exist')
|
265
|
326
|
return False
|
266
|
327
|
|
267
|
|
- obj.set_data(target_field, uid)
|
268
|
|
- obj.update()
|
269
|
328
|
return True
|
270
|
329
|
|
271
|
330
|
##@brief This class represent a data_handler for single reference to another object
|
|
@@ -285,6 +344,7 @@ class SingleRef(Reference):
|
285
|
344
|
|
286
|
345
|
|
287
|
346
|
##@brief This class represent a data_handler for multiple references to another object
|
|
347
|
+#@ingroup lodel2_datahandlers
|
288
|
348
|
#
|
289
|
349
|
# The fields using this data handlers are like SingleRef but can store multiple references in one field
|
290
|
350
|
# @note for the moment split on ',' chars
|
|
@@ -297,22 +357,28 @@ class MultipleRef(Reference):
|
297
|
357
|
super().__init__(**kwargs)
|
298
|
358
|
|
299
|
359
|
|
300
|
|
- def _check_data_value(self, value):
|
|
360
|
+ def check_data_value(self, value):
|
|
361
|
+ value, expt = super().check_data_value(value)
|
|
362
|
+ if expt is not None:
|
|
363
|
+ #error in parent
|
|
364
|
+ return value, expt
|
|
365
|
+ elif value is None:
|
|
366
|
+ #none value
|
|
367
|
+ return value, expt
|
|
368
|
+
|
301
|
369
|
expt = None
|
302
|
370
|
|
303
|
371
|
if isinstance(value, str):
|
304
|
372
|
value, expt = super()._check_data_value(value)
|
305
|
373
|
elif not hasattr(value, '__iter__'):
|
306
|
|
- return None, FieldValidationError("MultipleRef has to be an iterable or a string")
|
|
374
|
+ return None, FieldValidationError("MultipleRef has to be an iterable or a string, '%s' found" % value)
|
307
|
375
|
if self.max_item is not None:
|
308
|
376
|
if self.max_item < len(value):
|
309
|
377
|
return None, FieldValidationError("Too many items")
|
310
|
378
|
return value, expt
|
311
|
379
|
|
312
|
|
- def check_data_consistency(self, emcomponent, fname, datas):
|
313
|
|
- return True
|
314
|
|
-
|
315
|
380
|
def construct_data(self, emcomponent, fname, datas, cur_value):
|
|
381
|
+ cur_value = super().construct_data(emcomponent, fname, datas, cur_value)
|
316
|
382
|
if cur_value == 'None' or cur_value is None or cur_value == '':
|
317
|
383
|
return None
|
318
|
384
|
emcomponent_fields = emcomponent.fields()
|
|
@@ -336,14 +402,14 @@ class MultipleRef(Reference):
|
336
|
402
|
l_value = None
|
337
|
403
|
|
338
|
404
|
if l_value is not None:
|
339
|
|
- if self.back_ref is not None:
|
340
|
|
- br_class = self.back_ref[0]
|
|
405
|
+ if self.back_reference is not None:
|
|
406
|
+ br_class = self.back_reference[0]
|
341
|
407
|
for br_id in l_value:
|
342
|
408
|
query_filters = list()
|
343
|
409
|
query_filters.append((br_class.uid_fieldname()[0], '=', br_id))
|
344
|
410
|
br_obj = br_class.get(query_filters)
|
345
|
411
|
if len(br_obj) != 0:
|
346
|
|
- br_list = br_obj[0].data(self.back_ref[1])
|
|
412
|
+ br_list = br_obj[0].data(self.back_reference[1])
|
347
|
413
|
if br_list is None:
|
348
|
414
|
br_list = list()
|
349
|
415
|
if br_id not in br_list:
|
|
@@ -355,14 +421,15 @@ class MultipleRef(Reference):
|
355
|
421
|
# @param emcomponent EmComponent : An EmComponent child class instance
|
356
|
422
|
# @param fname : the field name
|
357
|
423
|
# @param datas dict : dict storing fields values
|
|
424
|
+ """
|
358
|
425
|
def make_consistency(self, emcomponent, fname, datas):
|
359
|
426
|
dh = emcomponent.field(fname)
|
360
|
427
|
|
361
|
428
|
logger.info('Warning : multiple uid capabilities are broken here')
|
362
|
429
|
uid = datas[emcomponent.uid_fieldname()[0]]
|
363
|
|
- if self.back_ref is not None:
|
364
|
|
- target_class = self.back_ref[0]
|
365
|
|
- target_field = self.back_ref[1]
|
|
430
|
+ if self.back_reference is not None:
|
|
431
|
+ target_class = self.back_reference[0]
|
|
432
|
+ target_field = self.back_reference[1]
|
366
|
433
|
target_uidfield = target_class.uid_fieldname()[0]
|
367
|
434
|
|
368
|
435
|
l_value = datas[fname]
|
|
@@ -382,8 +449,10 @@ class MultipleRef(Reference):
|
382
|
449
|
l_uids_ref.append(uid)
|
383
|
450
|
obj[0].set_data(target_field, l_uids_ref)
|
384
|
451
|
obj[0].update()
|
|
452
|
+ """
|
385
|
453
|
|
386
|
454
|
## @brief Class designed to handle datas access will fieldtypes are constructing datas
|
|
455
|
+#@ingroup lodel2_datahandlers
|
387
|
456
|
#
|
388
|
457
|
# This class is designed to allow automatic scheduling of construct_data calls.
|
389
|
458
|
#
|