Classeur PDF des bulletins BUT courts

This commit is contained in:
Emmanuel Viennet 2023-12-06 20:04:40 +01:00
parent 78fbaf1ac8
commit 0f860f912c
6 changed files with 99 additions and 54 deletions

View File

@ -65,6 +65,38 @@ def bulletin_but(formsemestre_id: int, etudid: int = None, fmt="html"):
) )
if not formsemestre.formation.is_apc(): if not formsemestre.formation.is_apc():
raise ScoValueError("formation non BUT") raise ScoValueError("formation non BUT")
args = _build_bulletin_but_infos(etud, formsemestre, fmt=fmt)
if fmt == "pdf":
filename = scu.bul_filename(formsemestre, etud, prefix="bul-but")
bul_pdf = bulletin_but_court_pdf.make_bulletin_but_court_pdf(args)
return scu.sendPDFFile(bul_pdf, filename + ".pdf")
return render_template(
"but/bulletin_court_page.j2",
datetime=datetime,
sco=ScoData(formsemestre=formsemestre, etud=etud),
time=time,
version="butcourt",
**args,
)
def bulletin_but_court_pdf_frag(etud: Identite, formsemestre: FormSemestre) -> bytes:
"""Le code PDF d'un bulletin BUT court, à intégrer dans un document
(pour les classeurs de tous les bulletins)
"""
args = _build_bulletin_but_infos(etud, formsemestre)
return bulletin_but_court_pdf.make_bulletin_but_court_pdf(args, stand_alone=False)
def _build_bulletin_but_infos(
etud: Identite, formsemestre: FormSemestre, fmt="pdf"
) -> dict:
"""Réuni toutes les information pour le contenu d'un bulletin BUT court.
On indique le format ("html" ou "pdf") car il y a moins d'infos en HTML.
"""
bulletins_sem = BulletinBUT(formsemestre) bulletins_sem = BulletinBUT(formsemestre)
if fmt == "pdf": if fmt == "pdf":
bul: dict = bulletins_sem.bulletin_etud_complet(etud) bul: dict = bulletins_sem.bulletin_etud_complet(etud)
@ -106,16 +138,4 @@ def bulletin_but(formsemestre_id: int, etudid: int = None, fmt="html"):
if ue.type == UE_STANDARD and ue.acronyme in ue_acronyms if ue.type == UE_STANDARD and ue.acronyme in ue_acronyms
], ],
} }
if fmt == "pdf": return args
filename = scu.bul_filename(formsemestre, etud, prefix="bul-but")
bul_pdf = bulletin_but_court_pdf.make_bulletin_but_court_pdf(**args)
return scu.sendPDFFile(bul_pdf, filename + ".pdf")
return render_template(
"but/bulletin_court_page.j2",
datetime=datetime,
sco=ScoData(formsemestre=formsemestre, etud=etud),
time=time,
version="butcourt",
**args,
)

View File

