1
0
forked from ScoDoc/ScoDoc

Fix change group/import etud admission

This commit is contained in:
Emmanuel Viennet 2023-07-08 13:57:44 +02:00
parent 41a791282a
commit d88e41b83e
4 changed files with 32 additions and 19 deletions

View File

@ -119,11 +119,12 @@ class Partition(db.Model):
.first() .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. """Affect etudid to group_id in given partition.
Raises IntegrityError si conflit, Raises IntegrityError si conflit,
or ValueError si ce group_id n'est pas dans cette partition or ValueError si ce group_id n'est pas dans cette partition
ou que l'étudiant n'est pas inscrit au semestre. 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): if not group.id in (g.id for g in self.groups):
raise ScoValueError( raise ScoValueError(
@ -141,8 +142,19 @@ class Partition(db.Model):
.filter_by(partition_id=self.id) .filter_by(partition_id=self.id)
.first() .first()
) )
existing_group_id = existing_row[1]
if existing_row: 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: else:
new_row = group_membership.insert().values( new_row = group_membership.insert().values(
etudid=etudid, group_id=group.id etudid=etudid, group_id=group.id
@ -152,6 +164,7 @@ class Partition(db.Model):
except IntegrityError: except IntegrityError:
db.session.rollback() db.session.rollback()
raise raise
return True
class GroupDescr(db.Model): class GroupDescr(db.Model):

View File

@ -664,16 +664,17 @@ def set_group(etudid: int, group_id: int) -> bool: # OBSOLETE !
return True 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 """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}") if not group.partition.set_etud_group(etudid, group):
return # pas de changement
group.partition.set_etud_group(etudid, group)
# - log # - log
formsemestre: FormSemestre = group.partition.formsemestre formsemestre: FormSemestre = group.partition.formsemestre
log(f"change_etud_group_in_partition: etudid={etudid} group={group}")
Scolog.logdb( Scolog.logdb(
method="changeGroup", method="changeGroup",
etudid=etudid, etudid=etudid,

View File

@ -639,10 +639,10 @@ def scolars_import_admission(datafile, formsemestre_id=None, type_admission=None
fields = adm_get_fields(titles, formsemestre_id) fields = adm_get_fields(titles, formsemestre_id)
idx_nom = None idx_nom = None
idx_prenom = None idx_prenom = None
for idx in fields: for idx, field in fields.items():
if fields[idx][0] == "nom": if field[0] == "nom":
idx_nom = idx idx_nom = idx
if fields[idx][0] == "prenom": if field[0] == "prenom":
idx_prenom = idx idx_prenom = idx
if (idx_nom is None) or (idx_prenom is None): if (idx_nom is None) or (idx_prenom is None):
log("fields indices=" + ", ".join([str(x) for x in fields])) 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) # Retrouve l'étudiant parmi ceux du semestre par (nom, prenom)
nom = adm_normalize_string(line[idx_nom]) nom = adm_normalize_string(line[idx_nom])
prenom = adm_normalize_string(line[idx_prenom]) prenom = adm_normalize_string(line[idx_prenom])
if not (nom, prenom) in etuds_by_nomprenom: if (nom, prenom) not in etuds_by_nomprenom:
log( msg = f"""Étudiant <b>{line[idx_nom]} {line[idx_prenom]} inexistant</b>"""
"unable to find %s %s among members" % (line[idx_nom], line[idx_prenom]) diag.append(msg)
)
else: else:
etud = etuds_by_nomprenom[(nom, prenom)] etud = etuds_by_nomprenom[(nom, prenom)]
cur_adm = sco_etud.admission_list(cnx, args={"etudid": etud["etudid"]})[0] cur_adm = sco_etud.admission_list(cnx, args={"etudid": etud["etudid"]})[0]
# peuple les champs presents dans le tableau # peuple les champs presents dans le tableau
args = {} args = {}
for idx in fields: for idx, field in fields.items():
field_name, convertor = fields[idx] field_name, convertor = field
if field_name in modifiable_fields: if field_name in modifiable_fields:
try: try:
val = convertor(line[idx]) val = convertor(line[idx])
except ValueError: except ValueError as exc:
raise ScoFormatError( raise ScoFormatError(
'scolars_import_admission: valeur invalide, ligne %d colonne %s: "%s"' 'scolars_import_admission: valeur invalide, ligne %d colonne %s: "%s"'
% (nline, field_name, line[idx]), % (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, scodoc_dept=g.scodoc_dept,
formsemestre_id=formsemestre_id, formsemestre_id=formsemestre_id,
), ),
) ) from exc
if val is not None: # note: ne peut jamais supprimer une valeur if val is not None: # note: ne peut jamais supprimer une valeur
args[field_name] = val args[field_name] = val
if args: if args:

View File

@ -1,7 +1,7 @@
# -*- mode: python -*- # -*- mode: python -*-
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
SCOVERSION = "9.4.98" SCOVERSION = "9.4.99"
SCONAME = "ScoDoc" SCONAME = "ScoDoc"