Fix: ordre des partitions

This commit is contained in:
Emmanuel Viennet 2021-10-20 17:41:38 +02:00
parent 66dbec86bf
commit 0c913dacdc

View File

@ -806,8 +806,21 @@ def partition_create(
)
cnx = ndb.GetDBConnexion()
if numero is None:
numero = (
ndb.SimpleQuery(
"SELECT MAX(id) FROM partition WHERE formsemestre_id=%(formsemestre_id)s",
{"formsemestre_id": formsemestre_id},
).fetchone()[0]
or 0
)
partition_id = partitionEditor.create(
cnx, {"formsemestre_id": formsemestre_id, "partition_name": partition_name}
cnx,
{
"formsemestre_id": formsemestre_id,
"partition_name": partition_name,
"numero": numero,
},
)
log("createPartition: created partition_id=%s" % partition_id)
#
@ -1041,7 +1054,7 @@ def partition_move(partition_id, after=0, redirect=1):
others = get_partitions_list(formsemestre_id)
if len(others) > 1:
pidx = [p["partition_id"] for p in others].index(partition_id)
log("partition_move: after=%s pidx=%s" % (after, pidx))
# log("partition_move: after=%s pidx=%s" % (after, pidx))
neigh = None # partition to swap with
if after == 0 and pidx > 0:
neigh = others[pidx - 1]
@ -1049,8 +1062,20 @@ def partition_move(partition_id, after=0, redirect=1):
neigh = others[pidx + 1]
if neigh: #
# swap numero between partition and its neighbor
log("moving partition %s" % partition_id)
# log("moving partition %s" % partition_id)
cnx = ndb.GetDBConnexion()
# Si aucun numéro n'a été affecté, le met au minimum
min_numero = (
ndb.SimpleQuery(
"SELECT MIN(numero) FROM partition WHERE formsemestre_id=%(formsemestre_id)s",
{"formsemestre_id": formsemestre_id},
).fetchone()[0]
or 0
)
if neigh["numero"] is None:
neigh["numero"] = min_numero - 1
if partition["numero"] is None:
partition["numero"] = min_numero - 1 - after
partition["numero"], neigh["numero"] = neigh["numero"], partition["numero"]
partitionEditor.edit(cnx, partition)
partitionEditor.edit(cnx, neigh)