From 36a5c15e3a11bf3cb1835c7c003433c23058304a Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Mon, 28 Aug 2023 17:02:21 +0200 Subject: [PATCH] =?UTF-8?q?am=C3=A9liore=20l=C3=A9g=C3=A8rement=20le=20tra?= =?UTF-8?q?itement=20des=20exceptions=20pdf?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/scodoc/sco_bulletins_standard.py | 70 ++++++++++++++++++---------- app/scodoc/sco_pdf.py | 9 ++-- 2 files changed, 51 insertions(+), 28 deletions(-) diff --git a/app/scodoc/sco_bulletins_standard.py b/app/scodoc/sco_bulletins_standard.py index 83b33a0e..0039dba5 100644 --- a/app/scodoc/sco_bulletins_standard.py +++ b/app/scodoc/sco_bulletins_standard.py @@ -46,16 +46,20 @@ de la forme %(XXX)s sont remplacées par la valeur de XXX, pour XXX dans: Balises img: actuellement interdites. """ -from flask import url_for, g - -from reportlab.platypus import KeepTogether, Paragraph, Spacer, Table -from reportlab.lib.units import cm, mm +from flask import g, url_for from reportlab.lib.colors import Color, blue +from reportlab.lib.units import cm, mm +from reportlab.platypus import KeepTogether, Paragraph, Spacer, Table import app.scodoc.sco_utils as scu -from app.scodoc.sco_pdf import SU, make_paras -from app.scodoc import sco_preferences -from app.scodoc.sco_permissions import Permission +from app.scodoc import ( + gen_tables, + sco_bulletins_generator, + sco_bulletins_pdf, + sco_evaluations, + sco_groups, + sco_preferences, +) from app.scodoc.codes_cursus import ( UE_COLORS, UE_DEFAULT_COLOR, @@ -63,17 +67,17 @@ from app.scodoc.codes_cursus import ( UE_SPORT, UE_STANDARD, ) -from app.scodoc import sco_bulletins_generator -from app.scodoc import sco_bulletins_pdf -from app.scodoc import sco_groups -from app.scodoc import sco_evaluations -from app.scodoc import gen_tables +from app.scodoc.sco_exceptions import ScoPDFFormatError +from app.scodoc.sco_pdf import SU, make_paras +from app.scodoc.sco_permissions import Permission # Important: Le nom de la classe ne doit pas changer (bien le choisir), # car il sera stocké en base de données (dans les préférences) class BulletinGeneratorStandard(sco_bulletins_generator.BulletinGenerator): - description = "standard ScoDoc (version 2011)" # la description doit être courte: elle apparait dans le menu de paramètrage ScoDoc + "Les bulletins standards" + # la description doit être courte: elle apparait dans le menu de paramètrage ScoDoc + description = "standard ScoDoc (version 2011)" supported_formats = ["html", "pdf"] def bul_title_pdf(self) -> list: @@ -158,31 +162,47 @@ class BulletinGeneratorStandard(sco_bulletins_generator.BulletinGenerator): H.append('
') for app in self.infos["appreciations_list"]: if can_edit_app: - mlink = ( - 'modifier supprimer' - % (app["id"], app["id"]) - ) + mlink = f"""modifier + supprimer""" else: mlink = "" H.append( - '

%s%s%s

' - % (app["date"], app["comment"], mlink) + f"""

{app["date"]}{ + app["comment"]}{mlink} +

""" ) if can_edit_app: H.append( - '

Ajouter une appréciation

' + f"""

Ajouter une appréciation

""" % self.infos ) H.append("
") # Appréciations sur PDF: if self.infos.get("appreciations_list", False): story.append(Spacer(1, 3 * mm)) - story.append( - Paragraph( - SU("Appréciation : " + "\n".join(self.infos["appreciations_txt"])), - self.CellStyle, + try: + story.append( + Paragraph( + SU( + "Appréciation : " + + "\n".join(self.infos["appreciations_txt"]) + ), + self.CellStyle, + ) ) - ) + except AttributeError as exc: + raise ScoPDFFormatError( + "Appréciation invalide bloquant la génération du pdf" + ) from exc # ----- DECISION JURY if self.preferences["bul_show_decision"]: diff --git a/app/scodoc/sco_pdf.py b/app/scodoc/sco_pdf.py index 841f8b3f..631d71b0 100755 --- a/app/scodoc/sco_pdf.py +++ b/app/scodoc/sco_pdf.py @@ -55,10 +55,10 @@ from reportlab.lib import styles from flask import g +from app import log +from app.scodoc.sco_exceptions import ScoGenError, ScoPDFFormatError, ScoValueError from app.scodoc import sco_utils as scu from app.scodoc.sco_utils import CONFIG -from app import log -from app.scodoc.sco_exceptions import ScoGenError, ScoValueError import sco_version PAGE_HEIGHT = defaultPageSize[1] @@ -136,7 +136,10 @@ def make_paras(txt: str, style, suppress_empty=False) -> list[Paragraph]: if m.group(1): # non empty paragraph r.append(para) paras = r - result = [Paragraph(SU(s), style) for s in paras] + try: + result = [Paragraph(SU(s), style) for s in paras] + except AttributeError as exc: + raise ScoPDFFormatError("PDF: paragraphe avec balisage invalide") from exc except OSError as e: msg = str(e) # If a file is missing, try to display the invalid name