@ -34,6 +34,14 @@ from app.scodoc.sco_preferences import SemPreferences
def make_bulletin_but_court_pdf( def make_bulletin_but_court_pdf(
args: dict,
stand_alone: bool = True,
) -> bytes:
"""génère le bulletin court BUT en pdf.
Si stand_alone, génère un doc pdf complet (une page ici),
sinon un morceau (fragment) à intégrer dans un autre document.
args donne toutes les infos du contenu du bulletin:
bul: dict = None, bul: dict = None,
cursus: cursus_but.EtudCursusBUT = None, cursus: cursus_but.EtudCursusBUT = None,
decision_ues: dict = None, decision_ues: dict = None,
@ -45,14 +53,13 @@ def make_bulletin_but_court_pdf(
title: str = "", title: str = "",
ue_validation_by_niveau: dict[tuple[int, str], ScolarFormSemestreValidation] = None, ue_validation_by_niveau: dict[tuple[int, str], ScolarFormSemestreValidation] = None,
ues_acronyms: list[str] = None, ues_acronyms: list[str] = None,
) -> bytes: """
"génère le bulletin court BUT en pdf"
# A priori ce verrou n'est plus nécessaire avec Flask (multi-process) # A priori ce verrou n'est plus nécessaire avec Flask (multi-process)
# mais... # mais...
try: try:
PDFLOCK.acquire() PDFLOCK.acquire()
bul_generator = BulletinGeneratorBUTCourt(**locals()) bul_generator = BulletinGeneratorBUTCourt(**args)
bul_pdf = bul_generator.generate(fmt="pdf") bul_pdf = bul_generator.generate(fmt="pdf", stand_alone=stand_alone)
finally: finally:
PDFLOCK.release() PDFLOCK.release()
return bul_pdf return bul_pdf

View File

@ -61,7 +61,6 @@ from flask import g, request
from app import log, ScoValueError from app import log, ScoValueError
from app.models import FormSemestre, Identite from app.models import FormSemestre, Identite
from app.scodoc import sco_cache from app.scodoc import sco_cache
from app.scodoc import codes_cursus from app.scodoc import codes_cursus
from app.scodoc import sco_pdf from app.scodoc import sco_pdf
@ -213,17 +212,27 @@ def process_field(
def get_formsemestre_bulletins_pdf(formsemestre_id, version="selectedevals"): def get_formsemestre_bulletins_pdf(formsemestre_id, version="selectedevals"):
"Document pdf avec tous les bulletins du semestre, et filename" "Document pdf avec tous les bulletins du semestre, et filename"
from app.but import bulletin_but_court
from app.scodoc import sco_bulletins from app.scodoc import sco_bulletins
if version not in scu.BULLETINS_VERSIONS: formsemestre: FormSemestre = FormSemestre.query.get_or_404(formsemestre_id)
raise ScoValueError("version de bulletin demandée invalide") versions = (
scu.BULLETINS_VERSIONS_BUT
if formsemestre.formation.is_apc()
else scu.BULLETINS_VERSIONS
)
if version not in versions:
raise ScoValueError("version de bulletin demandée invalide !")
cached = sco_cache.SemBulletinsPDFCache.get(str(formsemestre_id) + "_" + version) cached = sco_cache.SemBulletinsPDFCache.get(str(formsemestre_id) + "_" + version)
if cached: if cached:
return cached[1], cached[0] return cached[1], cached[0]
fragments = [] fragments = []
# Make each bulletin # Make each bulletin
formsemestre: FormSemestre = FormSemestre.query.get_or_404(formsemestre_id)
for etud in formsemestre.get_inscrits(include_demdef=True, order=True): for etud in formsemestre.get_inscrits(include_demdef=True, order=True):
if version == "butcourt":
frag = bulletin_but_court.bulletin_but_court_pdf_frag(etud, formsemestre)
else:
frag, _ = sco_bulletins.do_formsemestre_bulletinetud( frag, _ = sco_bulletins.do_formsemestre_bulletinetud(
formsemestre, formsemestre,
etud, etud,

View File

@ -190,7 +190,7 @@ class SemBulletinsPDFCache(ScoDocCache):
@classmethod @classmethod
def invalidate_sems(cls, formsemestre_ids): def invalidate_sems(cls, formsemestre_ids):
"""Clear cached pdf for all given formsemestres""" """Clear cached pdf for all given formsemestres"""
for version in scu.BULLETINS_VERSIONS: for version in scu.BULLETINS_VERSIONS_BUT:
oids = [ oids = [
str(formsemestre_id) + "_" + version str(formsemestre_id) + "_" + version
for formsemestre_id in formsemestre_ids for formsemestre_id in formsemestre_ids

View File

@ -157,11 +157,24 @@ def formsemestre_recapcomplet(
H.append(f'<option value="{fmt}"{selected}>{label}</option>') H.append(f'<option value="{fmt}"{selected}>{label}</option>')
H.append( H.append(
f""" f"""
</select>&nbsp;(cliquer sur un nom pour afficher son bulletin ou </select>&nbsp;<span class="help">cliquer sur un nom pour afficher son bulletin ou
<a class="stdlink" <a class="stdlink"
href="{url_for('notes.formsemestre_bulletins_pdf', href="{url_for('notes.formsemestre_bulletins_pdf',
scodoc_dept=g.scodoc_dept, formsemestre_id=formsemestre_id) scodoc_dept=g.scodoc_dept, formsemestre_id=formsemestre_id)
}">ici avoir le classeur papier</a>) }">ici avoir le classeur pdf</a>
"""
)
if formsemestre.formation.is_apc():
H.append(
f"""&nbsp;ou en <a class="stdlink"
href="{url_for('notes.formsemestre_bulletins_pdf',
scodoc_dept=g.scodoc_dept, formsemestre_id=formsemestre_id, version="butcourt")
}">version courte BUT</a>
"""
)
H.append(
"""</span>
</form> </form>
""" """
) )

View File

@ -271,9 +271,7 @@ sco_publish(
) )
@bp.route( @bp.route("/formsemestre_bulletinetud")
"/formsemestre_bulletinetud", methods=["GET", "POST"]
) # POST pour compat anciens clients PHP (deprecated)
@scodoc @scodoc
@permission_required_compat_scodoc7(Permission.ScoView) @permission_required_compat_scodoc7(Permission.ScoView)
@scodoc7func @scodoc7func
@ -1863,8 +1861,6 @@ sco_publish(
@scodoc7func @scodoc7func
def formsemestre_bulletins_pdf(formsemestre_id, version="selectedevals"): def formsemestre_bulletins_pdf(formsemestre_id, version="selectedevals"):
"Publie les bulletins dans un classeur PDF" "Publie les bulletins dans un classeur PDF"
if version not in scu.BULLETINS_VERSIONS:
raise ScoValueError("version de bulletin demandée invalide")
pdfdoc, filename = sco_bulletins_pdf.get_formsemestre_bulletins_pdf( pdfdoc, filename = sco_bulletins_pdf.get_formsemestre_bulletins_pdf(
formsemestre_id, version=version formsemestre_id, version=version
) )