ScoDoc/app/but/jury_but_recap.py

180 lines
6.2 KiB
Python
Raw Normal View History

##############################################################################
# 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.etudiants import Identite
from app.models.formsemestre import FormSemestre
from app.models.ues import UniteEns
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
class RowCollector:
def __init__(self, cells: dict = None, titles: dict = None):
self.titles = titles
self.row = cells or {} # col_id : str
self.idx = 0
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 __setitem__(self, key, value):
self.row[key] = value
def __getitem__(self, key):
return self.row[key]
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)
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)
rcues = []
titles = {} # column_id : title
rows = []
for etudid in formsemestre2.etuds_inscriptions:
etud = Identite.query.get(etudid)
deca = jury_but.DecisionsProposeesAnnee(etud, formsemestre2)
row = RowCollector(titles=titles)
# --- Codes (seront cachés, mais exportés en excel)
row.add_cell("etudid", "etudid", etudid, "codes")
row.add_cell("code_nip", "code_nip", etud.code_nip or "", "codes")
# --- Identité étudiant (adapté de res_comon/get_table_recap, à factoriser)
row.add_cell("civilite_str", "Civ.", etud.civilite_str, "identite_detail")
row.add_cell("nom_disp", "Nom", etud.nom_disp(), "identite_detail")
row["_nom_disp_order"] = etud.sort_key
row.add_cell("prenom", "Prénom", etud.prenom, "identite_detail")
row.add_cell("nom_short", "Nom", etud.nom_short, "identite_court")
row["_nom_short_order"] = etud.sort_key
row["_nom_short_target"] = url_for(
"notes.formsemestre_bulletinetud",
scodoc_dept=g.scodoc_dept,
formsemestre_id=formsemestre2.id,
etudid=etudid,
)
row["_nom_short_target_attrs"] = f'class="etudinfo" id="{etudid}"'
row["_nom_disp_target"] = row["_nom_short_target"]
row["_nom_disp_target_attrs"] = row["_nom_short_target_attrs"]
rows.append(row.row)
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))
return rows, titles, column_ids