Procházet zdrojové kódy

Code cleaning for generic fieldtype

Yann Weber před 8 roky
rodič
revize
e7c1af45d9

+ 1
- 1
EditorialModel/fieldtypes/concat.py Zobrazit soubor

@@ -9,7 +9,7 @@ class EmFieldType(char.EmFieldType):
9 9
         self._field_list = field_list
10 10
         super().__init__(internal='automatic', max_length = max_length)
11 11
 
12
-    def _construct_data(self, lec, fname, datas, cur_value):
12
+    def construct_data(self, lec, fname, datas, cur_value):
13 13
         ret = ''
14 14
         for fname in self._field_list:
15 15
             ret += datas[fname]

+ 1
- 1
EditorialModel/fieldtypes/emuid.py Zobrazit soubor

@@ -20,7 +20,7 @@ class EmFieldType(integer.EmFieldType):
20 20
     def _check_data_value(self, value):
21 21
         return (value, None)
22 22
 
23
-    def _construct_data(self, lec, fname, datas, cur_value):
23
+    def construct_data(self, lec, fname, datas, cur_value):
24 24
         ret = None
25 25
         if self.is_id_class:
26 26
             if lec.implements_leclass():

+ 22
- 10
EditorialModel/fieldtypes/generic.py Zobrazit soubor

@@ -39,12 +39,6 @@ class GenericFieldType(object):
39 39
     def _check_data_value(self, value):
40 40
         return (value, None)
41 41
     
42
-    ## @brief Wrapper for _construct_data() method
43
-    # 
44
-    # Now useless
45
-    def construct_data(self, lec, fname, datas, cur_value):
46
-        return self._construct_data(lec, fname, datas, cur_value)
47
-
48 42
     ## @brief Build field value
49 43
     # @param lec LeCrud : A LeCrud child class
50 44
     # @param fname str : The field name
@@ -52,7 +46,7 @@ class GenericFieldType(object):
52 46
     # @param cur_value : the value for the current field (identified by fieldname)
53 47
     # @return constructed datas (for the fname field)
54 48
     # @throw RuntimeError if unable to construct data
55
-    def _construct_data(self, lec, fname, datas, cur_value):
49
+    def construct_data(self, lec, fname, datas, cur_value):
56 50
         if fname in datas.keys():
57 51
             return cur_value
58 52
         elif hasattr(lec.fieldtypes()[fname], 'default'):
@@ -91,6 +85,7 @@ class GenericFieldType(object):
91 85
             hash_dats.append((kdic, getattr(self, kdic)))
92 86
         return hash(tuple(hash_dats))
93 87
 
88
+## @brief Represent fieldtypes handling a single value
94 89
 class SingleValueFieldType(GenericFieldType):
95 90
     
96 91
     ## @brief Instanciate a new fieldtype
@@ -121,6 +116,9 @@ class SingleValueFieldType(GenericFieldType):
121 116
             return (None, None)
122 117
         return super().check_data_value(value)
123 118
 
119
+## @brief Abstract class for fieldtype representing references
120
+#
121
+# In MySQL its foreign keys (for example leo fieldtypes)
124 122
 class ReferenceFieldType(SingleValueFieldType):
125 123
 
126 124
     ## @brief Instanciate a new fieldtype
@@ -145,6 +143,9 @@ class ReferenceFieldType(SingleValueFieldType):
145 143
         );
146 144
         pass
147 145
 
146
+## @brief Abstract class for fieldtypes handling multiple values identified by a key
147
+#
148
+# For example i18n fieldtype
148 149
 class MultiValueFieldType(GenericFieldType):
149 150
     
150 151
     ## @brief Instanciate a new multivalue fieldtype
@@ -164,6 +165,12 @@ class MultiValueFieldType(GenericFieldType):
164 165
         self.value_fieldtype = value_fieldtype
165 166
 
166 167
 ## @brief Class designed to handle datas access will fieldtypes are constructing datas
168
+#
169
+# This class is designed to allow automatic scheduling of construct_data calls. 
170
+#
171
+# In theory it's able to detect circular dependencies
172
+# @todo test circular deps detection
173
+# @todo test circulat deps false positiv
167 174
 class DatasConstructor(object):
168 175
     
169 176
     ## @brief Init a DatasConstructor
@@ -181,10 +188,12 @@ class DatasConstructor(object):
181 188
         self._constructed = []
182 189
         ## Stores construct calls list
183 190
         self._construct_calls = []
184
-        
191
+    
192
+    ## @brief Implements the dict.keys() method on instance
185 193
     def keys(self):
186 194
         return self._datas.keys()
187
-
195
+    
196
+    ## @brief Allows to access the instance like a dict
188 197
     def __getitem__(self, fname):
189 198
         if fname not in self._constructed:
190 199
             if fname in self._construct_calls:
@@ -193,9 +202,12 @@ class DatasConstructor(object):
193 202
             self._datas[fname] = self._fieldtypes[fname].construct_data(self._lec, fname, self, cur_value)
194 203
             self._constructed.append(fname)
195 204
         return self._datas[fname]
196
-
205
+    
206
+    ## @brief Allows to set instance values like a dict
207
+    # @warning Should not append in theory
197 208
     def __setitem__(self, fname, value):
198 209
         self._datas[fname] = value
210
+        warnings.warn("Setting value of an DatasConstructor instance")
199 211
         
200 212
 
201 213
 #

+ 1
- 1
EditorialModel/fieldtypes/leo.py Zobrazit soubor

@@ -25,7 +25,7 @@ class EmFieldType(ReferenceFieldType):
25 25
     
26 26
     ## @brief If field value is an integer, returns a partially instanciated LeObject (only with an ID)
27 27
     # @todo what should we do if the get fails ? Raise ?
28
-    def _construct_data(self, lec, fname, datas, cur_value):
28
+    def construct_data(self, lec, fname, datas, cur_value):
29 29
         if isinstance(cur_value, str):
30 30
             # Cast to int
31 31
             try:

+ 1
- 1
EditorialModel/fieldtypes/rank.py Zobrazit soubor

@@ -15,7 +15,7 @@ class EmFieldType(integer.EmFieldType):
15 15
     def __init__(self, **kwargs):
16 16
         super().__init__(**kwargs)
17 17
 
18
-    def _construct_data(self, lec, fname, datas, cur_value):
18
+    def construct_data(self, lec, fname, datas, cur_value):
19 19
         superior_id = datas[EditorialModel.classtypes.relation_superior]
20 20
         if lec.is_lerel2type():
21 21
             subordinate = lec._subordinate_cls

+ 0
- 3
leapi/lecrud.py Zobrazit soubor

@@ -455,11 +455,8 @@ class _LeCrud(object):
455 455
 
456 456
     ## @brief Construct datas values
457 457
     #
458
-    # @warning assert that datas is complete
459
-    #
460 458
     # @param datas dict : Datas that have been returned by LeCrud.check_datas_value() methods
461 459
     # @return A new dict of datas
462
-    # @todo Decide wether or not the datas are modifed inplace or returned in a new dict (second solution for the moment)
463 460
     @classmethod
464 461
     def _construct_datas(cls, datas):
465 462
         constructor = DatasConstructor(cls, datas, cls.fieldtypes())

+ 1
- 0
settings.py Zobrazit soubor

@@ -4,6 +4,7 @@
4 4
 import pymysql
5 5
 import os
6 6
 
7
+lodel2_lib_path = os.path.dirname(os.path.abspath(__file__))
7 8
 base_path = os.path.dirname(os.path.abspath(__file__))
8 9
 debug = False
9 10
 debug_sql = False

Loading…
Zrušit
Uložit