ScoDoc-PE/app/but/jury_but_recap.py

247 lines
8.7 KiB
Python

##############################################################################
# ScoDoc
# Copyright (c) 1999 - 2022 Emmanuel Viennet. All rights reserved.
# See LICENSE
##############################################################################
"""Jury BUT: table recap annuelle et liens saisie
"""
import time
from flask import g, url_for
from app import db
from app.but import jury_but
from app.comp.res_but import ResultatsSemestreBUT
from app.comp import res_sem
from app.models.but_validations import RegroupementCoherentUE
from app.models.etudiants import Identite
from app.models.formsemestre import FormSemestre
from app.models.ues import UniteEns
from app.scodoc.sco_codes_parcours import (
BUT_BARRE_RCUE,
BUT_BARRE_UE,
BUT_BARRE_UE8,
BUT_RCUE_SUFFISANT,
)
from app.scodoc import sco_formsemestre_status
from app.scodoc import html_sco_header
from app.scodoc import sco_utils as scu
from app.scodoc.sco_exceptions import ScoException, ScoValueError
def formsemestre_saisie_jury_but(
formsemestre2: FormSemestre, readonly: bool = False
) -> str:
"""formsemestre est un semestre PAIR
Si readonly, ne montre pas le lien "saisir la décision"
=> page html complète
"""
# Quick & Dirty
# pour chaque etud de res2 trié
# S1: UE1, ..., UEn
# S2: UE1, ..., UEn
#
# UE1_s1, UE1_s2, moy_rcue, UE2... , Nbrcue_validables, Nbrcue<8, passage_de_droit, valide_moitie_rcue
#
# Pour chaque etud de res2 trié
# DecisionsProposeesAnnee(etud, formsemestre2)
# Pour le 1er etud, faire un check_ues_ready_jury(self) -> page d'erreur
# -> rcue .ue_1, .ue_2 -> stroe moy ues, rcue.moy_rcue, etc
if formsemestre2.semestre_id % 2 != 0:
raise ScoValueError("Cette page ne fonctionne que sur les semestres pairs")
rows, titles, column_ids = get_table_jury_but(formsemestre2, readonly=readonly)
if not rows:
return (
'<div class="table_recap"><div class="message">aucun étudiant !</div></div>'
)
filename = scu.sanitize_filename(
f"""jury-but-{formsemestre2.titre_num()}-{time.strftime("%Y-%m-%d")}"""
)
table_html = build_table_jury_but_html(filename, rows, titles, column_ids)
H = [
html_sco_header.sco_header(
page_title=f"{formsemestre2.sem_modalite()}: moyennes",
no_side_bar=True,
init_qtip=True,
javascripts=["js/etud_info.js", "js/table_recap.js"],
),
sco_formsemestre_status.formsemestre_status_head(
formsemestre_id=formsemestre2.id
),
]
H.append(table_html)
H.append(html_sco_header.sco_footer())
return "\n".join(H)
def build_table_jury_but_html(filename: str, rows, titles, column_ids) -> str:
"""assemble la table html"""
footer_rows = [] # inutile pour l'instant, à voir XXX
selected_etudid = None # inutile pour l'instant, à voir XXX
H = [
f"""<div class="table_recap"><table class="table_recap apc jury"
data-filename="{filename}">"""
]
# header
H.append(
f"""
<thead>
{scu.gen_row(column_ids, titles, "th")}
</thead>
"""
)
# body
H.append("<tbody>")
for row in rows:
H.append(f"{scu.gen_row(column_ids, row, selected_etudid=selected_etudid)}\n")
H.append("</tbody>\n")
# footer
H.append("<tfoot>")
idx_last = len(footer_rows) - 1
for i, row in enumerate(footer_rows):
H.append(f'{scu.gen_row(column_ids, row, "th" if i == idx_last else "td")}\n')
H.append(
"""
</tfoot>
</table>
</div>
"""
)
return "".join(H)
class RowCollector:
"""Une ligne de la table"""
def __init__(self, cells: dict = None, titles: dict = None, convert_values=True):
self.titles = titles
self.row = cells or {} # col_id : str
self.idx = 0
if convert_values:
self.fmt_note = scu.fmt_note
else:
self.fmt_note = lambda x: x
def __setitem__(self, key, value):
self.row[key] = value
def __getitem__(self, key):
return self.row[key]
def add_cell(
self,
col_id: str,
title: str,
content: str,
classes: str = "",
idx: int = None,
):
"Add a row to our table. classes is a list of css class names"
self.idx = idx if idx is not None else self.idx
self.row[col_id] = content
if classes:
self.row[f"_{col_id}_class"] = classes + f" c{self.idx}"
if not col_id in self.titles:
self.titles[col_id] = title
self.titles[f"_{col_id}_col_order"] = self.idx
if classes:
self.titles[f"_{col_id}_class"] = classes
self.idx += 1
def add_etud_cells(self, etud: Identite, formsemestre: FormSemestre):
"Les cells code, nom, prénom etc."
# --- Codes (seront cachés, mais exportés en excel)
self.add_cell("etudid", "etudid", etud.id, "codes")
self.add_cell("code_nip", "code_nip", etud.code_nip or "", "codes")
# --- Identité étudiant (adapté de res_comon/get_table_recap, à factoriser XXX TODO)
self.add_cell("civilite_str", "Civ.", etud.civilite_str, "identite_detail")
self.add_cell("nom_disp", "Nom", etud.nom_disp(), "identite_detail")
self["_nom_disp_order"] = etud.sort_key
self.add_cell("prenom", "Prénom", etud.prenom, "identite_detail")
self.add_cell("nom_short", "Nom", etud.nom_short, "identite_court")
self["_nom_short_order"] = etud.sort_key
self["_nom_short_target"] = url_for(
"notes.formsemestre_bulletinetud",
scodoc_dept=g.scodoc_dept,
formsemestre_id=formsemestre.id,
etudid=etud.id,
)
self["_nom_short_target_attrs"] = f'class="etudinfo" id="{etud.id}"'
self["_nom_disp_target"] = self["_nom_short_target"]
self["_nom_disp_target_attrs"] = self["_nom_short_target_attrs"]
def add_ue_cell(self, ue: UniteEns, val):
"cell de moyenne d'UE"
col_id = f"moy_ue_{ue.id}"
note_class = ""
if isinstance(val, float):
if val < BUT_BARRE_UE:
note_class = " moy_inf"
elif val >= BUT_BARRE_UE:
note_class = " moy_ue_valid"
if val < BUT_BARRE_UE8:
note_class = " moy_ue_warning" # notes très basses
self.add_cell(col_id, ue.acronyme, self.fmt_note(val), "col_ue" + note_class)
def add_rcue_cell(self, rcue: RegroupementCoherentUE):
"cell de moyenne d'UE"
col_id = f"moy_rcue_{rcue.ue_1.niveau_competence_id}" # le niveau_id
note_class = ""
val = rcue.moy_rcue
if isinstance(val, float):
if val < BUT_BARRE_RCUE:
note_class = " moy_inf"
elif val >= BUT_BARRE_RCUE:
note_class = " moy_ue_valid"
if val < BUT_RCUE_SUFFISANT:
note_class = " moy_ue_warning" # notes très basses
self.add_cell(
col_id,
f"{rcue.ue_1.acronyme}-{rcue.ue_2.acronyme}",
self.fmt_note(val),
"col_ue" + note_class,
)
def get_table_jury_but(
formsemestre2: FormSemestre, readonly: bool = False
) -> tuple[list[dict], list[str], list[str]]:
"""Construit la table des résultats annuels pour le jury BUT"""
res2: ResultatsSemestreBUT = res_sem.load_formsemestre_results(formsemestre2)
titles = {} # column_id : title
rows = []
for etudid in formsemestre2.etuds_inscriptions:
etud: Identite = Identite.query.get(etudid)
deca = jury_but.DecisionsProposeesAnnee(etud, formsemestre2)
row = RowCollector(titles=titles)
row.add_etud_cells(etud, formsemestre2)
# --- Les RCUEs
for rcue in deca.rcues_annee:
row.add_ue_cell(rcue.ue_1, rcue.moy_ue_1)
row.add_ue_cell(rcue.ue_2, rcue.moy_ue_2)
row.add_rcue_cell(rcue)
# --- Le lien de saisie
if not readonly:
row.add_cell(
"lien_saisie",
"",
f"""<a href="{url_for(
'notes.formsemestre_validation_but',
scodoc_dept=g.scodoc_dept,
etudid=etud.id,
formsemestre_id=formsemestre2.id,
)}" class="stdlink">saisie décision</a>
""",
)
rows.append(row.row)
res2.recap_add_partitions(rows, titles)
column_ids = [title for title in titles if not title.startswith("_")]
column_ids.sort(key=lambda col_id: titles.get("_" + col_id + "_col_order", 1000))
rows.sort(key=lambda row: row["_nom_disp_order"])
return rows, titles, column_ids