sco_inscr_passage: refactoring

This commit is contained in:
Emmanuel Viennet 2024-10-30 22:11:39 +01:00
parent 28e794c089
commit dcbf4f32c4

View File

@ -625,25 +625,52 @@ def etuds_select_boxes(
for src_cat in auth_etuds_by_cat.keys(): for src_cat in auth_etuds_by_cat.keys():
infos = auth_etuds_by_cat[src_cat]["infos"] infos = auth_etuds_by_cat[src_cat]["infos"]
infos["comment"] = infos.get("comment", "") # commentaire dans sous-titre boite infos["comment"] = infos.get("comment", "") # commentaire dans sous-titre boite
help_txt = infos.get("help", "")
etuds = auth_etuds_by_cat[src_cat]["etuds"] etuds = auth_etuds_by_cat[src_cat]["etuds"]
etuds.sort(key=itemgetter("nom")) etuds.sort(key=itemgetter("nom"))
with_checkbox = (not read_only) and auth_etuds_by_cat[src_cat]["infos"].get( with_checkbox = (not read_only) and auth_etuds_by_cat[src_cat]["infos"].get(
"with_checkbox", True "with_checkbox", True
) )
checkbox_name = auth_etuds_by_cat[src_cat]["infos"].get(
"checkbox_name", "etuds"
)
etud_key = auth_etuds_by_cat[src_cat]["infos"].get("etud_key", "etudid")
if etuds or show_empty_boxes: if etuds or show_empty_boxes:
infos["nbetuds"] = len(etuds) infos["nbetuds"] = len(etuds)
H.append( xls_url = (
"""<div class="pas_sembox" id="%(id)s"> scu.build_url_query(base_url, export_cat_xls=src_cat)
<div class="pas_sembox_title"><a href="%(title_target)s" """ if base_url and etuds
% infos else ""
) )
H.append(
etuds_select_box(
etuds,
infos,
with_checkbox=with_checkbox,
sel_inscrits=sel_inscrits,
xls_url=xls_url,
inscrits_ailleurs=inscrits_ailleurs,
)
)
H.append("</div>")
return "\n".join(H)
def etuds_select_box(
etuds: list[dict],
infos: dict,
with_checkbox: bool = True,
sel_inscrits: bool = True,
xls_url: str = "",
inscrits_ailleurs: set = None,
) -> str:
"""HTML pour une "boite" avec une liste d'étudiants à sélectionner"""
inscrits_ailleurs = inscrits_ailleurs or {}
box_id = infos["id"]
H = []
H.append(
f"""<div class="pas_sembox" id="{box_id}">
<div class="pas_sembox_title"><a href="{infos['title_target']}" """
)
help_txt = infos.get("help")
if help_txt: # bubble if help_txt: # bubble
H.append('title="%s"' % help_txt) H.append(f'title="{help_txt}"')
H.append( H.append(
""">%(title)s</a></div> """>%(title)s</a></div>
<div class="pas_sembox_subtitle">(%(nbetuds)d étudiants%(comment)s)""" <div class="pas_sembox_subtitle">(%(nbetuds)d étudiants%(comment)s)"""
@ -651,45 +678,64 @@ def etuds_select_boxes(
) )
if with_checkbox: if with_checkbox:
H.append( H.append(
""" (Select. f""" (Select.
<a href="#" class="stdlink" onclick="sem_select('%(id)s', true);">tous</a> <a href="#" class="stdlink" onclick="sem_select('{box_id}', true);">tous</a>
<a href="#" class="stdlink" onclick="sem_select('%(id)s', false );">aucun</a>""" # " <a href="#" class="stdlink" onclick="sem_select('{box_id}', false );">aucun</a>"""
% infos
) )
if sel_inscrits: if sel_inscrits:
H.append( H.append(
"""<a href="#" class="stdlink" onclick="sem_select_inscrits('%(id)s');">inscrits</a>""" f"""<a href="#" class="stdlink" onclick="sem_select_inscrits('{box_id}');">inscrits</a>"""
% infos
) )
if with_checkbox or sel_inscrits: if with_checkbox or sel_inscrits:
H.append(")") H.append(")")
if base_url and etuds: if xls_url:
url = scu.build_url_query(base_url, export_cat_xls=src_cat) H.append(f'<a href="{xls_url}">{scu.ICON_XLS}</a>&nbsp;')
H.append(f'<a href="{url}">{scu.ICON_XLS}</a>&nbsp;')
H.append("</div>") H.append("</div>")
for etud in etuds: for etud in etuds:
if etud.get("inscrit", False): is_inscrit = etud.get("inscrit", False)
c = " deja-inscrit" extra_class = (
checked = 'checked="checked"' "deja-inscrit"
else: if is_inscrit
checked = "" else ("inscrit-ailleurs" if etud["etudid"] in inscrits_ailleurs else "")
if etud["etudid"] in inscrits_ailleurs: )
c = " inscrit-ailleurs" H.append(
else: _etud_row(
c = "" etud,
sco_etud.format_etud_ident(etud) with_checkbox=with_checkbox,
checkbox_name=infos.get("checkbox_name", "etuds"),
etud_key=infos.get("etud_key", "etudid"),
is_inscrit=is_inscrit,
extra_class=extra_class,
)
)
H.append("</div>")
return "\n".join(H)
def _etud_row(
etud: dict,
with_checkbox: bool = True,
checkbox_name: str = "etuds",
etud_key: str = "",
is_inscrit: bool = False,
extra_class: str = "",
) -> str:
"""HTML 'row' for this etud"""
H = []
nomprenom = scu.format_nomprenom(etud)
if etud["etudid"]: if etud["etudid"]:
elink = f"""<a id="{etud['etudid']}" class="discretelink etudinfo {c}" elink = f"""<a id="{etud['etudid']}" class="discretelink etudinfo {extra_class}"
href="{ url_for( href="{ url_for(
'scolar.fiche_etud', 'scolar.fiche_etud',
scodoc_dept=g.scodoc_dept, scodoc_dept=g.scodoc_dept,
etudid=etud['etudid'], etudid=etud['etudid'],
) )
}">{etud['nomprenom']}</a> }">{nomprenom}</a>
""" """
else: else:
# ce n'est pas un etudiant ScoDoc # ce n'est pas un etudiant ScoDoc
elink = etud["nomprenom"] elink = nomprenom
if etud.get("datefinalisationinscription", None): if etud.get("datefinalisationinscription", None):
elink += ( elink += (
@ -702,24 +748,21 @@ def etuds_select_boxes(
if not etud.get("paiementinscription", True): if not etud.get("paiementinscription", True):
elink += '<span class="paspaye"> (non paiement)</span>' elink += '<span class="paspaye"> (non paiement)</span>'
H.append("""<div class="pas_etud%s">""" % c) H.append(f"""<div class="pas_etud {extra_class}">""")
if "etape" in etud: if "etape" in etud:
etape_str = etud["etape"] or "" etape_str = etud["etape"] or ""
else: else:
etape_str = "" etape_str = ""
H.append("""<span class="sp_etape">%s</span>""" % etape_str) H.append(f"""<span class="sp_etape">{etape_str}</span>""")
if with_checkbox: if with_checkbox:
H.append( H.append(
"""<input type="checkbox" name="%s:list" value="%s" %s>""" f"""<input type="checkbox" name="{checkbox_name}:list"
% (checkbox_name, etud[etud_key], checked) value="{etud[etud_key]}" {'checked="checked"' if is_inscrit else ''}>"""
) )
H.append(elink) H.append(elink)
if with_checkbox: if with_checkbox:
H.append("""</input>""") H.append("""</input>""")
H.append("</div>") H.append("</div>")
H.append("</div>")
H.append("</div>")
return "\n".join(H) return "\n".join(H)