From 0c913dacdcbdba84396a2cb9f1ee096fb9fbb46c Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Wed, 20 Oct 2021 17:41:38 +0200 Subject: [PATCH] Fix: ordre des partitions --- app/scodoc/sco_groups.py | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/app/scodoc/sco_groups.py b/app/scodoc/sco_groups.py index 7a31b995..120db6a4 100644 --- a/app/scodoc/sco_groups.py +++ b/app/scodoc/sco_groups.py @@ -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)