Browse Source

Replacing EmComponent method getDbE by db_engine

Yann Weber 9 years ago
parent
commit
4fb2f789b5

+ 1
- 1
Database/sqlutils.py View File

88
     from EditorialModel.components import EmComponent #dirty circula inclusion hack
88
     from EditorialModel.components import EmComponent #dirty circula inclusion hack
89
     if not issubclass(cls, EmComponent) or cls.table == None:
89
     if not issubclass(cls, EmComponent) or cls.table == None:
90
         raise TypeError("Excepting an EmComponent child class not an "+str(cls))
90
         raise TypeError("Excepting an EmComponent child class not an "+str(cls))
91
-    engine = cls.getDbE()
91
+    engine = cls.db_engine()
92
     return sqla.Table(cls.table, meta(engine))
92
     return sqla.Table(cls.table, meta(engine))
93
 
93
 
94
 ## This function is intended to execute ddl defined in sqlalter
94
 ## This function is intended to execute ddl defined in sqlalter

+ 7
- 9
EditorialModel/components.py View File

123
     ## Shortcut that return the sqlAlchemy engine
123
     ## Shortcut that return the sqlAlchemy engine
124
     def db_engine(cls):
124
     def db_engine(cls):
125
         return sqlutils.getEngine(cls.dbconf)
125
         return sqlutils.getEngine(cls.dbconf)
126
-    @classmethod
127
-    def getDbE(cls): return cls.db_engine();
128
 
126
 
129
     ## Do the query on the database for EmComponent::populate()
127
     ## Do the query on the database for EmComponent::populate()
130
     # @throw EmComponentNotExistError if the instance is not anymore stored in database
128
     # @throw EmComponentNotExistError if the instance is not anymore stored in database
131
     def _populate_db(self):
129
     def _populate_db(self):
132
-        dbe = self.__class__.getDbE()
130
+        dbe = self.__class__.db_engine()
133
         component = sql.Table(self.table, sqlutils.meta(dbe))
131
         component = sql.Table(self.table, sqlutils.meta(dbe))
134
         req = sql.sql.select([component])
132
         req = sql.sql.select([component])
135
 
133
 
174
         kwargs['uid'] = cls.new_uid()
172
         kwargs['uid'] = cls.new_uid()
175
         kwargs['date_update'] = kwargs['date_create'] = datetime.datetime.utcnow()
173
         kwargs['date_update'] = kwargs['date_create'] = datetime.datetime.utcnow()
176
 
174
 
177
-        dbe = cls.getDbE()
175
+        dbe = cls.db_engine()
178
         conn = dbe.connect()
176
         conn = dbe.connect()
179
 
177
 
180
         kwargs['rank'] = -1  # Warning !!!
178
         kwargs['rank'] = -1  # Warning !!!
207
     # @throw RunTimeError if it was unable to do the Db update
205
     # @throw RunTimeError if it was unable to do the Db update
208
     def _save_db(self, values):
206
     def _save_db(self, values):
209
         """ Do the query on the db """
207
         """ Do the query on the db """
210
-        dbe = self.__class__.getDbE()
208
+        dbe = self.__class__.db_engine()
211
         component = sql.Table(self.table, sqlutils.meta(dbe))
209
         component = sql.Table(self.table, sqlutils.meta(dbe))
212
         req = sql.update(component, values=values).where(component.c.uid == self.uid)
210
         req = sql.update(component, values=values).where(component.c.uid == self.uid)
213
 
211
 
223
     # @throw RunTimeError if it was unable to do the deletion
221
     # @throw RunTimeError if it was unable to do the deletion
224
     def delete(self):
222
     def delete(self):
225
         #<SQL>
223
         #<SQL>
226
-        dbe = self.__class__.getDbE()
224
+        dbe = self.__class__.db_engine()
227
         component = sql.Table(self.table, sqlutils.meta(dbe))
225
         component = sql.Table(self.table, sqlutils.meta(dbe))
228
         req = component.delete().where(component.c.uid == self.uid)
226
         req = component.delete().where(component.c.uid == self.uid)
229
         conn = dbe.connect()
227
         conn = dbe.connect()
