support pdf generation in Python 3

This commit is contained in:
Emmanuel Viennet 2021-07-13 17:00:25 +02:00
parent 6a18188eda
commit 86f728b781
3 changed files with 57 additions and 64 deletions

View File

@ -804,18 +804,7 @@ def formsemestre_bulletinetud(
except: except:
return scu.log_unknown_etud(context, REQUEST, format=format) return scu.log_unknown_etud(context, REQUEST, format=format)
sem = sco_formsemestre.get_formsemestre(context, formsemestre_id) bulletin = do_formsemestre_bulletinetud(
R = []
if format == "html" or format == "pdfmail":
R.append(
_formsemestre_bulletinetud_header_html(
context, etud, etudid, sem, formsemestre_id, format, version, REQUEST
)
)
R.append(
do_formsemestre_bulletinetud(
context, context,
formsemestre_id, formsemestre_id,
etudid, etudid,
@ -826,21 +815,29 @@ def formsemestre_bulletinetud(
prefer_mail_perso=prefer_mail_perso, prefer_mail_perso=prefer_mail_perso,
REQUEST=REQUEST, REQUEST=REQUEST,
)[0] )[0]
) if format not in {"html", "pdfmail"}:
return bulletin
if format == "html" or format == "pdfmail": sem = sco_formsemestre.get_formsemestre(context, formsemestre_id)
R.append("""<p>Situation actuelle: """) H = [
_formsemestre_bulletinetud_header_html(
context, etud, etudid, sem, formsemestre_id, format, version, REQUEST
),
bulletin,
]
H.append("""<p>Situation actuelle: """)
if etud["inscription_formsemestre_id"]: if etud["inscription_formsemestre_id"]:
R.append( H.append(
"""<a class="stdlink" href="formsemestre_status?formsemestre_id=%s">""" """<a class="stdlink" href="formsemestre_status?formsemestre_id=%s">"""
% etud["inscription_formsemestre_id"] % etud["inscription_formsemestre_id"]
) )
R.append(etud["inscriptionstr"]) H.append(etud["inscriptionstr"])
if etud["inscription_formsemestre_id"]: if etud["inscription_formsemestre_id"]:
R.append("""</a>""") H.append("""</a>""")
R.append("""</p>""") H.append("""</p>""")
if sem["modalite"] == "EXT": if sem["modalite"] == "EXT":
R.append( H.append(
"""<p><a """<p><a
href="formsemestre_ext_edit_ue_validations?formsemestre_id=%s&etudid=%s" href="formsemestre_ext_edit_ue_validations?formsemestre_id=%s&etudid=%s"
class="stdlink"> class="stdlink">
@ -849,19 +846,19 @@ def formsemestre_bulletinetud(
% (formsemestre_id, etudid) % (formsemestre_id, etudid)
) )
# Place du diagramme radar # Place du diagramme radar
R.append( H.append(
"""<form id="params"> """<form id="params">
<input type="hidden" name="etudid" id="etudid" value="%s"/> <input type="hidden" name="etudid" id="etudid" value="%s"/>
<input type="hidden" name="formsemestre_id" id="formsemestre_id" value="%s"/> <input type="hidden" name="formsemestre_id" id="formsemestre_id" value="%s"/>
</form>""" </form>"""
% (etudid, formsemestre_id) % (etudid, formsemestre_id)
) )
R.append('<div id="radar_bulletin"></div>') H.append('<div id="radar_bulletin"></div>')
# --- Pied de page # --- Pied de page
R.append(html_sco_header.sco_footer(context, REQUEST)) H.append(html_sco_header.sco_footer(context, REQUEST))
return "".join(R) return "".join(H)
def can_send_bulletin_by_mail(context, formsemestre_id, REQUEST): def can_send_bulletin_by_mail(context, formsemestre_id, REQUEST):

View File

@ -42,14 +42,12 @@ La préférence 'bul_pdf_class_name' est obsolete (inutilisée).
""" """
import time
import collections import collections
import io
import time
import traceback import traceback
try:
from io import StringIO # for Python 3
except ImportError:
from cStringIO import StringIO # for Python 2
import reportlab import reportlab
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Frame, PageBreak from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Frame, PageBreak
from reportlab.platypus import Table, TableStyle, Image, KeepInFrame from reportlab.platypus import Table, TableStyle, Image, KeepInFrame
@ -219,7 +217,7 @@ class BulletinGenerator(object):
else: else:
# Generation du document PDF # Generation du document PDF
sem = sco_formsemestre.get_formsemestre(self.context, formsemestre_id) sem = sco_formsemestre.get_formsemestre(self.context, formsemestre_id)
report = StringIO.StringIO() # in-memory document, no disk file report = io.BytesIO() # in-memory document, no disk file
document = sco_pdf.BaseDocTemplate(report) document = sco_pdf.BaseDocTemplate(report)
document.addPageTemplates( document.addPageTemplates(
sco_pdf.ScolarsPageTemplate( sco_pdf.ScolarsPageTemplate(

View File

@ -77,16 +77,14 @@ DEFAULT_PDF_FOOTER_TEMPLATE = CONFIG.DEFAULT_PDF_FOOTER_TEMPLATE
def SU(s): def SU(s):
"convert s from SCO default encoding to UTF8" "convert s from string to string suitable for ReportLab"
# Mis en service le 4/11/06, passage à ReportLab 2.0 # A priori inutile en Python 3, mais tester les "combining accents"
if not s: return s or ""
s = ""
# Remplace caractères composés # Remplace caractères composés
# eg 'e\xcc\x81' COMBINING ACUTE ACCENT par '\xc3\xa9' LATIN SMALL LETTER E WITH ACUTE # eg 'e\xcc\x81' COMBINING ACUTE ACCENT par '\xc3\xa9' LATIN SMALL LETTER E WITH ACUTE
# car les "combining accents" ne sont pas traités par ReportLab mais peuvent # car les "combining accents" ne sont pas traités par ReportLab mais peuvent
# nous être envoyés par certains navigateurs ou imports # nous être envoyés par certains navigateurs ou imports
u = unicodedata.normalize("NFC", six.text_type(s, SCO_ENCODING, "replace")) # return unicodedata.normalize("NFC", s)
return u.encode("utf8")
def _splitPara(txt): def _splitPara(txt):