WIP: new code table recap.

This commit is contained in:
Emmanuel Viennet 2023-01-22 16:39:46 -03:00 committed by iziram
parent c0a4f40803
commit 28baca0696
3 changed files with 1075 additions and 4 deletions

View File

@ -28,6 +28,7 @@ from app.scodoc.sco_cache import ResultatsSemestreCache
from app.scodoc.codes_cursus import UE_SPORT
from app.scodoc.sco_exceptions import ScoValueError
from app.scodoc import sco_utils as scu
from app.scodoc import table_builder as tb
# Il faut bien distinguer
# - ce qui est caché de façon persistente (via redis):
@ -452,3 +453,687 @@ class ResultatsSemestre(ResultatsCache):
# ici si l'étudiant est inscrit dans le semestre courant,
# somme des coefs des modules de l'UE auxquels il est inscrit
return self.compute_etud_ue_coef(etudid, ue)
# --- TABLEAU RECAP
def get_table_recap(
self,
convert_values=False,
include_evaluations=False,
mode_jury=False,
allow_html=True,
) -> tb.Table:
"""Table récap. des résultats.
allow_html: si vrai, peut mettre du HTML dans les valeurs
Result: XXX tuple avec
- rows: liste de dicts { column_id : value }
- titles: { column_id : title }
- columns_ids: (liste des id de colonnes)
Si convert_values, transforme les notes en chaines ("12.34").
Les colonnes générées sont:
etudid
rang : rang indicatif (basé sur moy gen)
moy_gen : moy gen indicative
moy_ue_<ue_id>, ..., les moyennes d'UE
moy_res_<modimpl_id>_<ue_id>, ... les moyennes de ressources dans l'UE
moy_sae_<modimpl_id>_<ue_id>, ... les moyennes de SAE dans l'UE
On ajoute aussi des attributs: XXX
- pour les lignes:
_css_row_class (inutilisé pour le monent)
_<column_id>_class classe css:
- la moyenne générale a la classe col_moy_gen
- les colonnes SAE ont la classe col_sae
- les colonnes Resources ont la classe col_res
- les colonnes d'UE ont la classe col_ue
- les colonnes de modules (SAE ou res.) d'une UE ont la classe mod_ue_<ue_id>
_<column_id>_order : clé de tri
"""
if convert_values:
fmt_note = scu.fmt_note
else:
fmt_note = lambda x: x
parcours = self.formsemestre.formation.get_parcours()
barre_moy = parcours.BARRE_MOY - scu.NOTES_TOLERANCE
barre_valid_ue = parcours.NOTES_BARRE_VALID_UE
barre_warning_ue = parcours.BARRE_UE_DISPLAY_WARNING
NO_NOTE = "-" # contenu des cellules sans notes
rows = []
dict_nom_res = {} # cache uid : nomcomplet
# def add_cell(
# row: dict,
# col_id: str,
# title: str,
# content: str,
# classes: str = "",
# idx: int = 100,
# ):
# "Add a cell to our table. classes is a list of css class names"
# row[col_id] = content
# if classes:
# row[f"_{col_id}_class"] = classes + f" c{idx}"
# if not col_id in titles:
# titles[col_id] = title
# titles[f"_{col_id}_col_order"] = idx
# if classes:
# titles[f"_{col_id}_class"] = classes
# return idx + 1
etuds_inscriptions = self.formsemestre.etuds_inscriptions
ues = self.formsemestre.query_ues(with_sport=True) # avec bonus
ues_sans_bonus = [ue for ue in ues if ue.type != UE_SPORT]
modimpl_ids = set() # modimpl effectivement présents dans la table
table = tb.Table()
for etudid in etuds_inscriptions:
idx = 0 # index de la colonne
etud = Identite.query.get(etudid)
row = tb.Row(table, etudid)
table.add_row(row)
# --- 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",
)
# --- Rang
if not self.formsemestre.block_moyenne_generale:
row.add_cell(
"rang",
"Rg",
self.etud_moy_gen_ranks[etudid],
"rang",
data={"order": f"{self.etud_moy_gen_ranks_int[etudid]:05d}"},
)
# --- Identité étudiant
url_bulletin = url_for(
"notes.formsemestre_bulletinetud",
scodoc_dept=g.scodoc_dept,
formsemestre_id=self.formsemestre.id,
etudid=etudid,
)
row.add_cell("civilite_str", "Civ.", etud.civilite_str, "identite_detail")
row.add_cell(
"nom_disp",
"Nom",
etud.nom_disp(),
"identite_detail",
data={"order": etud.sort_key},
target=url_bulletin,
target_attrs=f'class="etudinfo" id="{etudid}"',
)
row.add_cell("prenom", "Prénom", etud.prenom, "identite_detail")
row.add_cell(
"nom_short",
"Nom",
etud.nom_short,
"identite_court",
data={"order": etud.sort_key},
target=url_bulletin,
target_attrs=f'class="etudinfo" id="{etudid}"',
)
# --- Moyenne générale
if not self.formsemestre.block_moyenne_generale:
moy_gen = self.etud_moy_gen.get(etudid, False)
note_class = ""
if moy_gen is False:
moy_gen = NO_NOTE
elif isinstance(moy_gen, float) and moy_gen < barre_moy:
note_class = "moy_ue_warning" # en rouge
row.add_cell(
"moy_gen",
"Moy",
fmt_note(moy_gen),
"col_moy_gen",
classes=[note_class],
)
# Ajoute bulle sur titre du pied de table:
table.foot_title_row.cells["moy_gen"].target_attrs = (
'title="moyenne indicative"' if self.is_apc else ""
)
# --- Moyenne d'UE
nb_ues_validables, nb_ues_warning = 0, 0
for ue in ues_sans_bonus:
ue_status = self.get_etud_ue_status(etudid, ue.id)
if ue_status is not None:
col_id = f"moy_ue_{ue.id}"
val = ue_status["moy"]
note_class = ""
if isinstance(val, float):
if val < barre_moy:
note_class = " moy_inf"
elif val >= barre_valid_ue:
note_class = " moy_ue_valid"
nb_ues_validables += 1
if val < barre_warning_ue:
note_class = " moy_ue_warning" # notes très basses
nb_ues_warning += 1
row.add_cell(
col_id,
ue.acronyme,
fmt_note(val),
group=f"col_ue_{ue.id}",
classes=["col_ue", note_class],
)
table.foot_title_row.cells[
col_id
].target_attrs = (
f"""title="{ue.titre} S{ue.semestre_idx or '?'}" """
)
if mode_jury:
# pas d'autre colonnes de résultats
continue
# Bonus (sport) dans cette UE ?
# Le bonus sport appliqué sur cette UE
if (self.bonus_ues is not None) and (ue.id in self.bonus_ues):
val = self.bonus_ues[ue.id][etud.id] or ""
val_fmt = val_fmt_html = fmt_note(val)
if val:
val_fmt_html = f"""<span class="green-arrow-up"></span><span class="sp2l">{
val_fmt
}</span>"""
row.add_cell(
f"bonus_ue_{ue.id}",
f"Bonus {ue.acronyme}",
val_fmt_html if allow_html else val_fmt,
group=f"col_ue_{ue.id}",
classes=["col_ue_bonus"],
raw_content=val_fmt,
)
# Les moyennes des modules (ou ressources et SAÉs) dans cette UE
for modimpl in self.modimpls_in_ue(ue, etudid, with_bonus=False):
if ue_status["is_capitalized"]:
val = "-c-"
else:
modimpl_results = self.modimpls_results.get(modimpl.id)
if modimpl_results: # pas bonus
if self.is_apc: # BUT
moys_vers_ue = modimpl_results.etuds_moy_module.get(
ue.id
)
val = (
moys_vers_ue.get(etudid, "?")
if moys_vers_ue is not None
else ""
)
else: # classique: Series indépendante de l'UE
val = modimpl_results.etuds_moy_module.get(
etudid, "?"
)
else:
val = ""
col_id = (
f"moy_{modimpl.module.type_abbrv()}_{modimpl.id}_{ue.id}"
)
val_fmt = val_fmt_html = fmt_note(val)
if convert_values and (
modimpl.module.module_type == scu.ModuleType.MALUS
):
val_fmt_html = (
(scu.EMO_RED_TRIANGLE_DOWN + val_fmt) if val else ""
)
cell = row.add_cell(
col_id,
modimpl.module.code,
val_fmt_html,
group=f"col_ue_{ue.id}_modules",
classes=[
f"col_{modimpl.module.type_abbrv()}",
f"mod_ue_{ue.id}",
],
raw_content=val_fmt,
)
if modimpl.module.module_type == scu.ModuleType.MALUS:
# positionne la colonne à droite de l'UE
cell.group = f"col_ue_{ue.id}_malus"
table.insert_group(cell.group, after=f"col_ue_{ue.id}")
table.foot_title_row.cells[col_id].target = url_for(
"notes.moduleimpl_status",
scodoc_dept=g.scodoc_dept,
moduleimpl_id=modimpl.id,
)
nom_resp = dict_nom_res.get(modimpl.responsable_id)
if nom_resp is None:
user = User.query.get(modimpl.responsable_id)
nom_resp = user.get_nomcomplet() if user else ""
dict_nom_res[modimpl.responsable_id] = nom_resp
table.foot_title_row.cells[
col_id
].target_attrs = (
f""" title="{modimpl.module.titre} ({nom_resp})" """
)
modimpl_ids.add(modimpl.id)
nb_ues_etud_parcours = len(self.etud_ues_ids(etudid))
ue_valid_txt = (
ue_valid_txt_html
) = f"{nb_ues_validables}/{nb_ues_etud_parcours}"
if nb_ues_warning:
ue_valid_txt_html += " " + scu.EMO_WARNING
# place juste avant moy. gen.
table.insert_group("col_ues_validables", before="moy_gen")
classes = ["col_ue"]
if nb_ues_warning:
classes.append("moy_ue_warning")
elif nb_ues_validables < len(ues_sans_bonus):
classes.append("moy_inf")
row.add_cell(
"ues_validables",
"UEs",
ue_valid_txt_html,
"col_ues_validables",
classes=classes,
raw_content=ue_valid_txt,
data={"order": nb_ues_validables}, # tri
)
if mode_jury and self.validations:
if self.is_apc:
# formations BUT: pas de code semestre, concatene ceux des UEs
dec_ues = self.validations.decisions_jury_ues.get(etudid)
if dec_ues:
jury_code_sem = ",".join(
[dec_ues[ue_id].get("code", "") for ue_id in dec_ues]
)
else:
jury_code_sem = ""
else:
# formations classiques: code semestre
dec_sem = self.validations.decisions_jury.get(etudid)
jury_code_sem = dec_sem["code"] if dec_sem else ""
row.add_cell("jury_code_sem", "Jury", jury_code_sem, "jury_code_sem")
row.add_cell(
"jury_link",
"",
f"""<a href="{url_for('notes.formsemestre_validation_etud_form',
scodoc_dept=g.scodoc_dept, formsemestre_id=self.formsemestre.id, etudid=etudid
)
}">{"saisir" if not jury_code_sem else "modifier"} décision</a>""",
"col_jury_link",
)
self.recap_add_partitions(table)
self.recap_add_cursus(table)
self._recap_add_admissions(table)
# tri par rang croissant
if not self.formsemestre.block_moyenne_generale:
table.sort_rows(key=lambda e: e["_rang_order"])
else:
table.sort_rows(key=lambda e: e["_ues_validables_order"], reverse=True)
# INFOS POUR FOOTER
bottom_infos = self._recap_bottom_infos(
table, ues_sans_bonus, modimpl_ids, fmt_note
)
if include_evaluations:
self._recap_add_evaluations(table)
# Ajoute style "col_empty" aux colonnes de modules vides
row_moy = table.get_row_by_id("moy")
for col_id in table.column_ids:
if "col_empty" in row_moy.cells[col_id]:
table.column_classes[col_id].append("col_empty")
# Ligne avec la classe de chaque colonne
# récupère le type à partir des classes css (hack...)
row_type = tb.BottomRow(
table,
"type_col",
title="Type col.",
left_title_col_ids=["prenom", "nom_short"],
category="bottom_infos",
classes=["bottom_info"],
)
for col_id in table.column_ids:
group_name = table.column_group.get(col_id, "")
if group_name.startswith("col_"):
group_name = group_name[4:]
row_type.add_cell(col_id, None, group_name)
# Titres
table.add_head_row(self.head_title_row)
table.add_foot_row(self.foot_title_row)
return table
def _recap_bottom_infos(
self, table: tb.Table, ues, modimpl_ids: set, fmt_note
) -> dict:
"""Les informations à mettre en bas de la table: min, max, moy, ECTS, Apo"""
# Ordre des lignes: Min, Max, Moy, Coef, ECTS, Apo
row_min = tb.BottomRow(
table,
"min",
title="Min.",
left_title_col_ids=["prenom", "nom_short"],
category="bottom_infos",
classes=["bottom_info"],
)
row_max = tb.BottomRow(
table,
"max",
title="Max.",
left_title_col_ids=["prenom", "nom_short"],
category="bottom_infos",
classes=["bottom_info"],
)
row_moy = tb.BottomRow(
table,
"moy",
title="Moy.",
left_title_col_ids=["prenom", "nom_short"],
category="bottom_infos",
classes=["bottom_info"],
)
row_coef = tb.BottomRow(
table,
"coef",
title="Coef.",
left_title_col_ids=["prenom", "nom_short"],
category="bottom_infos",
classes=["bottom_info"],
)
row_ects = tb.BottomRow(
table,
"ects",
title="ECTS",
left_title_col_ids=["prenom", "nom_short"],
category="bottom_infos",
classes=["bottom_info"],
)
row_apo = tb.BottomRow(
table,
"apo",
title="Code Apogée",
left_title_col_ids=["prenom", "nom_short"],
category="bottom_infos",
classes=["bottom_info"],
)
# --- ECTS
# titre (à gauche) sur 2 colonnes pour s'adapter à l'affichage des noms/prenoms
for ue in ues:
col_id = f"moy_ue_{ue.id}"
row_ects.add_cell(col_id, None, ue.ects, "col_ue")
# ajoute cell UE vides sur ligne coef pour borders verticales
# XXX TODO classes dans table sur colonne ajoutées à tous les TD
row_coef.add_cell(col_id, None, "", "col_ue")
row_ects.add_cell(
"moy_gen",
None,
sum([ue.ects or 0 for ue in ues if ue.type != UE_SPORT]),
"col_moy_gen",
)
# --- MIN, MAX, MOY, APO
row_min.add_cell("moy_gen", None, fmt_note(self.etud_moy_gen.min()))
row_max.add_cell("moy_gen", None, fmt_note(self.etud_moy_gen.max()))
row_moy.add_cell("moy_gen", None, fmt_note(self.etud_moy_gen.mean()))
for ue in ues:
col_id = f"moy_ue_{ue.id}"
row_min.add_cell(col_id, None, fmt_note(self.etud_moy_ue[ue.id].min()))
row_max.add_cell(col_id, None, fmt_note(self.etud_moy_ue[ue.id].max()))
row_moy.add_cell(col_id, None, fmt_note(self.etud_moy_ue[ue.id].mean()))
row_apo.add_cell(col_id, None, ue.code_apogee or "")
for modimpl in self.formsemestre.modimpls_sorted:
if modimpl.id in modimpl_ids:
col_id = f"moy_{modimpl.module.type_abbrv()}_{modimpl.id}_{ue.id}"
if self.is_apc:
coef = self.modimpl_coefs_df[modimpl.id][ue.id]
else:
coef = modimpl.module.coefficient or 0
row_coef.add_cell(col_id, None, fmt_note(coef))
notes = self.modimpl_notes(modimpl.id, ue.id)
if np.isnan(notes).all():
# aucune note valide
row_min.add_cell(col_id, None, np.nan)
row_max.add_cell(col_id, None, np.nan)
moy = np.nan
else:
row_min.add_cell(col_id, None, fmt_note(np.nanmin(notes)))
row_max.add_cell(col_id, None, fmt_note(np.nanmax(notes)))
moy = np.nanmean(notes)
row_moy.add_cell(
col_id,
None,
fmt_note(moy),
# aucune note dans ce module ?
classes=["col_empty" if np.isnan(moy) else ""],
)
row_apo.add_cell(col_id, None, modimpl.module.code_apogee or "")
return { # { key : row } avec key = min, max, moy, coef, ...
"min": row_min,
"max": row_max,
"moy": row_moy,
"coef": row_coef,
"ects": row_ects,
"apo": row_apo,
}
def _recap_etud_groups_infos(
self, etudid: int, row: dict, titles: dict
): # XXX non utilisé
"""Table recap: ajoute à row les colonnes sur les groupes pour cet etud"""
# dec = self.get_etud_decision_sem(etudid)
# if dec:
# codes_nb[dec["code"]] += 1
row_class = ""
etud_etat = self.get_etud_etat(etudid)
if etud_etat == DEM:
gr_name = "Dém."
row_class = "dem"
elif etud_etat == DEF:
gr_name = "Déf."
row_class = "def"
else:
# XXX probablement à revoir pour utiliser données cachées,
# via get_etud_groups_in_partition ou autre
group = sco_groups.get_etud_main_group(etudid, self.formsemestre.id)
gr_name = group["group_name"] or ""
row["group"] = gr_name
row["_group_class"] = "group"
if row_class:
row["_tr_class"] = " ".join([row.get("_tr_class", ""), row_class])
titles["group"] = "Gr"
def _recap_add_admissions(self, table: tb.Table):
"""Ajoute les colonnes "admission"
Les colonnes ont la classe css "admission"
"""
fields = {
"bac": "Bac",
"specialite": "Spécialité",
"type_admission": "Type Adm.",
"classement": "Rg. Adm.",
}
first = True
for cid, title in fields.items():
cell_head, cell_foot = table.add_title(cid, title)
cell_head.classes.append("admission")
cell_foot.classes.append("admission")
if first:
cell_head.classes.append("admission_first")
cell_foot.classes.append("admission_first")
first = False
for row in table.rows:
etud = Identite.query.get(row.id) # TODO XXX
admission = etud.admission.first()
first = True
for cid, title in fields.items():
cell = row.add_cell(
cid,
title,
getattr(admission, cid) or "",
"admission",
)
if first:
cell.classes.append("admission_first")
first = False
def recap_add_cursus(self, table: tb.Table):
"""Ajoute colonne avec code cursus, eg 'S1 S2 S1'"""
table.insert_group("cursus", before="col_ues_validables")
cid = "code_cursus"
formation_code = self.formsemestre.formation.formation_code
for row in table.rows:
etud = Identite.query.get(row.id) # TODO XXX à optimiser: etud dans row
row.add_cell(
cid,
"Cursus",
" ".join(
[
f"S{ins.formsemestre.semestre_id}"
for ins in reversed(etud.inscriptions())
if ins.formsemestre.formation.formation_code == formation_code
]
),
"cursus",
)
def recap_add_partitions(self, table: tb.Table):
"""Ajoute les colonnes indiquant les groupes
La table contient des rows avec la clé etudid.
Les colonnes ont la classe css "partition"
"""
table.insert_group("partition", after="parcours")
partitions, partitions_etud_groups = sco_groups.get_formsemestre_groups(
self.formsemestre.id
)
first_partition = True
for partition in partitions:
col_classes = ["partition"]
if not first_partition:
col_classes.append("partition_aux")
first_partition = False
cid = f"part_{partition['partition_id']}"
cell_head, cell_foot = table.add_title(cid, partition["partition_name"])
cell_head.classes += col_classes
cell_foot.classes += col_classes
if partition["bul_show_rank"]:
rg_cid = cid + "_rg" # rang dans la partition
cell_head, cell_foot = table.add_title(
cid, f"Rg {partition['partition_name']}"
)
cell_head.classes.append("partition_rangs")
cell_foot.classes.append("partition_rangs")
partition_etud_groups = partitions_etud_groups[partition["partition_id"]]
for row in table.rows:
group = None # group (dict) de l'étudiant dans cette partition
# dans NotesTableCompat, à revoir
etud_etat = self.get_etud_etat(row.id) # row.id == etudid
tr_classes = []
if etud_etat == scu.DEMISSION:
gr_name = "Dém."
tr_classes.append("dem")
elif etud_etat == DEF:
gr_name = "Déf."
tr_classes.append("def")
else:
group = partition_etud_groups.get(row["etudid"])
gr_name = group["group_name"] if group else ""
if gr_name:
row.add_cell(
cid,
partition["partition_name"],
gr_name,
"partition",
classes=col_classes,
)
# Rangs dans groupe
if (
partition["bul_show_rank"]
and (group is not None)
and (group["id"] in self.moy_gen_rangs_by_group)
):
rang = self.moy_gen_rangs_by_group[group["id"]][0]
row.add_cell(rg_cid, None, rang.get(row["etudid"], ""), "partition")
def _recap_add_evaluations(self, table: tb.Table):
"""Ajoute les colonnes avec les notes aux évaluations
rows est une liste de dict avec une clé "etudid"
Les colonnes ont la classe css "evaluation"
"""
# nouvelle ligne pour description évaluations:
row_descr_eval = tb.BottomRow(
table,
"evaluations",
title="Description évaluations",
left_title_col_ids=["prenom", "nom_short"],
category="bottom_infos",
classes=["bottom_info"],
)
first_eval = True
for modimpl in self.formsemestre.modimpls_sorted:
evals = self.modimpls_results[modimpl.id].get_evaluations_completes(modimpl)
eval_index = len(evals) - 1
inscrits = {i.etudid for i in modimpl.inscriptions}
first_eval_of_mod = True
for e in evals:
col_id = f"eval_{e.id}"
title = f'{modimpl.module.code} {eval_index} {e.jour.isoformat() if e.jour else ""}'
col_classes = ["evaluation"]
if first_eval:
col_classes.append("first")
elif first_eval_of_mod:
col_classes.append("first_of_mod")
first_eval_of_mod = first_eval = False
eval_index -= 1
notes_db = sco_evaluation_db.do_evaluation_get_all_notes(
e.evaluation_id
)
for row in table.rows:
etudid = row.id
if etudid in inscrits:
if etudid in notes_db:
val = notes_db[etudid]["value"]
else:
# Note manquante mais prise en compte immédiate: affiche ATT
val = scu.NOTES_ATTENTE
content = scu.fmt_note(val)
classes = col_classes + [
{
"ABS": "abs",
"ATT": "att",
"EXC": "exc",
}.get(content, "")
]
row.add_cell(col_id, title, content, "", classes=classes)
else:
row.add_cell(
col_id,
title,
"ni",
"",
classes=col_classes + ["non_inscrit"],
)
table.get_row_by_id("coef").row[col_id] = e.coefficient
table.get_row_by_id("min").row[col_id] = "0"
table.get_row_by_id("max").row[col_id] = scu.fmt_note(e.note_max)
row_descr_eval.add_cell(
col_id,
None,
e.description or "",
target=url_for(
"notes.evaluation_listenotes",
scodoc_dept=g.scodoc_dept,
evaluation_id=e.id,
),
)

