Browse Source

Begin to work on relations API but some problems appear

Yann Weber 9 years ago
parent
commit
fb848fd1dd
2 changed files with 27 additions and 8 deletions
  1. 14
    3
      leobject/lefactory.py
  2. 13
    5
      leobject/letype.py

+ 14
- 3
leobject/lefactory.py View File

@@ -64,11 +64,13 @@ class LeFactory(object):
64 64
     @staticmethod
65 65
     def emclass_pycode(model, emclass):
66 66
         cls_fields = dict()
67
-        cls_linked_types = list()
67
+        cls_linked_types = dict() #keys are LeType classnames and values are tuples (attr_fieldname, attr_fieldtype)
68 68
         #Populating linked_type attr
69 69
         for rfield in [ f for f in emclass.fields() if f.fieldtype == 'rel2type']:
70 70
             fti = rfield.fieldtype_instance()
71
-            cls_linked_types.append(LeFactory.name2classname(model.component(fti.rel_to_type_id).name))
71
+            cls_linked_types[LeFactory.name2classname(model.component(fti.rel_to_type_id).name)] = [
72
+                (f.name, LeFactory.fieldtype_construct_from_field(f)) for f in model.components('EmField') if f.rel_field_id == rfield.uid 
73
+            ]
72 74
         # Populating fieldtype attr
73 75
         for field in emclass.fields(relational = False):
74 76
             cls_fields[field.name] = LeFactory.fieldtype_construct_from_field(field)
@@ -90,7 +92,16 @@ class LeFactory(object):
90 92
 """.format(
91 93
             name = LeFactory.name2classname(emclass.name),
92 94
             ftypes = "{" + (','.join(['\n\t%s:%s' % (repr(f), v) for f, v in cls_fields.items()])) + "\n}",
93
-            ltypes = "{" + (','.join(cls_linked_types)) + '}',
95
+
96
+            ltypes = '{'+ (','.join(
97
+                [
98
+                    '\n\t{ltname}:{ltattr_list}'.format(
99
+                        ltname = lt,
100
+                        ltattr_list = '['+(','.join([
101
+                            '(%s,%s)'%(repr(ltname), ltftype) for ltname, ltftype in ltattr
102
+                        ]))+']'
103
+                    ) for lt, ltattr in cls_linked_types.items()
104
+                ]))+'}',
94 105
             fgroups = repr(cls_fieldgroup),
95 106
             classtype = repr(emclass.classtype)
96 107
         )

+ 13
- 5
leobject/letype.py View File

@@ -99,7 +99,7 @@ class LeType(object):
99 99
     def del_superior(self, leo, nature):
100 100
         if nature is None:
101 101
             raise ValueError('The argument nature cannot be None')
102
-        return self._datasource(leo.lodel_id, self.lodel_id, nature)
102
+        return self._datasource(leo, self, nature)
103 103
 
104 104
     ## @brief Get the linked objects lodel_id
105 105
     # @return an array of lodel_id linked with this object
@@ -110,20 +110,28 @@ class LeType(object):
110 110
     ## @brief Link this object with a LeObject
111 111
     # @note rel2type
112 112
     # @param leo LeObject : The object to be linked with
113
+    # @param **rel_attr : keywords arguments for relations attributes
113 114
     # @return True if success False allready done
114 115
     # @throw A Leo exception if the link is not allowed
115 116
     # @todo unit tests
116 117
     # @todo find a value for depth and rank....
117
-    def link_to(self, leo):
118
-        if leo.__class__ not in self._linked_types:
118
+    def link_to(self, leo, **rel_attr):
119
+        if leo.__class__ not in self._linked_types.keys():
119 120
             raise leobject.leobject.LeObjectError("Constraint error : %s cannot be linked with %s"%(self.__class__.__name__, leo.__class__.__name__))
120
-        return self._datasource.add_relation(self.lodel_id, leo.lodel_id)
121
+
122
+        for attr_name in rel_attr.keys():
123
+            if attr_name not in [ f for f,g in self._linked_types[leo.__class__] ]:
124
+                raise AttributeError("A rel2type between a %s and a %s doesn't have an attribute %s"%(self.__class__.__name__, leo.__class__.__name__, attr_name))
125
+            if not self._linked_types[leo.__class__][1].check(rel_attr[attr_name]):
126
+                raise ValueError("Wrong value '%s' for attribute %s"%(rel_attr[attr_name], attr_name))
127
+
128
+        return self._datasource.add_relation(self, leo, nature=None, depth=None, rank=None, **rel_attr)
121 129
 
122 130
     ## @brief Remove a link bewteen this object and another
123 131
     # @param leo LeObject : A LeObject instance linked with self
124 132
     # @todo unit tests
125 133
     def unlink(self, leo):
126
-        return self._datasource.del_relation(self.lodel_id, leo.lodel_id)
134
+        return self._datasource.del_relation(self, leo)
127 135
         
128 136
     ## @brief Delete a LeType from the datasource
129 137
     # @param filters list : list of filters (see @ref leobject_filters)

Loading…
Cancel
Save