Browse Source

Component.new_rank() correction

En cas de signe égale prend désormais en charge une valeur de new_rank superieur ou inferieur a la valeur actuelle.
Driky 9 years ago
parent
commit
0bdf755be4
1 changed files with 31 additions and 16 deletions
  1. 31
    16
      EditorialModel/components.py

+ 31
- 16
EditorialModel/components.py View File

@@ -165,24 +165,39 @@ class EmComponent(object):
165 165
                 component = sql.Table(self.table, sqlutils.meta(dbe))
166 166
                 req = sql.sql.select([component.c.uid, component.c.rank])
167 167
                 if(sign == '='):
168
-                    req = req.where(getattr(component.c, self.ranked_in) == self.ranked_in and (component.c.rank >= new_rank or component.c.rank >= new_rank - 1))
169
-
168
+                    req = req.where(getattr(component.c, self.ranked_in) == self.ranked_in and (component.c.rank == new_rank - 1))
170 169
                     c = dbe.connect()
171 170
                     res = c.execute(req)
172
-                    res = res.fetchall()
173
-
174
-                    vals = list()
175
-                    vals.append({'id' : self.id, 'rank' : new_rank})
176
-
177
-                    for row in res:
178
-                        vals.append({'id' : row.uid, 'rank' : row.rank+1})
179
-
180
-
181
-                    req = component.update().where(component.c.uid == sql.bindparam('id')).values(rank = sql.bindparam('rank'))
182
-                    c.execute(req, vals)
183
-                    c.close()
184
-
185
-                    self.rank = new_rank
171
+                    res = res.fetchone()
172
+                    if(res):
173
+                        if(new_rank < self.rank):
174
+                            req = req.where(getattr(component.c, self.ranked_in) == self.ranked_in and (component.c.rank >= new_rank))
175
+                        else:
176
+                            req = req.where(getattr(component.c, self.ranked_in) == self.ranked_in and (component.c.rank <= new_rank ))
177
+
178
+                        c = dbe.connect()
179
+                        res = c.execute(req)
180
+                        res = res.fetchall()
181
+
182
+                        vals = list()
183
+                        vals.append({'id' : self.id, 'rank' : new_rank})
184
+
185
+                        if(new_rank < self.rank):
186
+                            for row in res:
187
+                                vals.append({'id' : row.uid, 'rank' : row.rank+1})
188
+                        else:
189
+                            for row in res:
190
+                                vals.append({'id' : row.uid, 'rank' : row.rank-1})
191
+
192
+
193
+                        req = component.update().where(component.c.uid == sql.bindparam('id')).values(rank = sql.bindparam('rank'))
194
+                        c.execute(req, vals)
195
+                        c.close()
196
+
197
+                        self.rank = new_rank
198
+                    else:
199
+                        logger.error("Bad argument")
200
+                        raise ValueError('new_rank to big, new_rank - 1 doesn\'t exist. new_rank = '+str((new_rank)))
186 201
                 elif(sign == '+'):
187 202
                     req = req.where(getattr(component.c, self.ranked_in) == self.ranked_in and (component.c.rank <= self.rank + new_rank and component.c.rank > self.rank))
188 203
 

Loading…
Cancel
Save