|
@@ -149,11 +149,81 @@ class EmComponent(object):
|
149
|
149
|
#</SQL>
|
150
|
150
|
pass
|
151
|
151
|
|
152
|
|
- """ change the rank of the component
|
153
|
|
- @param int new_rank new position
|
154
|
|
- """
|
155
|
|
- def modify_rank(self, new_rank):
|
156
|
|
- pass
|
|
152
|
+ ## modify_rank
|
|
153
|
+ #
|
|
154
|
+ # Permet de changer le rank d'un component, soit en lui donnant un rank précis, soit en augmentant ou reduisant sont rank actuelle d'une valleur donné.
|
|
155
|
+ #
|
|
156
|
+ # @param new_rank int: le rank ou modificateur de rank
|
|
157
|
+ # @param sign str: Un charactère qui peut être : '=' pour afecter un rank, '+' pour ajouter le modificateur de rank ou '-' pour soustraire le modificateur de rank.
|
|
158
|
+ #
|
|
159
|
+ # @return bool: True en cas de réussite False en cas d'echec.
|
|
160
|
+ def modify_rank(self, new_rank, sign):
|
|
161
|
+ if(type(new_rank) is int):
|
|
162
|
+ if(new_rank >= 0):
|
|
163
|
+ dbe = self.__class__.getDbE()
|
|
164
|
+ component = sql.Table(self.table, sqlutils.meta(dbe))
|
|
165
|
+ req = sql.sql.select([component.c.uid, component.c.rank])
|
|
166
|
+ if(sign == '='):
|
|
167
|
+ req = req.where(component.c.ranked_in == self.ranked_in and (component.c.rank >= new_rank or component.c.rank >= new_rank - 1))
|
|
168
|
+
|
|
169
|
+ c = dbe.connect()
|
|
170
|
+ res = c.execute(req)
|
|
171
|
+ res = res.fetchall()
|
|
172
|
+
|
|
173
|
+ vals = list()
|
|
174
|
+ vals.append({'id' : self.id, 'rank' : new_rank})
|
|
175
|
+
|
|
176
|
+ for row in res:
|
|
177
|
+ vals.append({'id' : row.uid, 'rank' : row.rank+1})
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+ req = component.update().where(component.c.uid == sql.bindparam('id')).values(rank = sql.bindparam('rank'))
|
|
181
|
+ c.execute(req, vals)
|
|
182
|
+ c.close()
|
|
183
|
+
|
|
184
|
+ elif(sign == '+'):
|
|
185
|
+ req = req.where(component.c.ranked_in == self.ranked_in and (component.c.rank <= self.rank + new_rank and component.c.rank > self.rank))
|
|
186
|
+
|
|
187
|
+ c = dbe.connect()
|
|
188
|
+ res = c.execute(req)
|
|
189
|
+ res = res.fetchall()
|
|
190
|
+
|
|
191
|
+ vals = list()
|
|
192
|
+ vals.append({'id' : self.id, 'rank' : self.rank + new_rank})
|
|
193
|
+
|
|
194
|
+ for row in res:
|
|
195
|
+ vals.append({'id' : row.uid, 'rank' : row.rank - 1})
|
|
196
|
+
|
|
197
|
+ req = component.update().where(component.c.uid == sql.bindparam('id')).values(rank = sql.bindparam('rank'))
|
|
198
|
+ c.execute(req, vals)
|
|
199
|
+ c.close()
|
|
200
|
+
|
|
201
|
+ elif(sign == '-'):
|
|
202
|
+ req = req.where(component.c.ranked_in == self.ranked_in and (component.c.rank >= self.rank - new_rank and component.c.rank < self.rank))
|
|
203
|
+
|
|
204
|
+ c = dbe.connect()
|
|
205
|
+ res = c.execute(req)
|
|
206
|
+ res = res.fetchall()
|
|
207
|
+
|
|
208
|
+ vals = list()
|
|
209
|
+ vals.append({'id' : self.id, 'rank' : self.rank - new_rank})
|
|
210
|
+
|
|
211
|
+ for row in res:
|
|
212
|
+ vals.append({'id' : row.uid, 'rank' : row.rank + 1})
|
|
213
|
+
|
|
214
|
+ req = component.update().where(component.c.uid == sql.bindparam('id')).values(rank = sql.bindparam('rank'))
|
|
215
|
+ c.execute(req, vals)
|
|
216
|
+ c.close()
|
|
217
|
+ else:
|
|
218
|
+ logger.error("Bad argument")
|
|
219
|
+ raise TypeError('Excepted a string (\'=\' or \'+\' or \'-\') not a '+str(type(new_rank)))
|
|
220
|
+ else:
|
|
221
|
+ logger.error("Bad argument")
|
|
222
|
+ raise ValueError('Excepted a positive int not a negative. new_rank = '+str((new_rank)))
|
|
223
|
+ else:
|
|
224
|
+ logger.error("Bad argument")
|
|
225
|
+ raise TypeError('Excepted a int not a '+str(type(new_rank)))
|
|
226
|
+
|
157
|
227
|
|
158
|
228
|
def __repr__(self):
|
159
|
229
|
if self.name is None:
|