240
     # Retourne le rank le plus élevé pour le groupe de component au quel apartient l'objet actuelle
238
     # Retourne le rank le plus élevé pour le groupe de component au quel apartient l'objet actuelle
241
     #return int
239
     #return int
242
     def get_max_rank(self):
240
     def get_max_rank(self):
243
-        dbe = self.__class__.getDbE()
241
+        dbe = self.__class__.db_engine()
244
         component = sql.Table(self.table, sqlutils.meta(dbe))
242
         component = sql.Table(self.table, sqlutils.meta(dbe))
245
         req = sql.sql.select([component.c.rank]).where(getattr(component.c, self.ranked_in) == getattr(self, self.ranked_in)).order_by(component.c.rank.desc())
243
         req = sql.sql.select([component.c.rank]).where(getattr(component.c, self.ranked_in) == getattr(self, self.ranked_in)).order_by(component.c.rank.desc())
246
         conn = dbe.connect()
244
         conn = dbe.connect()
269
         
267
         
270
         if isinstance(new_rank, int):
268
         if isinstance(new_rank, int):
271
             if (new_rank >= 0):
269
             if (new_rank >= 0):
272
-                dbe = self.__class__.getDbE()
270
+                dbe = self.__class__.db_engine()
273
                 component = sql.Table(self.table, sqlutils.meta(dbe))
271
                 component = sql.Table(self.table, sqlutils.meta(dbe))
274
                 req = sql.sql.select([component.c.uid, component.c.rank])
272
                 req = sql.sql.select([component.c.uid, component.c.rank])
275
 
273
 
401
         if cls.table is None:
399
         if cls.table is None:
402
             raise NotImplementedError("Abstract method")
400
             raise NotImplementedError("Abstract method")
403
 
401
 
404
-        dbe = cls.getDbE()
402
+        dbe = cls.db_engine()
405
 
403
 
406
         uidtable = sql.Table('uids', sqlutils.meta(dbe))
404
         uidtable = sql.Table('uids', sqlutils.meta(dbe))
407
         conn = dbe.connect()
405
         conn = dbe.connect()

+ 2
- 2
EditorialModel/test/test_field.py View File

79
     # @param field EmField: EmField object
79
     # @param field EmField: EmField object
80
     # @return Number of found records
80
     # @return Number of found records
81
     def _get_field_records_Db(self,field):
81
     def _get_field_records_Db(self,field):
82
-        dbe = EmComponent.getDbE()
82
+        dbe = EmComponent.db_engine()
83
         fieldtable = sqla.Table(EmField.table, sqlutils.meta(dbe))
83
         fieldtable = sqla.Table(EmField.table, sqlutils.meta(dbe))
84
         conn = dbe.connect()
84
         conn = dbe.connect()
85
         req = fieldtable.select().where(fieldtable.c.uid==field.uid).where(fieldtable.c.name==field.name)
85
         req = fieldtable.select().where(fieldtable.c.uid==field.uid).where(fieldtable.c.name==field.name)
103
     # @param table_name str: Name of the table
103
     # @param table_name str: Name of the table
104
     # @return list of columns
104
     # @return list of columns
105
     def _get_table_columns_Db(self, table_name):
105
     def _get_table_columns_Db(self, table_name):
106
-        table = sqla.Table(table_name, sqlutils.meta(EmComponent.getDbE()))
106
+        table = sqla.Table(table_name, sqlutils.meta(EmComponent.db_engine()))
107
         return table.c
107
         return table.c
108
 
108
 
109
 ## TestField (Class)
109
 ## TestField (Class)

+ 6
- 6
EditorialModel/types.py View File

64
     def field_groups(self):
64
     def field_groups(self):
65
         fg_table = sqlutils.getTable(EmFieldGroup)
65
         fg_table = sqlutils.getTable(EmFieldGroup)
66
         req = fg_table.select(fg_table.c.uid).where(fg_table.c.class_id == self.class_id)
66
         req = fg_table.select(fg_table.c.uid).where(fg_table.c.class_id == self.class_id)
67
-        conn = self.__class__.getDbE().connect()
67
+        conn = self.__class__.db_engine().connect()
68
         res = conn.execute(req)
68
         res = conn.execute(req)
