diff --git a/app/models/formations.py b/app/models/formations.py index e2eadf460..39ad45083 100644 --- a/app/models/formations.py +++ b/app/models/formations.py @@ -63,7 +63,7 @@ class Formation(db.Model): "titre complet pour affichage" return f"""Formation {self.titre} ({self.acronyme}) [version {self.version}] code {self.formation_code}""" - def to_dict(self, with_refcomp_attrs=False): + def to_dict(self, with_refcomp_attrs=False, with_departement=True): """As a dict. Si with_refcomp_attrs, ajoute attributs permettant de retrouver le ref. de comp. """ @@ -73,7 +73,8 @@ class Formation(db.Model): e.pop("referentiel_competence") e["code_specialite"] = e["code_specialite"] or "" e["commentaire"] = e["commentaire"] or "" - e["departement"] = self.departement.to_dict() + if with_departement and self.departement: + e["departement"] = self.departement.to_dict() e["formation_id"] = self.id # ScoDoc7 backward compat if with_refcomp_attrs and self.referentiel_competence: e["refcomp_version_orebut"] = self.referentiel_competence.version_orebut diff --git a/app/scodoc/sco_edit_formation.py b/app/scodoc/sco_edit_formation.py index 6209a2be4..11c31436c 100644 --- a/app/scodoc/sco_edit_formation.py +++ b/app/scodoc/sco_edit_formation.py @@ -30,6 +30,7 @@ """ import flask from flask import g, url_for, request +import sqlalchemy from app import db from app import log @@ -167,7 +168,9 @@ def formation_edit(formation_id=None, create=False): submitlabel = "Modifier les valeurs" H = [ html_sco_header.sco_header(page_title="Modification d'une formation"), - """

Modification de la formation %(acronyme)s

""" % initvalues, + f"""

Modification de la formation {formation.acronyme} + version {formation.version} +

""", ] if is_locked: H.append( @@ -260,15 +263,17 @@ def formation_edit(formation_id=None, create=False): "acronyme": tf[2]["acronyme"], "titre": tf[2]["titre"], "version": version, + "dept_id": g.scodoc_dept_id, } - ndb.quote_dict(args) - others = sco_formations.formation_list(args=args) - if others and ((len(others) > 1) or others[0]["formation_id"] != formation_id): + other_formations: list[Formation] = Formation.query.filter_by(**args).all() + if other_formations and ( + (len(other_formations) > 1) or other_formations[0].id != formation_id + ): return ( "\n".join(H) + tf_error_message( f"""Valeurs incorrectes: il existe déjà une formation avec même titre, acronyme et version. """ @@ -328,11 +333,24 @@ def do_formation_edit(args): del args["type_parcours"] for field in formation.__dict__: - if field and field[0] != "_" and field in args: - setattr(formation, field, args[field]) + if field in args: + value = args[field].strip() if isinstance(args[field], str) else args[field] + if field and field[0] != "_": + setattr(formation, field, value) db.session.add(formation) - db.session.commit() + try: + db.session.commit() + except sqlalchemy.exc.IntegrityError as exc: + db.session.rollback() + raise ScoValueError( + "On ne peut pas créer deux formations avec mêmes acronymes, titres et versions !", + dest_url=url_for( + "notes.formation_edit", + scodoc_dept=g.scodoc_dept, + formation_id=formation.id, + ), + ) from exc formation.invalidate_cached_sems() diff --git a/app/scodoc/sco_formations.py b/app/scodoc/sco_formations.py index a109cf5e9..b9a44d3a3 100644 --- a/app/scodoc/sco_formations.py +++ b/app/scodoc/sco_formations.py @@ -52,7 +52,6 @@ from app.scodoc import codes_cursus from app.scodoc import sco_edit_matiere from app.scodoc import sco_edit_module from app.scodoc import sco_edit_ue -from app.scodoc import sco_formations from app.scodoc import sco_formsemestre from app.scodoc import sco_preferences from app.scodoc import sco_tag_module @@ -93,7 +92,6 @@ def formation_list(formation_id=None, args={}): ### XXX obsolete, à supprimer args = {"formation_id": formation_id} cnx = ndb.GetDBConnexion() r = _formationEditor.list(cnx, args=args) - # log('%d formations found' % len(r)) return r