Browse Source

Updated generated code with LeRel2Type child classes

Yann Weber 9 years ago
parent
commit
61786cc71e
3 changed files with 36 additions and 17 deletions
  1. 2
    2
      leapi/leclass.py
  2. 32
    13
      leapi/lefactory.py
  3. 2
    2
      leapi/lerelation.py

+ 2
- 2
leapi/leclass.py View File

@@ -10,8 +10,8 @@ class _LeClass(_LeObject):
10 10
 
11 11
     ## @brief Stores fieldtypes by field name
12 12
     _fieldtypes = dict()
13
-    ## @brief Stores relation with some LeType using rel2type fields. Key = rel2type fieldname value = LeType class
14
-    _linked_types = dict()
13
+    ## @brief Stores authorized link2type
14
+    _linked_types = list()
15 15
     ## @brief Stores fieldgroups and the fields they contains
16 16
     _fieldgroups = dict()
17 17
     ## @brief Stores the EM uid

+ 32
- 13
leapi/lefactory.py View File

@@ -75,6 +75,31 @@ class LeFactory(object):
75 75
                 res_ft_l.append( '%s: %s'%(repr(fname), constructor) )
76 76
         return (res_uid_ft, res_ft_l)
77 77
 
78
+    ## @brief Given a Model generate concrete instances of LeRel2Type classes to represent relations
79
+    # @param model : the EditorialModel
80
+    # @return python code
81
+    def emrel2type_pycode(self, model):
82
+        res_code = ""
83
+        for field in [ f for f in model.components('EmField') if f.fieldtype == 'rel2type']:
84
+            related = model.component(field.rel_to_type_id)
85
+            src = field.em_class
86
+            cls_name = "Rel_%s2%s"%(src.name, related.name)
87
+
88
+            attr_l = dict()
89
+            for attr in [ f for f in model.components('EmField') if f.rel_field_id == field.uid]:
90
+                attr_l[attr.name] = LeFactory.fieldtype_construct_from_field(attr)
91
+
92
+            rel_code = """
93
+class {classname}(LeRel2Type):
94
+    _rel_attr_fieldtypes = {attr_dict}
95
+
96
+""".format(
97
+    classname = cls_name,
98
+    attr_dict = "{" + (','.join(['\n    %s: %s' % (repr(f), v) for f,v in attr_l.items()])) + "\n}"
99
+)
100
+            res_code += rel_code
101
+        return res_code
102
+
78 103
     ## @brief Given a Model and an EmClass instances generate python code for corresponding LeClass
79 104
     # @param model Model : A Model instance
80 105
     # @param emclass EmClass : An EmClass instance from model
@@ -82,13 +107,11 @@ class LeFactory(object):
82 107
     def emclass_pycode(self, model, emclass):
83 108
 
84 109
         cls_fields = dict()
85
-        cls_linked_types = dict() #keys are LeType classnames and values are tuples (attr_fieldname, attr_fieldtype)
110
+        cls_linked_types = list() #Stores authorized LeObject for rel2type
86 111
         #Populating linked_type attr
87 112
         for rfield in [ f for f in emclass.fields() if f.fieldtype == 'rel2type']:
88 113
             fti = rfield.fieldtype_instance()
89
-            cls_linked_types[LeFactory.name2classname(model.component(fti.rel_to_type_id).name)] = [
90
-                (f.name, LeFactory.fieldtype_construct_from_field(f)) for f in model.components('EmField') if f.rel_field_id == rfield.uid 
91
-            ]
114
+            cls_linked_types.append(LeFactory.name2classname(model.component(fti.rel_to_type_id).name))
92 115
         # Populating fieldtype attr
93 116
         for field in emclass.fields(relational = False):
94 117
             self.needed_fieldtypes |= set([field.fieldtype])
@@ -104,15 +127,7 @@ class LeFactory(object):
104 127
             name = LeFactory.name2classname(emclass.name),
105 128
             ftypes = "{" + (','.join(['\n    %s: %s' % (repr(f), v) for f, v in cls_fields.items()])) + "\n}",
106 129
 
107
-            ltypes = '{'+ (','.join(
108
-                [
109
-                    '\n    {ltname}: {ltattr_list}'.format(
110
-                        ltname = lt,
111
-                        ltattr_list = '['+(', '.join([
112
-                            '(%s, %s)'%(repr(ltname), ltftype) for ltname, ltftype in ltattr
113
-                        ]))+']'
114
-                    ) for lt, ltattr in cls_linked_types.items()
115
-                ]))+'}',
130
+            ltypes = "[" + (','.join(cls_linked_types))+"]",
116 131
             classtype = repr(emclass.classtype)
117 132
         )
118 133
 
@@ -289,12 +304,16 @@ class {name}(LeType, {leclass}):
289 304
                 uid=emtype.uid
290 305
             )
291 306
 
307
+        #Generating concret class of LeRel2Type
308
+        result += self.emrel2type_pycode(model)
309
+
292 310
         #Set attributes of created LeClass and LeType child classes
293 311
         for emclass in emclass_l:
294 312
             result += self.emclass_pycode(model, emclass)
295 313
         for emtype in emtype_l:
296 314
             result += self.emtype_pycode(model, emtype)
297 315
 
316
+
298 317
         #Populating LeObject._me_uid dict for a rapid fetch of LeType and LeClass given an EM uid
299 318
         me_uid = {comp.uid: LeFactory.name2classname(comp.name) for comp in emclass_l + emtype_l}
300 319
         result += """

+ 2
- 2
leapi/lerelation.py View File

@@ -9,9 +9,9 @@ from . import lecrud
9 9
 class _LeRelation(lecrud._LeCrud):
10 10
     
11 11
     ## @brief Handles the superior
12
-    _lesup_fieldtype = ft_leo.EmFieldType(True)
12
+    _lesup_fieldtype = {'lesup': ft_leo.EmFieldType(True)}
13 13
     ## @brief Handles the subordinate
14
-    _lesub_fieldtype = ft_leo.EmFieldType(False)
14
+    _lesub_fieldtype = {'lesub': ft_leo.EmFieldType(False) }
15 15
     ## @brief Stores the list of fieldtypes that are common to all relations
16 16
     _rel_fieldtypes = dict()
17 17
     ## @brief Stores the list of fieldtypes handling relations attributes

Loading…
Cancel
Save