From 0c1c6de7bce2c9d4bf78f36c558979c4ede73fb7 Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Sun, 13 Mar 2022 22:02:30 +0100 Subject: [PATCH] Bulletins PDF: nouvelle gestion des bookmarks et filigranes --- app/scodoc/sco_bulletins_generator.py | 11 ++++-- app/scodoc/sco_bulletins_pdf.py | 21 ++++------- app/scodoc/sco_exceptions.py | 5 +-- app/scodoc/sco_pdf.py | 52 +++++++++++++-------------- app/static/css/scodoc.css | 4 +-- app/templates/bul_head.html | 2 +- 6 files changed, 46 insertions(+), 49 deletions(-) diff --git a/app/scodoc/sco_bulletins_generator.py b/app/scodoc/sco_bulletins_generator.py index 23e6751f31..219741b941 100644 --- a/app/scodoc/sco_bulletins_generator.py +++ b/app/scodoc/sco_bulletins_generator.py @@ -154,10 +154,13 @@ class BulletinGenerator: from app.scodoc import sco_preferences formsemestre_id = self.infos["formsemestre_id"] + marque_debut_bulletin = sco_pdf.DebutBulletin( + self.infos["etud"]["nomprenom"], filigranne=self.infos["filigranne"] + ) objects = [] # partie haute du bulletin objects += self.bul_title_pdf() # pylint: disable=no-member - objects += [sco_pdf.DebutBulletin(self.infos["etud"]["nomprenom"])] + index_obj_debut = len(objects) # table des notes objects += self.bul_table(format="pdf") # pylint: disable=no-member @@ -165,10 +168,12 @@ class BulletinGenerator: objects += self.bul_part_below(format="pdf") # pylint: disable=no-member # signatures objects += self.bul_signatures_pdf() # pylint: disable=no-member - if self.scale_table_in_page: # Réduit sur une page - objects = [KeepInFrame(0, 0, objects, mode="shrink")] + objects = [marque_debut_bulletin, KeepInFrame(0, 0, objects, mode="shrink")] + else: + # Insere notre marqueur qui permet de générer les bookmarks et filigrannes: + objects.insert(index_obj_debut, marque_debut_bulletin) # # objects.append(sco_pdf.FinBulletin()) if not stand_alone: diff --git a/app/scodoc/sco_bulletins_pdf.py b/app/scodoc/sco_bulletins_pdf.py index d51a34ebdd..92eabf2367 100644 --- a/app/scodoc/sco_bulletins_pdf.py +++ b/app/scodoc/sco_bulletins_pdf.py @@ -77,7 +77,7 @@ def assemble_bulletins_pdf( story: list, bul_title: str, infos, - pagesbookmarks, + pagesbookmarks=None, filigranne=None, server_name="", ): @@ -106,8 +106,7 @@ def assemble_bulletins_pdf( preferences=sco_preferences.SemPreferences(formsemestre_id), ) ) - document.build(story) - # document.multiBuild(story) + document.multiBuild(story) data = report.getvalue() return data @@ -173,7 +172,7 @@ def process_field(field, cdict, style, suppress_empty_pars=False, format="pdf"): def get_formsemestre_bulletins_pdf(formsemestre_id, version="selectedevals"): - "document pdf et filename" + "Document pdf avec tous les bulletins du semestre, et filename" from app.scodoc import sco_bulletins cached = sco_cache.SemBulletinsPDFCache.get(str(formsemestre_id) + "_" + version) @@ -182,20 +181,14 @@ def get_formsemestre_bulletins_pdf(formsemestre_id, version="selectedevals"): fragments = [] # Make each bulletin formsemestre: FormSemestre = FormSemestre.query.get_or_404(formsemestre_id) - bookmarks = {} - filigrannes = {} - i = 1 for etud in formsemestre.get_inscrits(include_demdef=True, order=True): - frag, filigranne = sco_bulletins.do_formsemestre_bulletinetud( + frag, _ = sco_bulletins.do_formsemestre_bulletinetud( formsemestre, etud.id, format="pdfpart", version=version, ) fragments += frag - filigrannes[i] = filigranne - bookmarks[i] = etud.sex_nom(no_accents=True) - i = i + 1 # infos = {"DeptName": sco_preferences.get_preference("DeptName", formsemestre_id)} if request: @@ -209,15 +202,13 @@ def get_formsemestre_bulletins_pdf(formsemestre_id, version="selectedevals"): fragments, formsemestre.titre_mois(), infos, - bookmarks, - filigranne=filigrannes, server_name=server_name, ) finally: sco_pdf.PDFLOCK.release() # - dt = time.strftime("%Y-%m-%d") - filename = "bul-%s-%s.pdf" % (formsemestre.titre_num(), dt) + date_iso = time.strftime("%Y-%m-%d") + filename = "bul-%s-%s.pdf" % (formsemestre.titre_num(), date_iso) filename = scu.unescape_html(filename).replace(" ", "_").replace("&", "") # fill cache sco_cache.SemBulletinsPDFCache.set( diff --git a/app/scodoc/sco_exceptions.py b/app/scodoc/sco_exceptions.py index 112658e658..394dbf1e5d 100644 --- a/app/scodoc/sco_exceptions.py +++ b/app/scodoc/sco_exceptions.py @@ -112,8 +112,9 @@ class ScoNonEmptyFormationObject(ScoValueError): class ScoInvalidIdType(ScoValueError): - """Pour les clients qui s'obstinnent à utiliser des bookmarks ou - historiques anciens avec des ID ScoDoc7""" + """Pour les clients qui s'obstinent à utiliser des bookmarks + ou historiques anciens avec des ID ScoDoc7. + """ def __init__(self, msg=""): import app.scodoc.sco_utils as scu diff --git a/app/scodoc/sco_pdf.py b/app/scodoc/sco_pdf.py index 4f674e6c98..ac1338aadc 100755 --- a/app/scodoc/sco_pdf.py +++ b/app/scodoc/sco_pdf.py @@ -190,8 +190,9 @@ class DebutBulletin(BulMarker): Element vide utilisé pour générer les bookmarks """ - def __init__(self, bookmark=None): + def __init__(self, bookmark=None, filigranne=None): self.bookmark = bookmark + self.filigranne = filigranne super().__init__() @@ -212,19 +213,17 @@ class ScoDocPageTemplate(PageTemplate): preferences=None, # dictionnary with preferences, required ): """Initialise our page template.""" - from app.scodoc.sco_logos import ( - find_logo, - ) # defered import (solve circular dependency ->sco_logo ->scodoc, ->sco_pdf + # defered import (solve circular dependency ->sco_logo ->scodoc, ->sco_pdf + from app.scodoc.sco_logos import find_logo self.preferences = preferences - self.pagesbookmarks = pagesbookmarks + self.pagesbookmarks = pagesbookmarks or {} self.pdfmeta_author = author self.pdfmeta_title = title self.pdfmeta_subject = subject self.server_name = server_name self.filigranne = filigranne self.page_number = 1 - self.current_page_bookmark = None self.footer_template = footer_template if self.preferences: self.with_page_background = self.preferences["bul_pdf_with_background"] @@ -294,16 +293,11 @@ class ScoDocPageTemplate(PageTemplate): canvas.setTitle(SU(self.pdfmeta_title)) if self.pdfmeta_subject: canvas.setSubject(SU(self.pdfmeta_subject)) - # if self.current_page_bookmark: - # canvas.bookmarkPage(self.current_page_bookmark) - # canvas.addOutlineEntry( - # SU(self.current_page_bookmark), self.current_page_bookmark - # ) - # else: XXX A REMETTRE - # bookmark = self.pagesbookmarks.get(doc.page, None) - # if bookmark != None: - # canvas.bookmarkPage(bookmark) - # canvas.addOutlineEntry(SU(bookmark), bookmark) + + bookmark = self.pagesbookmarks.get(doc.page, None) + if bookmark: + canvas.bookmarkPage(bookmark) + canvas.addOutlineEntry(SU(bookmark), bookmark) # ---- Footer canvas.setFont( self.preferences["SCOLAR_FONT"], self.preferences["SCOLAR_FONT_SIZE_FOOT"] @@ -323,26 +317,30 @@ class ScoDocPageTemplate(PageTemplate): if not self.preferences: return # ---- Filigranne (texte en diagonal en haut a gauche de chaque page) - if self.filigranne: + filigranne = None + if hasattr(doc, "filigranne"): + # filigranne crée par DebutBulletin + filigranne = doc.filigranne + if not filigranne and self.filigranne: if isinstance(self.filigranne, str): filigranne = self.filigranne # same for all pages else: filigranne = self.filigranne.get(doc.page, None) - if filigranne: - canvas.saveState() - canvas.translate(9 * cm, 27.6 * cm) - canvas.rotate(30) - canvas.scale(4.5, 4.5) - canvas.setFillColorRGB(1.0, 0.65, 0.65, alpha=0.6) - canvas.drawRightString(0, 0, SU(filigranne)) - canvas.restoreState() + if filigranne: + canvas.saveState() + canvas.translate(9 * cm, 27.6 * cm) + canvas.rotate(30) + canvas.scale(4.5, 4.5) + canvas.setFillColorRGB(1.0, 0.65, 0.65, alpha=0.6) + canvas.drawRightString(0, 0, SU(filigranne)) + canvas.restoreState() + doc.filigranne = None def afterPage(self): """Called after all flowables have been drawn on a page. Increment pageNum since the page has been completed. """ self.page_number += 1 - self.current_page_bookmark = None class BulletinDocTemplate(BaseDocTemplate): @@ -359,6 +357,8 @@ class BulletinDocTemplate(BaseDocTemplate): self.canv.addOutlineEntry( SU(flowable.bookmark), flowable.bookmark, level=0, closed=None ) + if flowable.filigranne: + self.filigranne = flowable.filigranne # log(f"afterFlowable addOutlineEntry {flowable.bookmark} page {self.page}") diff --git a/app/static/css/scodoc.css b/app/static/css/scodoc.css index b5f2b325b3..eab9bb127d 100644 --- a/app/static/css/scodoc.css +++ b/app/static/css/scodoc.css @@ -288,13 +288,13 @@ div.logo-insidebar { } div.logo-logo { margin-left: -5px; - text-align: center ; + text-align: center ; } div.logo-logo img { box-sizing: content-box; margin-top: 10px; /* -10px */ - width: 135px; /* 128px */ + width: 80px; /* adapter suivant image */ padding-right: 5px; } div.sidebar-bottom { diff --git a/app/templates/bul_head.html b/app/templates/bul_head.html index d706ef9ae5..473393802b 100644 --- a/app/templates/bul_head.html +++ b/app/templates/bul_head.html @@ -7,7 +7,7 @@ {% if not is_apc %}

{{etud.nomprenom}}

{% endif %}