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
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 app import email
@ -817,7 +817,8 @@ def formsemestre_bulletinetud(
if format not in {"html", "pdfmail"}:
filename = scu.bul_filename(formsemestre, etud, format)
return scu.send_file(bulletin, filename, mime=scu.get_mime_suffix(format)[0])
elif format == "pdfmail":
return ""
H = [
_formsemestre_bulletinetud_header_html(etud, formsemestre, format, version),
bulletin,
@ -851,7 +852,6 @@ def do_formsemestre_bulletinetud(
etudid: int,
version="long", # short, long, selectedevals
format=None,
nohtml=False,
xml_with_decisions=False, # force décisions dans XML
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
@ -918,13 +918,6 @@ def do_formsemestre_bulletinetud(
if not can_send_bulletin_by_mail(formsemestre.id):
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(
I, version=version, format="pdf"
)
@ -935,28 +928,15 @@ def do_formsemestre_bulletinetud(
recipient_addr = etud.get("email", "") or etud.get("emailperso", "")
if not recipient_addr:
if nohtml:
h = "" # permet de compter les non-envois
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)
emaillink = '<a class="stdlink" href="mailto:%s">%s</a>' % (
recipient_addr,
recipient_addr,
)
return (
('<div class="head_message">Message mail envoyé à %s</div>' % (emaillink))
+ htm,
I["filigranne"],
)
flash(f"{etud['nomprenom']} n'a pas d'adresse e-mail !")
return False, I["filigranne"]
else:
mail_bulletin(formsemestre.id, I, pdfdata, filename, recipient_addr)
flash(f"mail envoyé à {recipient_addr}")
else:
raise ValueError("do_formsemestre_bulletinetud: invalid format (%s)" % format)
return True, I["filigranne"]
raise ValueError("do_formsemestre_bulletinetud: invalid format (%s)" % format)
def mail_bulletin(formsemestre_id, I, pdfdata, filename, recipient_addr):

View File

@ -49,6 +49,7 @@ Balises img: actuellement interdites.
from reportlab.platypus import KeepTogether, Paragraph, Spacer, Table
from reportlab.lib.units import cm, mm
from reportlab.lib.colors import Color, blue
from app.models import FormSemestre
from app.scodoc.sco_exceptions import ScoBugCatcher
import app.scodoc.sco_utils as scu
@ -271,7 +272,7 @@ class BulletinGeneratorStandard(sco_bulletins_generator.BulletinGenerator):
)
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
- col_keys: nom des colonnes de la table (clés)
- table: liste de dicts de chaines de caractères
@ -417,10 +418,7 @@ class BulletinGeneratorStandard(sco_bulletins_generator.BulletinGenerator):
# Chaque UE:
for ue in I["ues"]:
ue_type = None
try:
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
coef_ue = ue["coef_ue_txt"] if prefs["bul_show_ue_coef"] else ""
ue_descr = ue["ue_descr_txt"]
rowstyle = ""

View File

@ -36,7 +36,7 @@ import time
from xml.etree import ElementTree
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_login import current_user
from werkzeug.utils import redirect
@ -359,7 +359,7 @@ def formsemestre_bulletinetud(
)
if format == "oldjson":
format = "json"
return sco_bulletins.formsemestre_bulletinetud(
r = sco_bulletins.formsemestre_bulletinetud(
etud,
formsemestre_id=formsemestre_id,
format=format,
@ -368,6 +368,16 @@ def formsemestre_bulletinetud(
force_publishing=force_publishing,
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(
@ -1955,23 +1965,22 @@ def formsemestre_bulletins_mailetuds(
)
# Make each bulletin
nb_send = 0
nb_sent = 0
for etudid in etudids:
h, _ = sco_bulletins.do_formsemestre_bulletinetud(
sent, _ = sco_bulletins.do_formsemestre_bulletinetud(
formsemestre,
etudid,
version=version,
prefer_mail_perso=prefer_mail_perso,
format="pdfmail",
nohtml=True,
)
if h:
nb_send += 1
if sent:
nb_sent += 1
#
return (
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>'
% (nb_send, len(etudids), formsemestre_id)
% (nb_sent, len(etudids), formsemestre_id)
+ html_sco_header.sco_footer()
)