diff --git a/app/scodoc/codes_cursus.py b/app/scodoc/codes_cursus.py
index 001a1b8aa..e7af230dc 100644
--- a/app/scodoc/codes_cursus.py
+++ b/app/scodoc/codes_cursus.py
@@ -824,7 +824,8 @@ FORMATION_CURSUS_DESCRS = [p[1].__doc__ for p in _tp] # intitulés (eg pour men
FORMATION_CURSUS_TYPES = [p[0] for p in _tp] # codes numeriques (TYPE_CURSUS)
-def get_cursus_from_code(code_cursus):
+def get_cursus_from_code(code_cursus: int) -> TypeCursus:
+ "renvoie le cursus de code indiqué"
cursus = SCO_CURSUS.get(code_cursus)
if cursus is None:
log(f"Warning: invalid code_cursus: {code_cursus}")
diff --git a/app/scodoc/sco_formations.py b/app/scodoc/sco_formations.py
index 9022169de..5c53ef9b9 100644
--- a/app/scodoc/sco_formations.py
+++ b/app/scodoc/sco_formations.py
@@ -38,7 +38,7 @@ import app.scodoc.sco_utils as scu
import app.scodoc.notesdb as ndb
from app import db
from app import log
-from app.models import Formation, Module, UniteEns
+from app.models import Formation, FormSemestre, Module, UniteEns
from app.models import ScolarNews
from app.models.but_refcomp import (
ApcAppCritique,
@@ -431,13 +431,12 @@ def formation_import_xml(doc: str, import_tags=True, use_local_refcomp=False):
return formation.id, modules_old2new, ues_old2new
-def formation_list_table(formation_id=None, args: dict = None):
+def formation_list_table() -> GenTable:
"""List formation, grouped by titre and sorted by versions
and listing associated semestres
returns a table
"""
- args = args or {}
- formations = formation_list(formation_id=formation_id, args=args)
+ formations: list[Formation] = Formation.query.filter_by(dept_id=g.scodoc_dept_id)
title = "Programmes pédagogiques"
lockicon = scu.icontag(
"lock32_img", title="Comporte des semestres verrouillés", border="0"
@@ -452,82 +451,89 @@ def formation_list_table(formation_id=None, args: dict = None):
editable = current_user.has_permission(Permission.ScoChangeFormation)
# Traduit/ajoute des champs à afficher:
- for f in formations:
- try:
- f["parcours_name"] = codes_cursus.get_cursus_from_code(
- f["type_parcours"]
- ).NAME
- except:
- f["parcours_name"] = ""
- f["_titre_target"] = url_for(
- "notes.ue_table",
- scodoc_dept=g.scodoc_dept,
- formation_id=str(f["formation_id"]),
- )
- f["_titre_link_class"] = "stdlink"
- f["_titre_id"] = "titre-%s" % f["acronyme"].lower().replace(" ", "-")
+ rows = []
+ for formation in formations:
+ acronyme_no_spaces = formation.acronyme.lower().replace(" ", "-")
+ row = {
+ "acronyme": formation.acronyme,
+ "parcours_name": codes_cursus.get_cursus_from_code(
+ formation.type_parcours
+ ).NAME,
+ "titre": formation.titre,
+ "_titre_target": url_for(
+ "notes.ue_table",
+ scodoc_dept=g.scodoc_dept,
+ formation_id=formation.id,
+ ),
+ "_titre_link_class": "stdlink",
+ "_titre_id": f"""titre-{acronyme_no_spaces}""",
+ "version": formation.version or 0,
+ }
# Ajoute les semestres associés à chaque formation:
- f["sems"] = sco_formsemestre.do_formsemestre_list(
- args={"formation_id": f["formation_id"]}
- )
- f["sems_list_txt"] = ", ".join([s["session_id"] for s in f["sems"]])
- f["_sems_list_txt_html"] = ", ".join(
- [
- '%('
- "session_id)s " % s
- for s in f["sems"]
- ]
- + (
- [
- 'ajouter '
- % (f["acronyme"].lower().replace(" ", "-"), f["formation_id"])
- ]
- if current_user.has_permission(Permission.ScoImplement)
- else []
+ row["formsemestres"] = formation.formsemestres.order_by(
+ FormSemestre.date_debut
+ ).all()
+ row["sems_list_txt"] = ", ".join(s.session_id() for s in row["formsemestres"])
+ row["_sems_list_txt_html"] = (
+ ", ".join(
+ f"""{s.session_id()}
+ """
+ for s in row["formsemestres"]
)
+ + f""", ajouter
+ """
)
- if f["sems"]:
- f["date_fin_dernier_sem"] = max([s["date_fin_iso"] for s in f["sems"]])
- f["annee_dernier_sem"] = f["date_fin_dernier_sem"].split("-")[0]
+ if row["formsemestres"]:
+ row["date_fin_dernier_sem"] = (
+ row["formsemestres"][-1].date_fin.isoformat(),
+ )
+ row["annee_dernier_sem"] = row["formsemestres"][-1].date_fin.year
else:
- f["date_fin_dernier_sem"] = ""
- f["annee_dernier_sem"] = ""
- formation: Formation = Formation.query.get_or_404(f["formation_id"])
- locked = formation.has_locked_sems()
+ row["date_fin_dernier_sem"] = ""
+ row["annee_dernier_sem"] = 0
#
- if locked:
+ if formation.has_locked_sems():
but_locked = lockicon
+ but_suppr = ''
else:
but_locked = ''
- if editable and not locked:
- but_suppr = (
- '%s'
- % (
- f["formation_id"],
- f["acronyme"].lower().replace(" ", "-"),
- suppricon,
- )
- )
- else:
- but_suppr = ''
+ if editable:
+ but_suppr = f"""{suppricon}"""
+ else:
+ but_suppr = ''
if editable:
- but_edit = (
- '%s'
- % (f["formation_id"], f["acronyme"].lower().replace(" ", "-"), editicon)
- )
+ but_edit = f"""{editicon}"""
else:
but_edit = ''
- f["buttons"] = ""
- f["_buttons_html"] = but_locked + but_suppr + but_edit
- # Tri par annee_denier_sem, type, acronyme, titre, version décroissante
- formations.sort(key=itemgetter("version"), reverse=True)
- formations.sort(key=itemgetter("titre"))
- formations.sort(key=itemgetter("acronyme"))
- formations.sort(key=itemgetter("parcours_name"))
- formations.sort(
- key=itemgetter("annee_dernier_sem"), reverse=True
- ) # plus recemments utilises en tete
+ row["buttons"] = ""
+ row["_buttons_html"] = but_locked + but_suppr + but_edit
+ rows.append(row)
+ # Tri par annee_dernier_sem, type, acronyme, titre, version décroissante
+ # donc plus récemment utilsiée en tête
+ rows.sort(
+ key=lambda row: (
+ -row["annee_dernier_sem"],
+ row["parcours_name"],
+ row["acronyme"],
+ row["titre"],
+ -row["version"],
+ )
+ )
+ for i, row in enumerate(rows):
+ row["_buttons_order"] = f"{i:05d}"
#
columns_ids = (
@@ -550,18 +556,16 @@ def formation_list_table(formation_id=None, args: dict = None):
}
return GenTable(
columns_ids=columns_ids,
- rows=formations,
+ rows=rows,
titles=titles,
- origin="Généré par %s le " % sco_version.SCONAME
- + scu.timedate_human_repr()
- + "",
+ origin=f"Généré par {sco_version.SCONAME} le {scu.timedate_human_repr()}",
caption=title,
html_caption=title,
table_id="formation_list_table",
html_class="formation_list_table table_leftalign",
html_with_td_classes=True,
html_sortable=True,
- base_url="%s?formation_id=%s" % (request.base_url, formation_id),
+ base_url="{request.base_url}?formation_id={formation_id}",
page_title=title,
pdf_title=title,
preferences=sco_preferences.SemPreferences(),