|
@@ -53,7 +53,14 @@ class EmComponent(object):
|
53
|
53
|
# @param id_or_name int|str: name or id of the object
|
54
|
54
|
# @throw TypeError if id_or_name is not an integer nor a string
|
55
|
55
|
# @throw NotImplementedError if called with EmComponent
|
56
|
|
- def __init__(self, id_or_name):
|
|
56
|
+ def __init__(self, id_or_name, dbconf = 'default'):
|
|
57
|
+
|
|
58
|
+ self.dbconf = dbconf
|
|
59
|
+ if self.dbconf:
|
|
60
|
+ self.db_engine = sqlutils.get_engine(dbconf)
|
|
61
|
+ else:
|
|
62
|
+ self.db_engine = False
|
|
63
|
+
|
57
|
64
|
if type(self) == EmComponent:
|
58
|
65
|
raise NotImplementedError('Abstract class')
|
59
|
66
|
|
|
@@ -129,15 +136,15 @@ class EmComponent(object):
|
129
|
136
|
|
130
|
137
|
super(EmComponent, self).__setattr__('deleted', False)
|
131
|
138
|
|
132
|
|
- @classmethod
|
|
139
|
+ #@classmethod
|
133
|
140
|
## Shortcut that return the sqlAlchemy engine
|
134
|
|
- def db_engine(cls):
|
135
|
|
- return sqlutils.get_engine(cls.dbconf)
|
|
141
|
+ #def db_engine(cls):
|
|
142
|
+ # return sqlutils.get_engine(cls.dbconf)
|
136
|
143
|
|
137
|
144
|
## Do the query on the database for EmComponent::populate()
|
138
|
145
|
# @throw EmComponentNotExistError if the instance is not anymore stored in database
|
139
|
146
|
def _populate_db(self):
|
140
|
|
- dbe = self.__class__.db_engine()
|
|
147
|
+ dbe = self.db_engine
|
141
|
148
|
component = sql.Table(self.table, sqlutils.meta(dbe))
|
142
|
149
|
req = sql.sql.select([component])
|
143
|
150
|
|
|
@@ -159,7 +166,7 @@ class EmComponent(object):
|
159
|
166
|
## Insert a new component in the database
|
160
|
167
|
#
|
161
|
168
|
# This function create and assign a new UID and handle the date_create and date_update values
|
162
|
|
- #
|
|
169
|
+ # @warning There is a mandatory argument dbconf that indicate wich database configuration to use
|
163
|
170
|
# @param **kwargs : Names arguments representing object properties
|
164
|
171
|
# @return An instance of the created component
|
165
|
172
|
# @throw TypeError if an element of kwargs isn't a valid object propertie or if a mandatory argument is missing
|
|
@@ -194,21 +201,27 @@ class EmComponent(object):
|
194
|
201
|
# if cls._fields[name].notNull and cls._fields[name].default == None:
|
195
|
202
|
# raise TypeError("Missing argument : "+name)
|
196
|
203
|
|
|
204
|
+ if 'dbconf' in kwargs:
|
|
205
|
+ if not kwargs['db_engine']:
|
|
206
|
+ raise NotImplementedError("Its a nonsense to call create with no database")
|
|
207
|
+ dbconf = kwargs['dbconf']
|
|
208
|
+ else:
|
|
209
|
+ dbconf = 'default'
|
|
210
|
+ dbe = sqlutils.get_engine(dbconf)
|
197
|
211
|
|
198
|
|
- kwargs['uid'] = cls.new_uid()
|
|
212
|
+ kwargs['uid'] = cls.new_uid(dbe)
|
199
|
213
|
kwargs['date_update'] = kwargs['date_create'] = datetime.datetime.utcnow()
|
200
|
214
|
|
201
|
|
- dbe = cls.db_engine()
|
202
|
215
|
conn = dbe.connect()
|
203
|
216
|
|
204
|
|
- kwargs['rank'] = cls.get_max_rank( kwargs[cls.ranked_in] )+1
|
|
217
|
+ kwargs['rank'] = cls._get_max_rank( kwargs[cls.ranked_in], dbe )+1
|
205
|
218
|
|
206
|
219
|
table = sql.Table(cls.table, sqlutils.meta(dbe))
|
207
|
220
|
req = table.insert(kwargs)
|
208
|
221
|
if not conn.execute(req):
|
209
|
222
|
raise RuntimeError("Unable to create the "+cls.__class__.__name__+" EmComponent ")
|
210
|
223
|
conn.close()
|
211
|
|
- return cls(kwargs['name'])
|
|
224
|
+ return cls(kwargs['name'], dbconf)
|
212
|
225
|
|
213
|
226
|
## Write the representation of the component in the database
|
214
|
227
|
# @return bool
|
|
@@ -232,7 +245,7 @@ class EmComponent(object):
|
232
|
245
|
# @throw RunTimeError if it was unable to do the Db update
|
233
|
246
|
def _save_db(self, values):
|
234
|
247
|
""" Do the query on the db """
|
235
|
|
- dbe = self.__class__.db_engine()
|
|
248
|
+ dbe = self.db_engine
|
236
|
249
|
component = sql.Table(self.table, sqlutils.meta(dbe))
|
237
|
250
|
req = sql.update(component, values=values).where(component.c.uid == self.uid)
|
238
|
251
|
|
|
@@ -247,7 +260,7 @@ class EmComponent(object):
|
247
|
260
|
# @throw RunTimeError if it was unable to do the deletion
|
248
|
261
|
def delete(self):
|
249
|
262
|
#<SQL>
|
250
|
|
- dbe = self.__class__.db_engine()
|
|
263
|
+ dbe = self.db_engine
|
251
|
264
|
component = sql.Table(self.table, sqlutils.meta(dbe))
|
252
|
265
|
req = component.delete().where(component.c.uid == self.uid)
|
253
|
266
|
conn = dbe.connect()
|
|
@@ -260,13 +273,11 @@ class EmComponent(object):
|
260
|
273
|
super(EmComponent, self).__setattr__('deleted', True)
|
261
|
274
|
return True
|
262
|
275
|
|
263
|
|
- ## get_max_rank
|
264
|
|
- # Retourne le rank le plus élevé pour le groupe de component au quel apartient l'objet actuelle
|
|
276
|
+ ## @brief Get the maximum rank given an EmComponent child class and a ranked_in filter
|
265
|
277
|
# @param ranked_in_value mixed: The rank "family"
|
266
|
|
- # @param return -1 if no EmComponent found else return an integer >= 0
|
|
278
|
+ # @return -1 if no EmComponent found else return an integer >= 0
|
267
|
279
|
@classmethod
|
268
|
|
- def get_max_rank(cls, ranked_in_value):
|
269
|
|
- dbe = cls.db_engine()
|
|
280
|
+ def _get_max_rank(cls, ranked_in_value, dbe):
|
270
|
281
|
component = sql.Table(cls.table, sqlutils.meta(dbe))
|
271
|
282
|
req = sql.sql.select([component.c.rank]).where(getattr(component.c, cls.ranked_in) == ranked_in_value).order_by(component.c.rank.desc())
|
272
|
283
|
c = dbe.connect()
|
|
@@ -278,6 +289,12 @@ class EmComponent(object):
|
278
|
289
|
else:
|
279
|
290
|
return -1
|
280
|
291
|
|
|
292
|
+ ## Only make a call to the class method
|
|
293
|
+ # @return A positive integer or -1 if no components
|
|
294
|
+ # @see EmComponent::_get_max_rank()
|
|
295
|
+ def get_max_rank(self, ranked_in_value):
|
|
296
|
+ return self.__class__._get_max_rank(ranked_in_value, self.db_engine)
|
|
297
|
+
|
281
|
298
|
## Set a new rank for this component
|
282
|
299
|
# @note This function assume that ranks are properly set from 1 to x with no gap
|
283
|
300
|
# @param new_rank int: The new rank
|
|
@@ -298,9 +315,9 @@ class EmComponent(object):
|
298
|
315
|
limits = [ self.rank + ( 1 if mod > 0 else -1), new_rank ] #The range of modified ranks
|
299
|
316
|
limits.sort()
|
300
|
317
|
|
301
|
|
- dbe = self.db_engine()
|
|
318
|
+ dbe = self.db_engine
|
302
|
319
|
conn = dbe.connect()
|
303
|
|
- table = sqlutils.get_table(self.__class__)
|
|
320
|
+ table = sqlutils.get_table(self)
|
304
|
321
|
|
305
|
322
|
#Selecting the components that will be modified
|
306
|
323
|
req = table.select().where( getattr(table.c, self.ranked_in) == getattr(self, self.ranked_in)).where(table.c.rank >= limits[0]).where(table.c.rank <= limits[1])
|
|
@@ -358,11 +375,11 @@ class EmComponent(object):
|
358
|
375
|
#
|
359
|
376
|
# Use the class property table
|
360
|
377
|
# @return A new uid (an integer)
|
361
|
|
- def new_uid(cls):
|
|
378
|
+ def new_uid(cls, db_engine):
|
362
|
379
|
if cls.table is None:
|
363
|
380
|
raise NotImplementedError("Abstract method")
|
364
|
381
|
|
365
|
|
- dbe = cls.db_engine()
|
|
382
|
+ dbe = db_engine
|
366
|
383
|
|
367
|
384
|
uidtable = sql.Table('uids', sqlutils.meta(dbe))
|
368
|
385
|
conn = dbe.connect()
|