View File

@ -474,10 +474,54 @@ def _gen_formsemestre_recapcomplet_table(
mode_jury=mode_jury,
read_only=not formsemestre.can_edit_jury(),
)
table.data["filename"] = filename
table.select_row(selected_etudid)
return table
if not rows:
return (
'<div class="table_recap"><div class="message">aucun étudiant !</div></div>'
)
H = [
f"""<div class="table_recap">
{
table.html(
classes=[
'table_recap',
'apc' if formsemestre.formation.is_apc() else 'classic',
'jury' if mode_jury else ''
],
data={"filename":filename}
)
}
<table class="table_recap {
'apc' if formsemestre.formation.is_apc() else 'classic'
} {'jury' if mode_jury else ''}"
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 gen_formsemestre_recapcomplet_excel(

342
app/scodoc/table_builder.py Normal file
View File

@ -0,0 +1,342 @@
##############################################################################
# ScoDoc
# Copyright (c) 1999 - 2023 Emmanuel Viennet. All rights reserved.
# See LICENSE
##############################################################################
"""Classes pour aider à construire des tables de résultats
"""
from collections import defaultdict
class Table:
"""Construction d'une table de résultats
table = Table()
row = table.new_row(id="xxx", category="yyy")
row.new_cell( col_id, title, content [,classes] [, idx], [group], [keys:dict={}] )
rows = table.get_rows([category="yyy"])
table.sort_rows(key [, reverse])
table.set_titles(titles)
table.update_titles(titles)
table.set_column_groups(groups: list[str])
table.sort_columns()
table.insert_group(group:str, [after=str], [before=str])
"""
def __init__(
self,
selected_row_id: str = None,
classes: list[str] = None,
data: dict[str, str] = None,
):
self.rows: list["Row"] = []
"ordered list of Rows"
self.row_by_id: dict[str, "Row"] = {}
self.classes = classes or []
"list of classes for the table element"
self.column_ids = []
"ordered list of columns ids"
self.data = data or {}
"data-xxx"
self.groups = []
"ordered list of column groups names"
self.head = []
self.foot = []
self.column_group = {}
"the group of the column: { col_id : group }"
self.column_index: dict[str, int] = {}
"index the column: { col_id : int }"
self.column_classes: defaultdict[str, list[str]] = defaultdict(lambda: [])
"classe ajoutée à toutes les cellules de la colonne: { col_id : class }"
self.selected_row_id = selected_row_id
"l'id de la ligne sélectionnée"
self.titles = {}
"Column title: { col_id : titre }"
self.head_title_row: "Row" = Row(self, "title_head", cell_elt="th")
self.foot_title_row: "Row" = Row(self, "title_foot", cell_elt="th")
self.empty_cell = Cell.empty()
def get_row_by_id(self, row_id) -> "Row":
"return the row, or None"
return self.row_by_id.get(row_id)
def _prepare(self):
"""Sort table elements before generation"""
self.sort_columns()
def html(self, classes: list[str] = None, data: dict[str, str] = None) -> str:
"""HTML version of the table
classes are prepended to existing classes
data may replace existing data
"""
self._prepare()
classes = classes + self.classes
data = self.data.copy().update(data)
elt_class = f"""class="{' '.join(classes)}" """ if classes else ""
attrs_str = " ".join(f' data-{k}="v"' for k, v in data.items())
newline = "\n"
header = (
f"""
<thead>
{ newline.join(row.html() for row in self.head) }
</thead>
"""
if self.head
else ""
)
footer = (
f"""
<tfoot>
{ newline.join(row.html() for row in self.foot) }
</tfoot>
"""
if self.foot
else ""
)
return f"""<table {elt_class} {attrs_str}>
{header}
{
newline.join(row.html() for row in self.rows)
}
{footer}
</table>
"""
def add_row(self, row: "Row") -> "Row":
"""Append a new row"""
self.rows.append(row)
self.row_by_id[row.id] = row
return row
def add_head_row(self, row: "Row") -> "Row":
"Add a row to table head"
# row = Row(self, cell_elt="th", category="head")
self.head.append(row)
self.row_by_id[row.id] = row
return row
def add_foot_row(self, row: "Row") -> "Row":
"Add a row to table foot"
self.foot.append(row)
self.row_by_id[row.id] = row
return row
def sort_rows(self, key: callable, reverse: bool = False):
"""Sort table rows"""
self.rows.sort(key=key, reverse=reverse)
def sort_columns(self):
"""Sort columns ids"""
groups_order = {group: i for i, group in enumerate(self.groups)}
self.column_ids.sort(
key=lambda col_id: (groups_order.get(self.column_group.get(col_id), col_id))
)
def insert_group(self, group: str, after: str = None, before: str = None):
"""Déclare un groupe de colonnes et le place avant ou après un autre groupe.
Si pas d'autre groupe indiqué, le place après, à droite du dernier.
Si le group existe déjà, ne fait rien (ne le déplace pas).
"""
if group in self.groups:
return
other = after or before
if other is None:
self.groups.append(group)
else:
if not other in self.groups:
raise ValueError(f"invalid column group '{other}'")
index = self.groups.index(other)
if after:
index += 1
self.groups.insert(index, group)
def set_groups(self, groups: list[str]):
"""Define column groups and set order"""
self.groups = groups
def set_titles(self, titles: dict[str, str]):
"""Set columns titles"""
self.titles = titles
def update_titles(self, titles: dict[str, str]):
"""Set columns titles"""
self.titles.update(titles)
def add_title(self, col_id, title: str = None) -> tuple["Cell", "Cell"]:
"""Record this title,
and create cells for footer and header if they don't already exist.
"""
title = title or ""
if not col_id in self.titles:
self.titles[col_id] = title
cell_head = self.head_title_row.cells.get(
col_id, self.head_title_row.add_cell(col_id, title, title)
)
cell_foot = self.foot_title_row.cells.get(
col_id, self.foot_title_row.add_cell(col_id, title, title)
)
return cell_head, cell_foot
class Row:
"""A row."""
def __init__(
self,
table: Table,
row_id=None,
category=None,
cell_elt: str = None,
classes: list[str] = None,
):
self.category = category
self.cells = {}
self.cell_elt = cell_elt
self.classes: list[str] = classes or []
"classes sur le <tr>"
self.cur_idx_by_group = defaultdict(lambda: 0)
self.id = row_id
self.table = table
def add_cell(
self,
col_id: str,
title: str,
content: str,
group: str = None,
attrs: list[str] = None,
classes: list[str] = None,
data: dict[str, str] = None,
elt: str = None,
idx: int = None,
raw_content=None,
target_attrs: dict = None,
target: str = None,
) -> "Cell":
"""Create cell and add it to the row.
classes is a list of css class names
"""
if idx is None:
idx = self.cur_idx_by_group[group]
self.cur_idx_by_group[group] += 1
else:
self.cur_idx_by_group[group] = idx
self.table.column_index[col_id] = idx
cell = Cell(
content,
classes + [group or ""], # ajoute le nom de groupe aux classes
elt=elt or self.cell_elt,
attrs=attrs,
data=data,
raw_content=raw_content,
target=target,
target_attrs=target_attrs,
)
return self.add_cell_instance(col_id, cell, column_group=group, title=title)
def add_cell_instance(
self, col_id: str, cell: "Cell", column_group: str = None, title: str = None
) -> "Cell":
"""Add a cell to the row.
Si title est None, il doit avoir été ajouté avec table.add_title().
"""
self.cells[col_id] = cell
if column_group is not None:
self.table.column_group[col_id] = column_group
if title is not None:
self.table.add_title(col_id, title)
return cell
def html(self) -> str:
"""html for row, with cells"""
elt_class = f"""class="{' '.join(self.classes)}" """ if self.classes else ""
tr_id = (
"""id="row_selected" """ if (self.id == self.table.selected_etudid) else ""
) # TODO XXX remplacer id par une classe
return f"""<tr {tr_id} {elt_class}>{
"".join([self.cells.get(col_id,
self.table.empty_cell).html(
column_classes=self.table.column_classes.get(col_id)
)
for col_id in self.table.column_ids ])
}</tr>"""
class BottomRow(Row):
"""Une ligne spéciale pour le pied de table
avec un titre à gauche
(répété sur les colonnes indiquées par left_title_col_ids),
et automatiquement ajouté au footer.
"""
def __init__(
self, *args, left_title_col_ids: list[str] = None, left_title=None, **kwargs
):
super().__init__(*args, **kwargs)
self.left_title_col_ids = left_title_col_ids
if left_title is not None:
self.set_left_title(left_title)
self.table.add_foot_row(self)
def set_left_title(self, title: str = ""):
"Fill left title cells"
for col_id in self.left_title_col_ids:
self.add_cell(col_id, None, title)
class Cell:
"""Une cellule de table"""
def __init__(
self,
content,
classes: list[str] = None,
elt="td",
attrs: list[str] = None,
data: dict = None,
raw_content=None,
target: str = None,
target_attrs: dict = None,
):
"""if specified, raw_content will be used for raw exports like xlsx"""
self.content = content
self.classes: list = classes or []
self.elt = elt if elt is not None else "td"
self.attrs = attrs or []
if self.elt == "th":
self.attrs["scope"] = "row"
self.data = data or {}
self.raw_content = raw_content # not yet used
self.target = target
self.target_attrs = target_attrs or {}
@classmethod
def empty(cls):
"create a new empty cell"
return cls("")
def __str__(self):
return str(self.content)
def html(self, column_classes: list[str] = None) -> str:
"html for cell"
attrs_str = f"""class="{' '.join(
[cls for cls in (self.classes + (column_classes or [])) if cls])
}" """
# Autres attributs:
attrs_str += " ".join([f"{k}={v}" for (k, v) in self.attrs.items()])
# et data-x
attrs_str += " ".join([f' data-{k}="v"' for k, v in self.data.items()])
if (self.target is not None) or self.target_attrs:
href = f'href="{self.target}"' if self.target else ""
target_attrs_str = " ".join(
[f"{k}={v}" for (k, v) in self.target_attrs.items()]
)
content = f"<a {href} {target_attrs_str}>{content}</a>"
return f"""<{self.elt} {attrs_str}>{self.content}</{self.elt}>"""