Browse Source

Started LeRelation and childs implementation

Begin to work on fieldtypes
Yann Weber 9 years ago
parent
commit
9cc1e8ab96

+ 19
- 0
EditorialModel/classtypes.py View File

32
     }
32
     }
33
 }
33
 }
34
 
34
 
35
+relations_common_fields = {
36
+    'id_relation': {
37
+        'fieldtype': 'pk',
38
+        'internal': 'automatic',
39
+    },
40
+    'nature': {
41
+        'fieldtype': 'naturerelation',
42
+        'internal': 'automatic',
43
+    },
44
+    'depth': {
45
+        'fieldtype': 'integer',
46
+        'internal': 'automatic',
47
+    },
48
+    'rank': {
49
+        'fieldtype': 'integer',
50
+        'internal': 'automatic',
51
+    },
52
+}
53
+
35
 
54
 
36
 def pk_name():
55
 def pk_name():
37
     for name, option in common_fields.items():
56
     for name, option in common_fields.items():

+ 1
- 1
EditorialModel/fieldtypes/generic.py View File

12
     ## @brief Text describing the fieldtype
12
     ## @brief Text describing the fieldtype
13
     help = 'Generic field type : abstract class for every fieldtype'
13
     help = 'Generic field type : abstract class for every fieldtype'
14
     ## @brief Allowed type for handled datas
14
     ## @brief Allowed type for handled datas
15
-    _allowed_ftype = ['char', 'str', 'int', 'bool', 'datetime', 'text', 'rel2type']
15
+    _allowed_ftype = ['char', 'str', 'int', 'bool', 'datetime', 'text', 'rel2type', 'leobject']
16
 
16
 
17
     ## @brief The basic lowlevel value type
17
     ## @brief The basic lowlevel value type
18
     ftype = None
18
     ftype = None

+ 1
- 3
EditorialModel/fieldtypes/pk.py View File

18
         }
18
         }
19
         # Checking args
19
         # Checking args
20
         for name, value in kwargs.items():
20
         for name, value in kwargs.items():
21
-            if name not in allowed:
22
-                raise TypeError("Got an unexpected argument '%s' for pk EmFieldType" % name)
23
-            if value != allowed[name]:
21
+            if name in allowed and value != allowed[name]:
24
                 raise ValueError("The value '%s' for argument '%s' for pk EmFieldType is not allowed" % (value, name))
22
                 raise ValueError("The value '%s' for argument '%s' for pk EmFieldType is not allowed" % (value, name))
25
 
23
 
26
         kwargs.update(allowed)
24
         kwargs.update(allowed)

+ 58
- 3
leapi/lefactory.py View File

49
         with open(self._code_filename, "w+") as dynfp:
49
         with open(self._code_filename, "w+") as dynfp:
50
             dynfp.write(self.generate_python(model, datasource_cls, datasource_args))
50
             dynfp.write(self.generate_python(model, datasource_cls, datasource_args))
51
 
51
 
52
+    ## @brief Generate fieldtypes for concret classes
53
+    # @param ft_dict dict : key = fieldname value = fieldtype __init__ args
54
+    # @return (uid_fieldtypes, fieldtypes) designed to be printed in generated code
55
+    def concret_fieldtypes(self, ft_dict):
56
+        res_ft_l = list()
57
+        res_uid_ft = None
58
+        for fname, ftargs in ft_dict.items():
59
+            ftargs = copy.copy(ftargs)
60
+            fieldtype = ftargs['fieldtype']
61
+            self.needed_fieldtypes |= set([fieldtype])
62
+            del(ftargs['fieldtype'])
63
+
64
+            constructor = '{ftname}.EmFieldType(**{ftargs})'.format(
65
+                ftname = GenericFieldType.module_name(fieldtype),
66
+                ftargs = ftargs,
67
+            )
68
+
69
+            if fieldtype == 'pk':
70
+                #
71
+                #       WARNING multiple PK not supported
72
+                #
73
+                res_uid_ft = "{ %s: %s }"%(repr(fname),constructor)
74
+            else:
75
+                res_ft_l.append( '%s: %s'%(repr(fname), constructor) )
76
+        return (res_uid_ft, res_ft_l)
77
+
52
     ## @brief Given a Model and an EmClass instances generate python code for corresponding LeClass
78
     ## @brief Given a Model and an EmClass instances generate python code for corresponding LeClass
53
     # @param model Model : A Model instance
79
     # @param model Model : A Model instance
54
     # @param emclass EmClass : An EmClass instance from model
80
     # @param emclass EmClass : An EmClass instance from model
140
 import leapi
166
 import leapi
141
 import leapi.lecrud
167
 import leapi.lecrud
142
 import leapi.leobject
168
 import leapi.leobject
169
+import leapi.lerelation
143
 from leapi.leclass import _LeClass
170
 from leapi.leclass import _LeClass
144
 from leapi.letype import _LeType
171
 from leapi.letype import _LeType
145
 """
172
 """
155
             leobj_me_uid[comp.uid] = LeFactory.name2classname(comp.name)
182
             leobj_me_uid[comp.uid] = LeFactory.name2classname(comp.name)
156
         
183
         
157
         #Building the fieldtypes dict of LeObject
184
         #Building the fieldtypes dict of LeObject
