|
@@ -1,5 +1,5 @@
|
1
|
1
|
# -*- coding: utf-8 -*-
|
2
|
|
-from lodel.leapi.datahandlers.field_data_handler import FieldDataHandler
|
|
2
|
+from lodel.leapi.datahandlers.field_data_handler import FieldDataHandler, FieldValidationError
|
3
|
3
|
from lodel.editorial_model.components import EmClass
|
4
|
4
|
|
5
|
5
|
|
|
@@ -10,32 +10,20 @@ class Reference(FieldDataHandler):
|
10
|
10
|
# @param internal bool : if False, the field is not internal
|
11
|
11
|
# @param **kwargs : other arguments
|
12
|
12
|
def __init__(self, allowed_classes = None, internal=False, **kwargs):
|
13
|
|
- self.allowed_classes = None if allowed_classes is None else set(allowed_classes)
|
|
13
|
+ self.__allowed_classes = None if allowed_classes is None else set(allowed_classes)
|
14
|
14
|
super().__init__(internal=internal, **kwargs)
|
15
|
15
|
|
16
|
|
- ## @brief gets the target of the reference
|
17
|
|
- def get_relateds(self):
|
18
|
|
- return self._refs
|
19
|
|
-
|
20
|
|
- ## @brief checks if the data value is valid
|
21
|
|
- # @param value
|
22
|
|
- # @return
|
|
16
|
+ ## @brief Check value
|
|
17
|
+ # @param value *
|
|
18
|
+ # @return tuple(value, exception)
|
23
|
19
|
def _check_data_value(self, value):
|
24
|
|
-
|
25
|
|
- if not isinstance(value, self._refs_class):
|
26
|
|
- return value, "The reference should be an instance of %s, %s gotten" % (self._refs_class, value.__class__)
|
27
|
|
-
|
28
|
20
|
if isinstance(value, EmClass):
|
29
|
21
|
value = [value]
|
|
22
|
+ for elt in value:
|
|
23
|
+ if not issubclass(elt.__class__, EmClass):
|
|
24
|
+ return None, FieldValidationError("Some elements of this references are not EmClass instances")
|
|
25
|
+ if self.__allowed_classes is not None:
|
|
26
|
+ if not isinstance(elt, self.__allowed_classes):
|
|
27
|
+ return None, FieldValidationError("Some element of this references are not valids (don't fit with allowed_classes")
|
|
28
|
+ return value
|
30
|
29
|
|
31
|
|
- if isinstance(value, dict):
|
32
|
|
- ref_values = value.values()
|
33
|
|
-
|
34
|
|
- for related in value:
|
35
|
|
- if not isinstance(related, EmClass):
|
36
|
|
- return value, "The reference %s should be an instance of EmClass, %s gotten" % (related.display_name,
|
37
|
|
- related.__class__)
|
38
|
|
- if self.allowed_classes is not None and related.__class__.display_name not in self.allowed_classes:
|
39
|
|
- return value, "The reference %s should be an instance of either one of those classes : %s, %s gotten" % \
|
40
|
|
- (related.display_name, self.allowed_classes, related.__class__)
|
41
|
|
- return value, None
|