edition formation: modernisation code

This commit is contained in:
Emmanuel Viennet 2023-02-20 16:25:23 +01:00
parent a694618a16
commit 09657f1ebb
3 changed files with 29 additions and 12 deletions

View File

@ -63,7 +63,7 @@ class Formation(db.Model):
"titre complet pour affichage" "titre complet pour affichage"
return f"""Formation {self.titre} ({self.acronyme}) [version {self.version}] code {self.formation_code}""" 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. """As a dict.
Si with_refcomp_attrs, ajoute attributs permettant de retrouver le ref. de comp. Si with_refcomp_attrs, ajoute attributs permettant de retrouver le ref. de comp.
""" """
@ -73,6 +73,7 @@ class Formation(db.Model):
e.pop("referentiel_competence") e.pop("referentiel_competence")
e["code_specialite"] = e["code_specialite"] or "" e["code_specialite"] = e["code_specialite"] or ""
e["commentaire"] = e["commentaire"] or "" e["commentaire"] = e["commentaire"] or ""
if with_departement and self.departement:
e["departement"] = self.departement.to_dict() e["departement"] = self.departement.to_dict()
e["formation_id"] = self.id # ScoDoc7 backward compat e["formation_id"] = self.id # ScoDoc7 backward compat
if with_refcomp_attrs and self.referentiel_competence: if with_refcomp_attrs and self.referentiel_competence:

View File

@ -30,6 +30,7 @@
""" """
import flask import flask
from flask import g, url_for, request from flask import g, url_for, request
import sqlalchemy
from app import db from app import db
from app import log from app import log
@ -167,7 +168,9 @@ def formation_edit(formation_id=None, create=False):
submitlabel = "Modifier les valeurs" submitlabel = "Modifier les valeurs"
H = [ H = [
html_sco_header.sco_header(page_title="Modification d'une formation"), html_sco_header.sco_header(page_title="Modification d'une formation"),
"""<h2>Modification de la formation %(acronyme)s</h2>""" % initvalues, f"""<h2>Modification de la formation {formation.acronyme}
version {formation.version}
</h2>""",
] ]
if is_locked: if is_locked:
H.append( H.append(
@ -260,15 +263,17 @@ def formation_edit(formation_id=None, create=False):
"acronyme": tf[2]["acronyme"], "acronyme": tf[2]["acronyme"],
"titre": tf[2]["titre"], "titre": tf[2]["titre"],
"version": version, "version": version,
"dept_id": g.scodoc_dept_id,
} }
ndb.quote_dict(args) other_formations: list[Formation] = Formation.query.filter_by(**args).all()
others = sco_formations.formation_list(args=args) if other_formations and (
if others and ((len(others) > 1) or others[0]["formation_id"] != formation_id): (len(other_formations) > 1) or other_formations[0].id != formation_id
):
return ( return (
"\n".join(H) "\n".join(H)
+ tf_error_message( + tf_error_message(
f"""Valeurs incorrectes: il existe déjà <a href="{ f"""Valeurs incorrectes: il existe déjà <a href="{
url_for('notes.ue_table', scodoc_dept=g.scodoc_dept, formation_id=others[0]["id"]) url_for('notes.ue_table', scodoc_dept=g.scodoc_dept, formation_id=other_formations[0].id)
}">une formation</a> avec même titre, }">une formation</a> avec même titre,
acronyme et version. acronyme et version.
""" """
@ -328,11 +333,24 @@ def do_formation_edit(args):
del args["type_parcours"] del args["type_parcours"]
for field in formation.__dict__: for field in formation.__dict__:
if field and field[0] != "_" and field in args: if field in args:
setattr(formation, field, args[field]) 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.add(formation)
try:
db.session.commit() 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() formation.invalidate_cached_sems()

View File

@ -52,7 +52,6 @@ from app.scodoc import codes_cursus
from app.scodoc import sco_edit_matiere from app.scodoc import sco_edit_matiere
from app.scodoc import sco_edit_module from app.scodoc import sco_edit_module
from app.scodoc import sco_edit_ue from app.scodoc import sco_edit_ue
from app.scodoc import sco_formations
from app.scodoc import sco_formsemestre from app.scodoc import sco_formsemestre
from app.scodoc import sco_preferences from app.scodoc import sco_preferences
from app.scodoc import sco_tag_module 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} args = {"formation_id": formation_id}
cnx = ndb.GetDBConnexion() cnx = ndb.GetDBConnexion()
r = _formationEditor.list(cnx, args=args) r = _formationEditor.list(cnx, args=args)
# log('%d formations found' % len(r))
return r return r