From 58b831513dc350ca78c6f8e9122e99a574c8705b Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Wed, 10 Apr 2024 15:29:30 +0200 Subject: [PATCH] =?UTF-8?q?Am=C3=A9liore=20traitement=20des=20erreurs=20lo?= =?UTF-8?q?rs=20de=20la=20g=C3=A9n=C3=A9ration=20des=20PDF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/scodoc/gen_tables.py | 6 +++++- app/scodoc/sco_bulletins_generator.py | 12 ++++++++++-- app/scodoc/sco_exceptions.py | 2 +- app/scodoc/sco_pdf.py | 7 ++++++- app/scodoc/sco_pv_lettres_inviduelles.py | 23 ++++++++++++++--------- app/scodoc/sco_pv_pdf.py | 6 +++++- app/scodoc/sco_trombino.py | 13 +++++++++---- app/scodoc/sco_trombino_tours.py | 11 ++++++++--- 8 files changed, 58 insertions(+), 22 deletions(-) diff --git a/app/scodoc/gen_tables.py b/app/scodoc/gen_tables.py index efe7a32d..3c08326c 100644 --- a/app/scodoc/gen_tables.py +++ b/app/scodoc/gen_tables.py @@ -48,6 +48,7 @@ from typing import Any from urllib.parse import urlparse, urlencode, parse_qs, urlunparse from openpyxl.utils import get_column_letter +import reportlab from reportlab.platypus import Paragraph, Spacer from reportlab.platypus import Table, KeepInFrame from reportlab.lib.colors import Color @@ -812,7 +813,10 @@ if __name__ == "__main__": document, ) ) - document.build(objects) + try: + document.build(objects) + except (ValueError, KeyError, reportlab.platypus.doctemplate.LayoutError) as exc: + raise ScoPDFFormatError(str(exc)) from exc data = doc.getvalue() with open("/tmp/gen_table.pdf", "wb") as f: f.write(data) diff --git a/app/scodoc/sco_bulletins_generator.py b/app/scodoc/sco_bulletins_generator.py index b8a00f9b..4fbeb982 100644 --- a/app/scodoc/sco_bulletins_generator.py +++ b/app/scodoc/sco_bulletins_generator.py @@ -61,7 +61,7 @@ from flask_login import current_user from app.models import FormSemestre, Identite, ScoDocSiteConfig from app.scodoc import sco_utils as scu -from app.scodoc.sco_exceptions import NoteProcessError +from app.scodoc.sco_exceptions import NoteProcessError, ScoPDFFormatError from app import log from app.scodoc import sco_formsemestre from app.scodoc import sco_pdf @@ -228,7 +228,15 @@ class BulletinGenerator: preferences=sco_preferences.SemPreferences(formsemestre_id), ) ) - document.build(story) + try: + document.build(story) + except ( + ValueError, + KeyError, + reportlab.platypus.doctemplate.LayoutError, + ) as exc: + raise ScoPDFFormatError(str(exc)) from exc + data = report.getvalue() return data diff --git a/app/scodoc/sco_exceptions.py b/app/scodoc/sco_exceptions.py index 23552c5c..aa2d04d0 100644 --- a/app/scodoc/sco_exceptions.py +++ b/app/scodoc/sco_exceptions.py @@ -103,7 +103,7 @@ class ScoPDFFormatError(ScoValueError): super().__init__( f"""Erreur dans un format pdf:

{msg}

-

Vérifiez les paramètres (polices de caractères, balisage) +

Vérifiez les paramètres (polices de caractères, balisage, réglages bulletins...) dans les paramètres ou préférences.

""", diff --git a/app/scodoc/sco_pdf.py b/app/scodoc/sco_pdf.py index 02d6af7a..02cd6e87 100755 --- a/app/scodoc/sco_pdf.py +++ b/app/scodoc/sco_pdf.py @@ -458,7 +458,12 @@ def pdf_basic_page( if title: head = Paragraph(SU(title), StyleSheet["Heading3"]) objects = [head] + objects - document.build(objects) + + try: + document.build(objects) + except (ValueError, KeyError, reportlab.platypus.doctemplate.LayoutError) as exc: + raise ScoPDFFormatError(str(exc)) from exc + data = report.getvalue() return data diff --git a/app/scodoc/sco_pv_lettres_inviduelles.py b/app/scodoc/sco_pv_lettres_inviduelles.py index e5bf055c..c3d069e7 100644 --- a/app/scodoc/sco_pv_lettres_inviduelles.py +++ b/app/scodoc/sco_pv_lettres_inviduelles.py @@ -50,7 +50,7 @@ from app.scodoc import sco_bulletins_pdf from app.scodoc import sco_pv_dict from app.scodoc import sco_pdf from app.scodoc import sco_preferences -from app.scodoc.sco_exceptions import ScoValueError +from app.scodoc.sco_exceptions import ScoPDFFormatError, ScoValueError from app.scodoc.sco_cursus_dut import SituationEtudCursus from app.scodoc.sco_pv_templates import CourrierIndividuelTemplate, jury_titres import sco_version @@ -132,7 +132,11 @@ def pdf_lettres_individuelles( ) ) - document.build(objects) + try: + document.build(objects) + except (ValueError, KeyError, reportlab.platypus.doctemplate.LayoutError) as exc: + raise ScoPDFFormatError(str(exc)) from exc + data = report.getvalue() return data @@ -241,13 +245,14 @@ def pdf_lettre_individuelle(sem, decision, etud: Identite, params, signature=Non titre_jury_court = "s" else: titre_jury_court = "" - params[ - "autorisations_txt" - ] = """Vous êtes autorisé%s à continuer dans le%s semestre%s : %s""" % ( - etud.e, - titre_jury_court, - titre_jury_court, - decision["autorisations_descr"], + params["autorisations_txt"] = ( + """Vous êtes autorisé%s à continuer dans le%s semestre%s : %s""" + % ( + etud.e, + titre_jury_court, + titre_jury_court, + decision["autorisations_descr"], + ) ) else: params["autorisations_txt"] = "" diff --git a/app/scodoc/sco_pv_pdf.py b/app/scodoc/sco_pv_pdf.py index bc398128..ef72180e 100644 --- a/app/scodoc/sco_pv_pdf.py +++ b/app/scodoc/sco_pv_pdf.py @@ -126,7 +126,11 @@ def pvjury_pdf( ) ) - document.build(objects) + try: + document.build(objects) + except (ValueError, KeyError, reportlab.platypus.doctemplate.LayoutError) as exc: + raise ScoPDFFormatError(str(exc)) from exc + data = report.getvalue() return data diff --git a/app/scodoc/sco_trombino.py b/app/scodoc/sco_trombino.py index 1ed5081f..7431609b 100644 --- a/app/scodoc/sco_trombino.py +++ b/app/scodoc/sco_trombino.py @@ -47,12 +47,11 @@ from app import db, log from app.models import Identite import app.scodoc.sco_utils as scu from app.scodoc.TrivialFormulator import TrivialFormulator -from app.scodoc.sco_exceptions import ScoValueError +from app.scodoc.sco_exceptions import ScoPDFFormatError, ScoValueError from app.scodoc.sco_pdf import SU from app.scodoc import html_sco_header from app.scodoc import htmlutils from app.scodoc import sco_import_etuds -from app.scodoc import sco_etud from app.scodoc import sco_excel from app.scodoc import sco_groups_view from app.scodoc import sco_pdf @@ -388,7 +387,10 @@ def _trombino_pdf(groups_infos): preferences=sco_preferences.SemPreferences(sem["formsemestre_id"]), ) ) - document.build(objects) + try: + document.build(objects) + except (ValueError, KeyError, reportlab.platypus.doctemplate.LayoutError) as exc: + raise ScoPDFFormatError(str(exc)) from exc report.seek(0) return send_file( report, @@ -465,7 +467,10 @@ def _listeappel_photos_pdf(groups_infos): preferences=sco_preferences.SemPreferences(sem["formsemestre_id"]), ) ) - document.build(objects) + try: + document.build(objects) + except (ValueError, KeyError, reportlab.platypus.doctemplate.LayoutError) as exc: + raise ScoPDFFormatError(str(exc)) from exc data = report.getvalue() return scu.sendPDFFile(data, filename) diff --git a/app/scodoc/sco_trombino_tours.py b/app/scodoc/sco_trombino_tours.py index ddec2349..dc565cf0 100644 --- a/app/scodoc/sco_trombino_tours.py +++ b/app/scodoc/sco_trombino_tours.py @@ -31,7 +31,7 @@ """ import io - +import reportlab from reportlab.lib import colors from reportlab.lib.colors import black from reportlab.lib.pagesizes import A4, A3 @@ -277,10 +277,12 @@ def pdf_trombino_tours( preferences=sco_preferences.SemPreferences(), ) ) + try: document.build(objects) - except (ValueError, KeyError) as exc: + except (ValueError, KeyError, reportlab.platypus.doctemplate.LayoutError) as exc: raise ScoPDFFormatError(str(exc)) from exc + data = report.getvalue() return scu.sendPDFFile(data, filename) @@ -470,7 +472,10 @@ def pdf_feuille_releve_absences( preferences=sco_preferences.SemPreferences(), ) ) - document.build(objects) + try: + document.build(objects) + except (ValueError, KeyError, reportlab.platypus.doctemplate.LayoutError) as exc: + raise ScoPDFFormatError(str(exc)) from exc data = report.getvalue() return scu.sendPDFFile(data, filename)