Browse Source

First implementation of hierarchical data handler

Yann Weber 8 years ago
parent
commit
2b954d4a4d
3 changed files with 31 additions and 9 deletions
  1. 4
    1
      Makefile
  2. 9
    2
      lodel/leapi/datahandlers/base_classes.py
  3. 18
    6
      lodel/leapi/datahandlers/references.py

+ 4
- 1
Makefile View File

1
 dyncode_filename='lodel/leapi/dyncode.py'
1
 dyncode_filename='lodel/leapi/dyncode.py'
2
 
2
 
3
-all: test doc refresh_dyn
3
+all: tests doc dyncode
4
 
4
 
5
+# generate doxygen documentation
5
 doc: cleandoc
6
 doc: cleandoc
6
 	doxygen
7
 	doxygen
7
 
8
 
9
 em_test:
10
 em_test:
10
 	python3 em_test.py
11
 	python3 em_test.py
11
 
12
 
13
+# generate leapi dynamic code
12
 dyncode: clean_dyn em_test
14
 dyncode: clean_dyn em_test
13
 	python3 scripts/refreshdyn.py examples/em_test.pickle $(dyncode_filename) && echo -e "\n\nCode generated in $(dyncode_filename)"
15
 	python3 scripts/refreshdyn.py examples/em_test.pickle $(dyncode_filename) && echo -e "\n\nCode generated in $(dyncode_filename)"
14
 
16
 
17
+# run tests
15
 tests:
18
 tests:
16
 	./runtest -v
19
 	./runtest -v
17
 
20
 

+ 9
- 2
lodel/leapi/datahandlers/base_classes.py View File

244
 # @note SQL implementation could be tricky
244
 # @note SQL implementation could be tricky
245
 class MultipleRef(Reference):
245
 class MultipleRef(Reference):
246
     
246
     
247
-    def __init__(self, allowed_classes = None, **kwargs):
248
-        super().__init__(allowed_classes = allowed_classes, **kwargs)
247
+    ##
248
+    # @param max_item int | None : indicate the maximum number of item referenced by this field, None mean no limit
249
+    def __init__(self, max_item = None, **kwargs):
250
+        super().__init__(**kwargs)
249
 
251
 
252
+        
253
+    def _check_data_value(self, value):
254
+        if self.max_item is not None:
255
+            if self.max_item < len(value):
256
+                return None, FieldValidationError("To many items")
250
 
257
 

+ 18
- 6
lodel/leapi/datahandlers/references.py View File

10
     # @param allowed_classes list | None : list of allowed em classes if None no restriction
10
     # @param allowed_classes list | None : list of allowed em classes if None no restriction
11
     # @param internal bool
11
     # @param internal bool
12
     # @param kwargs
12
     # @param kwargs
13
-    def __init__(self, allowed_classes=None, internal=False, **kwargs):
14
-        super().__init__(allowed_classes=allowed_classes, internal=internal, **kwargs)
13
+    def __init__(self, max_length = None, **kwargs):
14
+        super().__init__(**kwargs)
15
 
15
 
16
     ## @brief Check value
16
     ## @brief Check value
17
     # @param value *
17
     # @param value *
20
         val, expt = super()._check_data_value()
20
         val, expt = super()._check_data_value()
21
         if not isinstance(expt, Exception):
21
         if not isinstance(expt, Exception):
22
             val = list(val)
22
             val = list(val)
23
+        val, expt = super()._check_data_value(value.values())
23
         return val, expt
24
         return val, expt
24
 
25
 
25
 
26
 
30
     # @param allowed_classes list | None : list of allowed em classes if None no restriction
31
     # @param allowed_classes list | None : list of allowed em classes if None no restriction
31
     # @param internal bool : if False, the field is not internal
32
     # @param internal bool : if False, the field is not internal
32
     # @param kwargs : Other named arguments
33
     # @param kwargs : Other named arguments
33
-    def __init__(self, allowed_classes=None, internal=False, **kwargs):
34
-        super().__init__(allowed_classes=allowed_classes, internal=internal, **kwargs)
34
+    def __init__(self, **kwargs):
35
+        super().__init__(**kwargs)
35
 
36
 
36
     ## @brief Check value
37
     ## @brief Check value
37
     # @param value *
38
     # @param value *
40
         val, expt = super()._check_data_value()
41
         val, expt = super()._check_data_value()
41
         if not isinstance(expt, Exception):
42
         if not isinstance(expt, Exception):
42
             val = set(val)
43
             val = set(val)
44
+        val, expt = super()._check_data_value(value.values())
43
         return val, expt
45
         return val, expt
44
 
46
 
45
 
47
 
50
     # @param allowed_classes list | None : list of allowed em classes if None no restriction
52
     # @param allowed_classes list | None : list of allowed em classes if None no restriction
51
     # @param internal bool : if False, the field is not internal
53
     # @param internal bool : if False, the field is not internal
52
     # @param kwargs : Other named arguments
54
     # @param kwargs : Other named arguments
53
-    def __init__(self, allowed_classes=None, internal=False, **kwargs):
54
-        super().__init__(allowed_classes=allowed_classes, internal=internal, **kwargs)
55
+    def __init__(self, **kwargs):
56
+        super().__init__(**kwargs)
55
 
57
 
56
     ## @brief Check value
58
     ## @brief Check value
57
     # @param value *
59
     # @param value *
63
         return (
65
         return (
64
                 None if isinstance(expt, Exception) else value,
66
                 None if isinstance(expt, Exception) else value,
65
                 expt)
67
                 expt)
68
+
69
+## @brief This Reference class is designed to handler hierarchy with some constraint
70
+class Hierarch(MultipleRef):
71
+    
72
+    ## @brief Instanciate a data handler handling hierarchical relation with constraints
73
+    # @param back_reference tuple : Here it is mandatory to have a back ref (like a parent field)
74
+    # @param max_depth int | None :  limit of depth
75
+    # @param max_childs int | Nine : maximum number of childs by nodes
76
+    def __init__(self, back_reference, max_depth = None, max_childs = None, **kwargs):
77
+        super().__init__(back_reference = back_reference)

Loading…
Cancel
Save