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,7 +1,8 @@
1 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 6
 doc: cleandoc
6 7
 	doxygen
7 8
 
@@ -9,9 +10,11 @@ doc: cleandoc
9 10
 em_test:
10 11
 	python3 em_test.py
11 12
 
13
+# generate leapi dynamic code
12 14
 dyncode: clean_dyn em_test
13 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 18
 tests:
16 19
 	./runtest -v
17 20
 

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

@@ -244,7 +244,14 @@ class SingleRef(Reference):
244 244
 # @note SQL implementation could be tricky
245 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,8 +10,8 @@ class List(MultipleRef):
10 10
     # @param allowed_classes list | None : list of allowed em classes if None no restriction
11 11
     # @param internal bool
12 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 16
     ## @brief Check value
17 17
     # @param value *
@@ -20,6 +20,7 @@ class List(MultipleRef):
20 20
         val, expt = super()._check_data_value()
21 21
         if not isinstance(expt, Exception):
22 22
             val = list(val)
23
+        val, expt = super()._check_data_value(value.values())
23 24
         return val, expt
24 25
 
25 26
 
@@ -30,8 +31,8 @@ class Set(MultipleRef):
30 31
     # @param allowed_classes list | None : list of allowed em classes if None no restriction
31 32
     # @param internal bool : if False, the field is not internal
32 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 37
     ## @brief Check value
37 38
     # @param value *
@@ -40,6 +41,7 @@ class Set(MultipleRef):
40 41
         val, expt = super()._check_data_value()
41 42
         if not isinstance(expt, Exception):
42 43
             val = set(val)
44
+        val, expt = super()._check_data_value(value.values())
43 45
         return val, expt
44 46
 
45 47
 
@@ -50,8 +52,8 @@ class Map(MultipleRef):
50 52
     # @param allowed_classes list | None : list of allowed em classes if None no restriction
51 53
     # @param internal bool : if False, the field is not internal
52 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 58
     ## @brief Check value
57 59
     # @param value *
@@ -63,3 +65,13 @@ class Map(MultipleRef):
63 65
         return (
64 66
                 None if isinstance(expt, Exception) else value,
65 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