Browse Source

Replacing EmComponent method getDbE by db_engine

Yann Weber 9 years ago
parent
commit
4fb2f789b5
4 changed files with 16 additions and 18 deletions
  1. 1
    1
      Database/sqlutils.py
  2. 7
    9
      EditorialModel/components.py
  3. 2
    2
      EditorialModel/test/test_field.py
  4. 6
    6
      EditorialModel/types.py

+ 1
- 1
Database/sqlutils.py View File

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

+ 7
- 9
EditorialModel/components.py View File

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

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

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

+ 6
- 6
EditorialModel/types.py View File

@@ -64,7 +64,7 @@ class EmType(EmComponent):
64 64
     def field_groups(self):
65 65
         fg_table = sqlutils.getTable(EmFieldGroup)
66 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 68
         res = conn.execute(req)
69 69
         rows = res.fetchall()
70 70
         conn.close()
@@ -132,7 +132,7 @@ class EmType(EmComponent):
132 132
     # @return sqlalchemy em_type_hierarchy table object
133 133
     # @todo Don't hardcode table name
134 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 137
     @property
138 138
     ## Return the EmClassType of the type
@@ -164,7 +164,7 @@ class EmType(EmComponent):
164 164
     # @throw RunTimeError if a nature fetched from db is not valid
165 165
     # @see EmType::subordinates(), EmType::superiors()
166 166
     def _subOrSup(self, sup = True):
167
-        conn = self.getDbE().connect()
167
+        conn = self.db_engine().connect()
168 168
         htable = self.__class__._tableHierarchy()
169 169
         req = htable.select()
170 170
         if sup:
@@ -212,7 +212,7 @@ class EmType(EmComponent):
212 212
         elif self.name != em_type.name:
213 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 216
         htable = self.__class__._tableHierarchy()
217 217
         values = { 'subordinate_id': self.uid, 'superior_id': em_type.uid, 'nature': relation_nature }
218 218
         req = htable.insert(values=values)
@@ -237,7 +237,7 @@ class EmType(EmComponent):
237 237
         if relation_nature not in EmClassType.natures(self.classtype['name']):
238 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 241
         htable = self.__class__._tableHierarchy()
242 242
         req = htable.delete(htable.c.superior_id == em_type.uid and htable.c.nature == relation_nature)
243 243
         conn.execute(req)
@@ -253,7 +253,7 @@ class EmType(EmComponent):
253 253
     ## @brief Return the list of all the types linked to this type, should they be superiors or subordinates
254 254
     # @return A list of EmType objects
255 255
     def _linked_types_Db(self):
256
-        conn = self.getDbE().connect()
256
+        conn = self.db_engine().connect()
257 257
         htable = self.__class__._tableHierarchy()
258 258
         req = htable.select(htable.c.superior_id, htable.c.subordinate_id)
259 259
         req = req.where(sql.or_(htable.c.subordinate_id == self.uid, htable.c.superior_id == self.uid))

Loading…
Cancel
Save