Envoi bulletins PDF par mail.

This commit is contained in:
Emmanuel Viennet 2022-03-22 19:18:25 +01:00
parent 8f8472eb3e
commit 2e2338af63
3 changed files with 31 additions and 44 deletions

View File

@ -32,7 +32,7 @@ import email
import time import time
from flask import g, request from flask import g, request
from flask import render_template, url_for from flask import flash, render_template, url_for
from flask_login import current_user from flask_login import current_user
from app import email from app import email
@ -817,7 +817,8 @@ def formsemestre_bulletinetud(
if format not in {"html", "pdfmail"}: if format not in {"html", "pdfmail"}:
filename = scu.bul_filename(formsemestre, etud, format) filename = scu.bul_filename(formsemestre, etud, format)
return scu.send_file(bulletin, filename, mime=scu.get_mime_suffix(format)[0]) return scu.send_file(bulletin, filename, mime=scu.get_mime_suffix(format)[0])
elif format == "pdfmail":
return ""
H = [ H = [
_formsemestre_bulletinetud_header_html(etud, formsemestre, format, version), _formsemestre_bulletinetud_header_html(etud, formsemestre, format, version),
bulletin, bulletin,
@ -851,7 +852,6 @@ def do_formsemestre_bulletinetud(
etudid: int, etudid: int,
version="long", # short, long, selectedevals version="long", # short, long, selectedevals
format=None, format=None,
nohtml=False,
xml_with_decisions=False, # force décisions dans XML xml_with_decisions=False, # force décisions dans XML
force_publishing=False, # force publication meme si semestre non publié sur "portail" force_publishing=False, # force publication meme si semestre non publié sur "portail"
prefer_mail_perso=False, # mails envoyés sur adresse perso si non vide prefer_mail_perso=False, # mails envoyés sur adresse perso si non vide
@ -918,13 +918,6 @@ def do_formsemestre_bulletinetud(
if not can_send_bulletin_by_mail(formsemestre.id): if not can_send_bulletin_by_mail(formsemestre.id):
raise AccessDenied("Vous n'avez pas le droit d'effectuer cette opération !") raise AccessDenied("Vous n'avez pas le droit d'effectuer cette opération !")
if nohtml:
htm = "" # speed up if html version not needed
else:
htm, _ = sco_bulletins_generator.make_formsemestre_bulletinetud(
I, version=version, format="html"
)
pdfdata, filename = sco_bulletins_generator.make_formsemestre_bulletinetud( pdfdata, filename = sco_bulletins_generator.make_formsemestre_bulletinetud(
I, version=version, format="pdf" I, version=version, format="pdf"
) )
@ -935,27 +928,14 @@ def do_formsemestre_bulletinetud(
recipient_addr = etud.get("email", "") or etud.get("emailperso", "") recipient_addr = etud.get("email", "") or etud.get("emailperso", "")
if not recipient_addr: if not recipient_addr:
if nohtml: flash(f"{etud['nomprenom']} n'a pas d'adresse e-mail !")
h = "" # permet de compter les non-envois return False, I["filigranne"]
else: else:
h = (
"<div class=\"boldredmsg\">%s n'a pas d'adresse e-mail !</div>"
% etud["nomprenom"]
) + htm
return h, I["filigranne"]
#
mail_bulletin(formsemestre.id, I, pdfdata, filename, recipient_addr) mail_bulletin(formsemestre.id, I, pdfdata, filename, recipient_addr)
emaillink = '<a class="stdlink" href="mailto:%s">%s</a>' % ( flash(f"mail envoyé à {recipient_addr}")
recipient_addr,
recipient_addr, return True, I["filigranne"]
)
return (
('<div class="head_message">Message mail envoyé à %s</div>' % (emaillink))
+ htm,
I["filigranne"],
)
else:
raise ValueError("do_formsemestre_bulletinetud: invalid format (%s)" % format) raise ValueError("do_formsemestre_bulletinetud: invalid format (%s)" % format)

View File

@ -49,6 +49,7 @@ Balises img: actuellement interdites.
from reportlab.platypus import KeepTogether, Paragraph, Spacer, Table from reportlab.platypus import KeepTogether, Paragraph, Spacer, Table
from reportlab.lib.units import cm, mm from reportlab.lib.units import cm, mm
from reportlab.lib.colors import Color, blue from reportlab.lib.colors import Color, blue
from app.models import FormSemestre
from app.scodoc.sco_exceptions import ScoBugCatcher from app.scodoc.sco_exceptions import ScoBugCatcher
import app.scodoc.sco_utils as scu import app.scodoc.sco_utils as scu
@ -271,7 +272,7 @@ class BulletinGeneratorStandard(sco_bulletins_generator.BulletinGenerator):
) )
def build_bulletin_table(self): def build_bulletin_table(self):
"""Génère la table centrale du bulletin de notes """Génère la table centrale du bulletin de notes classique (pas BUT)
Renvoie: col_keys, P, pdf_style, col_widths Renvoie: col_keys, P, pdf_style, col_widths
- col_keys: nom des colonnes de la table (clés) - col_keys: nom des colonnes de la table (clés)
- table: liste de dicts de chaines de caractères - table: liste de dicts de chaines de caractères
@ -417,10 +418,7 @@ class BulletinGeneratorStandard(sco_bulletins_generator.BulletinGenerator):
# Chaque UE: # Chaque UE:
for ue in I["ues"]: for ue in I["ues"]:
ue_type = None ue_type = None
try:
coef_ue = ue["coef_ue_txt"] if prefs["bul_show_ue_coef"] else "" coef_ue = ue["coef_ue_txt"] if prefs["bul_show_ue_coef"] else ""
except TypeError as exc:
raise ScoBugCatcher(f"ue={ue!r}") from exc
ue_descr = ue["ue_descr_txt"] ue_descr = ue["ue_descr_txt"]
rowstyle = "" rowstyle = ""

View File

@ -36,7 +36,7 @@ import time
from xml.etree import ElementTree from xml.etree import ElementTree
import flask import flask
from flask import abort, flash, jsonify, render_template, url_for from flask import abort, flash, jsonify, redirect, render_template, url_for
from flask import current_app, g, request from flask import current_app, g, request
from flask_login import current_user from flask_login import current_user
from werkzeug.utils import redirect from werkzeug.utils import redirect
@ -359,7 +359,7 @@ def formsemestre_bulletinetud(
) )
if format == "oldjson": if format == "oldjson":
format = "json" format = "json"
return sco_bulletins.formsemestre_bulletinetud( r = sco_bulletins.formsemestre_bulletinetud(
etud, etud,
formsemestre_id=formsemestre_id, formsemestre_id=formsemestre_id,
format=format, format=format,
@ -368,6 +368,16 @@ def formsemestre_bulletinetud(
force_publishing=force_publishing, force_publishing=force_publishing,
prefer_mail_perso=prefer_mail_perso, prefer_mail_perso=prefer_mail_perso,
) )
if format == "pdfmail":
return redirect(
url_for(
"notes.formsemestre_bulletinetud",
scodoc_dept=g.scodoc_dept,
etudid=etud.id,
formsemestre_id=formsemestre_id,
)
)
return r
sco_publish( sco_publish(
@ -1955,23 +1965,22 @@ def formsemestre_bulletins_mailetuds(
) )
# Make each bulletin # Make each bulletin
nb_send = 0 nb_sent = 0
for etudid in etudids: for etudid in etudids:
h, _ = sco_bulletins.do_formsemestre_bulletinetud( sent, _ = sco_bulletins.do_formsemestre_bulletinetud(
formsemestre, formsemestre,
etudid, etudid,
version=version, version=version,
prefer_mail_perso=prefer_mail_perso, prefer_mail_perso=prefer_mail_perso,
format="pdfmail", format="pdfmail",
nohtml=True,
) )
if h: if sent:
nb_send += 1 nb_sent += 1
# #
return ( return (
html_sco_header.sco_header() html_sco_header.sco_header()
+ '<p>%d bulletins sur %d envoyés par mail !</p><p><a class="stdlink" href="formsemestre_status?formsemestre_id=%s">continuer</a></p>' + '<p>%d bulletins sur %d envoyés par mail !</p><p><a class="stdlink" href="formsemestre_status?formsemestre_id=%s">continuer</a></p>'
% (nb_send, len(etudids), formsemestre_id) % (nb_sent, len(etudids), formsemestre_id)
+ html_sco_header.sco_footer() + html_sco_header.sco_footer()
) )