69
         rows = res.fetchall()
69
         rows = res.fetchall()
70
         conn.close()
70
         conn.close()
132
     # @return sqlalchemy em_type_hierarchy table object
132
     # @return sqlalchemy em_type_hierarchy table object
133
     # @todo Don't hardcode table name
133
     # @todo Don't hardcode table name
134
     def _tableHierarchy(cl):
134
     def _tableHierarchy(cl):
135
-        return sql.Table('em_type_hierarchy', sqlutils.meta(cl.getDbE()))
135
+        return sql.Table('em_type_hierarchy', sqlutils.meta(cl.db_engine()))
136
 
136
 
137
     @property
137
     @property
138
     ## Return the EmClassType of the type
138
     ## Return the EmClassType of the type
164
     # @throw RunTimeError if a nature fetched from db is not valid
164
     # @throw RunTimeError if a nature fetched from db is not valid
165
     # @see EmType::subordinates(), EmType::superiors()
165
     # @see EmType::subordinates(), EmType::superiors()
166
     def _subOrSup(self, sup = True):
166
     def _subOrSup(self, sup = True):
167
-        conn = self.getDbE().connect()
167
+        conn = self.db_engine().connect()
168
         htable = self.__class__._tableHierarchy()
168
         htable = self.__class__._tableHierarchy()
169
         req = htable.select()
169
         req = htable.select()
170
         if sup:
170
         if sup:
212
         elif self.name != em_type.name:
212
         elif self.name != em_type.name:
213
             raise ValueError("Not allowed to put a different em_type as superior in a relation of nature '"+relation_nature+"'")
213
             raise ValueError("Not allowed to put a different em_type as superior in a relation of nature '"+relation_nature+"'")
214
 
214
 
215
-        conn = self.getDbE().connect()
215
+        conn = self.db_engine().connect()
216
         htable = self.__class__._tableHierarchy()
216
         htable = self.__class__._tableHierarchy()
217
         values = { 'subordinate_id': self.uid, 'superior_id': em_type.uid, 'nature': relation_nature }
217
         values = { 'subordinate_id': self.uid, 'superior_id': em_type.uid, 'nature': relation_nature }
218
         req = htable.insert(values=values)
218
         req = htable.insert(values=values)
237
         if relation_nature not in EmClassType.natures(self.classtype['name']):
237
         if relation_nature not in EmClassType.natures(self.classtype['name']):
238
             raise ValueError("Invalid nature for add_superior : '"+relation_nature+"'. Allowed relations for this type are "+str(EmClassType.natures(self.classtype['name'])))
238
             raise ValueError("Invalid nature for add_superior : '"+relation_nature+"'. Allowed relations for this type are "+str(EmClassType.natures(self.classtype['name'])))
239
 
239
 
240
-        conn = self.getDbE().connect()
240
+        conn = self.db_engine().connect()
241
         htable = self.__class__._tableHierarchy()
241
         htable = self.__class__._tableHierarchy()
242
         req = htable.delete(htable.c.superior_id == em_type.uid and htable.c.nature == relation_nature)
242
         req = htable.delete(htable.c.superior_id == em_type.uid and htable.c.nature == relation_nature)
243
         conn.execute(req)
243
         conn.execute(req)
253
     ## @brief Return the list of all the types linked to this type, should they be superiors or subordinates
253
     ## @brief Return the list of all the types linked to this type, should they be superiors or subordinates
254
     # @return A list of EmType objects
254
     # @return A list of EmType objects
255
     def _linked_types_Db(self):
255
     def _linked_types_Db(self):
256
-        conn = self.getDbE().connect()
256
+        conn = self.db_engine().connect()
257
         htable = self.__class__._tableHierarchy()
257
         htable = self.__class__._tableHierarchy()
258
         req = htable.select(htable.c.superior_id, htable.c.subordinate_id)
258
         req = htable.select(htable.c.superior_id, htable.c.subordinate_id)
259
         req = req.where(sql.or_(htable.c.subordinate_id == self.uid, htable.c.superior_id == self.uid))
259
         req = req.where(sql.or_(htable.c.subordinate_id == self.uid, htable.c.superior_id == self.uid))

Loading…
Cancel
Save