From d88e41b83efa936088de3d65d2581e8f778edabc Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Sat, 8 Jul 2023 13:57:44 +0200 Subject: [PATCH] Fix change group/import etud admission --- app/models/groups.py | 17 +++++++++++++++-- app/scodoc/sco_groups.py | 11 ++++++----- app/scodoc/sco_import_etuds.py | 21 ++++++++++----------- sco_version.py | 2 +- 4 files changed, 32 insertions(+), 19 deletions(-) diff --git a/app/models/groups.py b/app/models/groups.py index 35d22f8e9..dce377da8 100644 --- a/app/models/groups.py +++ b/app/models/groups.py @@ -119,11 +119,12 @@ class Partition(db.Model): .first() ) - def set_etud_group(self, etudid: int, group: "GroupDescr"): + def set_etud_group(self, etudid: int, group: "GroupDescr") -> bool: """Affect etudid to group_id in given partition. Raises IntegrityError si conflit, or ValueError si ce group_id n'est pas dans cette partition ou que l'étudiant n'est pas inscrit au semestre. + Return True si changement, False s'il était déjà dans ce groupe. """ if not group.id in (g.id for g in self.groups): raise ScoValueError( @@ -141,8 +142,19 @@ class Partition(db.Model): .filter_by(partition_id=self.id) .first() ) + existing_group_id = existing_row[1] if existing_row: - existing_row.update({"group_id": group.id}) + if group.id == existing_group_id: + return False + update_row = ( + group_membership.update() + .where( + group_membership.c.etudid == etudid, + group_membership.c.group_id == existing_group_id, + ) + .values(group_id=group.id) + ) + db.session.execute(update_row) else: new_row = group_membership.insert().values( etudid=etudid, group_id=group.id @@ -152,6 +164,7 @@ class Partition(db.Model): except IntegrityError: db.session.rollback() raise + return True class GroupDescr(db.Model): diff --git a/app/scodoc/sco_groups.py b/app/scodoc/sco_groups.py index b02fd3379..8438ba09c 100644 --- a/app/scodoc/sco_groups.py +++ b/app/scodoc/sco_groups.py @@ -664,16 +664,17 @@ def set_group(etudid: int, group_id: int) -> bool: # OBSOLETE ! return True -def change_etud_group_in_partition(etudid: int, group: GroupDescr): +def change_etud_group_in_partition(etudid: int, group: GroupDescr) -> bool: """Inscrit etud au groupe - (et le desinscrit d'autres groupes de cette partition.) + (et le désinscrit d'autres groupes de cette partition) + Return True si changement, False s'il était déjà dans ce groupe. """ - log(f"change_etud_group_in_partition: etudid={etudid} group={group}") - - group.partition.set_etud_group(etudid, group) + if not group.partition.set_etud_group(etudid, group): + return # pas de changement # - log formsemestre: FormSemestre = group.partition.formsemestre + log(f"change_etud_group_in_partition: etudid={etudid} group={group}") Scolog.logdb( method="changeGroup", etudid=etudid, diff --git a/app/scodoc/sco_import_etuds.py b/app/scodoc/sco_import_etuds.py index 5bf61fdae..3141c412f 100644 --- a/app/scodoc/sco_import_etuds.py +++ b/app/scodoc/sco_import_etuds.py @@ -639,10 +639,10 @@ def scolars_import_admission(datafile, formsemestre_id=None, type_admission=None fields = adm_get_fields(titles, formsemestre_id) idx_nom = None idx_prenom = None - for idx in fields: - if fields[idx][0] == "nom": + for idx, field in fields.items(): + if field[0] == "nom": idx_nom = idx - if fields[idx][0] == "prenom": + if field[0] == "prenom": idx_prenom = idx if (idx_nom is None) or (idx_prenom is None): log("fields indices=" + ", ".join([str(x) for x in fields])) @@ -664,21 +664,20 @@ def scolars_import_admission(datafile, formsemestre_id=None, type_admission=None # Retrouve l'étudiant parmi ceux du semestre par (nom, prenom) nom = adm_normalize_string(line[idx_nom]) prenom = adm_normalize_string(line[idx_prenom]) - if not (nom, prenom) in etuds_by_nomprenom: - log( - "unable to find %s %s among members" % (line[idx_nom], line[idx_prenom]) - ) + if (nom, prenom) not in etuds_by_nomprenom: + msg = f"""Étudiant {line[idx_nom]} {line[idx_prenom]} inexistant""" + diag.append(msg) else: etud = etuds_by_nomprenom[(nom, prenom)] cur_adm = sco_etud.admission_list(cnx, args={"etudid": etud["etudid"]})[0] # peuple les champs presents dans le tableau args = {} - for idx in fields: - field_name, convertor = fields[idx] + for idx, field in fields.items(): + field_name, convertor = field if field_name in modifiable_fields: try: val = convertor(line[idx]) - except ValueError: + except ValueError as exc: raise ScoFormatError( 'scolars_import_admission: valeur invalide, ligne %d colonne %s: "%s"' % (nline, field_name, line[idx]), @@ -687,7 +686,7 @@ def scolars_import_admission(datafile, formsemestre_id=None, type_admission=None scodoc_dept=g.scodoc_dept, formsemestre_id=formsemestre_id, ), - ) + ) from exc if val is not None: # note: ne peut jamais supprimer une valeur args[field_name] = val if args: diff --git a/sco_version.py b/sco_version.py index 95fb08eb1..6ef51420b 100644 --- a/sco_version.py +++ b/sco_version.py @@ -1,7 +1,7 @@ # -*- mode: python -*- # -*- coding: utf-8 -*- -SCOVERSION = "9.4.98" +SCOVERSION = "9.4.99" SCONAME = "ScoDoc"