185
+        (leobj_uid_fieldtype, leobj_fieldtypes) = self.concret_fieldtypes(EditorialModel.classtypes.common_fields)
186
+        """
158
         leobj_fieldtypes = list()
187
         leobj_fieldtypes = list()
159
         leobj_uid_fieldtype = None
188
         leobj_uid_fieldtype = None
160
         for fname, ftargs in EditorialModel.classtypes.common_fields.items():
189
         for fname, ftargs in EditorialModel.classtypes.common_fields.items():
171
                 #
200
                 #
172
                 #       WARNING multiple PK not supported
201
                 #       WARNING multiple PK not supported
173
                 #
202
                 #
174
-                leobj_uid_fieldtype = "{ 'lodel_id': %s }"%constructor
203
+                leobj_uid_fieldtype = "{ %s: %s }"%(repr(fname),constructor)
175
             else:
204
             else:
176
                 leobj_fieldtypes.append( '%s: %s'%(repr(fname), constructor) )
205
                 leobj_fieldtypes.append( '%s: %s'%(repr(fname), constructor) )
177
-            
206
+        """
207
+        #Building the fieldtypes dict for LeRelation
208
+        (lerel_uid_fieldtype, lerel_fieldtypes) = self.concret_fieldtypes(EditorialModel.classtypes.relations_common_fields)
209
+        """
210
+        lerel_fieldtypes = list()
211
+        lerel_uid_fieldtype = None
212
+        for fname, ftargs in EditorialModel.classtypes.relations_common_fields.items():
213
+            ftargs = copy.copy(ftargs)
214
+            fieldtype = ftargs['fieldtype']
215
+            self.needed_fieldtypes |= set([fieldtype])
216
+            del(ftargs['fieldtype'])
217
+
218
+            constructor
219
+        """ 
178
 
220
 
179
         result += """
221
         result += """
180
 ## @brief _LeCrud concret class
222
 ## @brief _LeCrud concret class
190
     _uid_fieldtype = {leo_uid_fieldtype}
232
     _uid_fieldtype = {leo_uid_fieldtype}
191
     _leo_fieldtypes = {leo_fieldtypes}
233
     _leo_fieldtypes = {leo_fieldtypes}
192
 
234
 
235
+## @brief _LeRelation concret class
236
+# @see leapi.lerelation._LeRelation
237
+class LeRelation(LeCrud, leapi.lerelation._LeRelation):
238
+    _uid_fieldtype = {lerel_uid_fieldtype}
239
+    _rel_fieldtypes = {lerel_fieldtypes}
240
+    _rel_attr_fieldtypes = dict()
193
 
241
 
194
-class LeClass(LeObject, _LeClass):
242
+class LeHierarch(LeRelation, leapi.lerelation._LeHierarch):
243
+    _rel_attr_fieldtypes = dict()
244
+
245
+class LeRel2Type(LeRelation, leapi.lerelation._LeRel2Type):
195
     pass
246
     pass
196
 
247
 
248
+class LeClass(LeObject, _LeClass):
249
+    pass
197
 
250
 
198
 class LeType(LeClass, _LeType):
251
 class LeType(LeClass, _LeType):
199
     pass
252
     pass
203
             me_uid_l = repr(leobj_me_uid),
256
             me_uid_l = repr(leobj_me_uid),
204
             leo_uid_fieldtype = leobj_uid_fieldtype,
257
             leo_uid_fieldtype = leobj_uid_fieldtype,
205
             leo_fieldtypes = '{\n\t' + (',\n\t'.join(leobj_fieldtypes))+ '\n\t}',
258
             leo_fieldtypes = '{\n\t' + (',\n\t'.join(leobj_fieldtypes))+ '\n\t}',
259
+            lerel_fieldtypes = '{\n\t' + (',\n\t'.join(lerel_fieldtypes))+ '\n\t}',
260
+            lerel_uid_fieldtype = lerel_uid_fieldtype,
206
         )
261
         )
207
 
262
 
208
         emclass_l = model.components(EditorialModel.classes.EmClass)
263
         emclass_l = model.components(EditorialModel.classes.EmClass)

+ 46
- 0
leapi/lerelation.py View File

1
+#-*- coding: utf-8 -*-
2
+
3
+import copy
4
+
5
+import EditorialModel.fieldtypes.leo as ft_leo
6
+from . import lecrud
7
+
8
+## @brief Main class for relations
9
+class _LeRelation(lecrud._LeCrud):
10
+    
11
+    ## @brief Handles the superior
12
+    _lesup_fieldtype = ft_leo.EmFieldType(True)
13
+    ## @brief Handles the subordinate
14
+    _lesub_fieldtype = ft_leo.EmFieldType(False)
15
+    ## @brief Stores the list of fieldtypes that are common to all relations
16
+    _rel_fieldtypes = dict()
17
+    ## @brief Stores the list of fieldtypes handling relations attributes
18
+    _rel_attr_fieldtypes = dict()
19
+
20
+    def __init__(self, rel_id, **kwargs):
21
+       pass 
22
+ 
23
+    @classmethod
24
+    def fieldtypes(cls):
25
+        rel_ft = dict()
26
+        rel_ft.update(cls._lesup_fieldtype)
27
+        rel_ft.update(cls._lesub_fieldtype)
28
+        rel_ft.update(cls._rel_fieldtypes)
29
+        rel_ft.update(cls._rel_attr_fieldtypes)
30
+        return rel_ft
31
+
32
+    @classmethod
33
+    def _prepare_relational_fields(cls, field):
34
+        return LeApiQueryError("Relational field '%s' given but %s doesn't is not a LeObject"%(field,cls.__name__))
35
+
36
+            
37
+
38
+## @brief Abstract class to handle hierarchy relations
39
+class _LeHierarch(_LeRelation):
40
+    def __init__(self, rel_id):
41
+        pass
42
+
43
+## @brief Abstract class to handle rel2type relations
44
+class _LeRel2Type(_LeRelation):
45
+    pass
46
+    

+ 1
- 1
leapi/test/test_lecrud.py View File

1
 """
1
 """
2
-    Tests for _LeObject and LeObject
2
+    Tests for _LeCrud and LeCrud
3
 """
3
 """
4
 
4
 
5
 import unittest
5
 import unittest

Loading…
Cancel
Save