Ver código fonte

LeCrud: is_letype() and is_leclass() helper functions + use it in LeDataSourceSQL

ArnAud 9 anos atrás
pai
commit
142f2ff3f5
2 arquivos alterados com 32 adições e 19 exclusões
  1. 19
    17
      leapi/datasources/ledatasourcesql.py
  2. 13
    2
      leapi/lecrud.py

+ 19
- 17
leapi/datasources/ledatasourcesql.py Ver arquivo

@@ -51,7 +51,7 @@ class LeDataSourceSQL(DummyDatasource):
51 51
             fields = [(main_table, common_fields)]
52 52
 
53 53
         # it is a LeType or a LeClass, query on main table left join class table on lodel_id
54
-        elif hasattr(target_cls, '_leclass') or hasattr(target_cls, '_class_id'):
54
+        elif target_cls.is_letype() or target_cls.is_leclass():
55 55
             # find main table and main table datas
56 56
             main_table = self.datasource_utils.objects_table_name
57 57
             main_class = target_cls._leclass if hasattr(target_cls, '_leclass') else target_cls
@@ -170,22 +170,24 @@ class LeDataSourceSQL(DummyDatasource):
170 170
     # @todo should work with LeType, LeClass, and Relations
171 171
     def insert(self, target_cls, **datas):
172 172
         # it is a LeType
173
-        if (hasattr(target_cls, '_leclass')):
174
-            # find main table and main table datas
175
-            main_table = self.datasource_utils.objects_table_name
176
-            main_datas = {'class_id':target_cls._leclass._class_id, 'type_id':target_cls._type_id}
177
-            for main_column_name in common_fields:
178
-                if main_column_name in datas:
179
-                    main_datas[main_column_name] = datas[main_column_name]
180
-                    unset(datas[main_column_name])
181
-
182
-            cur = self.datasource_utils.query(self.connection, insert(main_table, main_datas))
183
-            lodel_id = cur.lastrowid
184
-
185
-            # insert in class_table
186
-            datas[self.datasource_utils.field_lodel_id] = lodel_id
187
-            class_table = self.datasource_utils.get_table_name_from_class(target_cls._leclass.__name__)
188
-            self.datasource_utils.query(self.connection, insert(class_table, datas))
173
+        if not target_cls.is_letype():
174
+            raise AttributeError("'%s' is not a LeType, it is not possible to insert it" % target_cls)
175
+
176
+        # find main table and main table datas
177
+        main_table = self.datasource_utils.objects_table_name
178
+        main_datas = {'class_id':target_cls._leclass._class_id, 'type_id':target_cls._type_id}
179
+        for main_column_name in common_fields:
180
+            if main_column_name in datas:
181
+                main_datas[main_column_name] = datas[main_column_name]
182
+                unset(datas[main_column_name])
183
+
184
+        cur = self.datasource_utils.query(self.connection, insert(main_table, main_datas))
185
+        lodel_id = cur.lastrowid
186
+
187
+        # insert in class_table
188
+        datas[self.datasource_utils.field_lodel_id] = lodel_id
189
+        class_table = self.datasource_utils.get_table_name_from_class(target_cls._leclass.__name__)
190
+        self.datasource_utils.query(self.connection, insert(class_table, datas))
189 191
 
190 192
         return lodel_id
191 193
 

+ 13
- 2
leapi/lecrud.py Ver arquivo

@@ -87,8 +87,19 @@ class _LeCrud(object):
87 87
         if len(cls._uid_fieldtype) == 0:
88 88
             raise NotImplementedError("Abstract method uid_name for %s!"%cls.__name__)
89 89
         return list(cls._uid_fieldtype.keys())[0]
90
-        
91
-    
90
+
91
+    ## @return maybe Bool: True if cls is a LeType or an instance of LeType
92
+    # @param cls Class: a Class or instanciated object
93
+    @classmethod
94
+    def is_letype(cls):
95
+        return hasattr(cls, '_leclass')
96
+
97
+    ## @return maybe Bool: True if cls is a LeClass or an instance of LeClass
98
+    # @param cls Class: a Class or instanciated object
99
+    @classmethod
100
+    def is_leclass(cls):
101
+        return hasattr(cls, '_class_id') and not cls.is_letype()
102
+
92 103
     ## @brief Returns object datas
93 104
     # @param
94 105
     # @return a dict of fieldname : value

Carregando…
Cancelar
Salvar