1
0
forked from ScoDoc/ScoDoc

Mode test pour les mails. Closes #326

This commit is contained in:
Emmanuel Viennet 2022-03-04 20:02:50 +01:00
parent e04a187a01
commit b4a7749e5a
6 changed files with 98 additions and 25 deletions

View File

@ -13,7 +13,7 @@ from logging.handlers import SMTPHandler, WatchedFileHandler
from flask import current_app, g, request
from flask import Flask
from flask import abort, has_request_context, jsonify
from flask import abort, flash, has_request_context, jsonify
from flask import render_template
from flask.logging import default_handler
from flask_sqlalchemy import SQLAlchemy
@ -457,15 +457,12 @@ from app.models import Departement
from app.scodoc import notesdb as ndb, sco_preferences
from app.scodoc import sco_cache
# admin_role = Role.query.filter_by(name="SuperAdmin").first()
# if admin_role:
# admin = (
# User.query.join(UserRole)
# .filter((UserRole.user_id == User.id) & (UserRole.role_id == admin_role.id))
# .first()
# )
# else:
# click.echo(
# "Warning: user database not initialized !\n (use: flask user-db-init)"
# )
# admin = None
def scodoc_flash_status_messages():
"""Should be called on each page: flash messages indicating specific ScoDoc status"""
email_test_mode_address = sco_preferences.get_preference("email_test_mode_address")
if email_test_mode_address:
flash(
f"Mode test: mails redirigés vers {email_test_mode_address}",
category="warning",
)

View File

@ -1,8 +1,17 @@
# -*- coding: UTF-8 -*
##############################################################################
# ScoDoc
# Copyright (c) 1999 - 2022 Emmanuel Viennet. All rights reserved.
# See LICENSE
##############################################################################
from threading import Thread
from flask import current_app
from flask import current_app, g
from flask_mail import Message
from app import mail
from app.scodoc import sco_preferences
def send_async_email(app, msg):
@ -11,20 +20,66 @@ def send_async_email(app, msg):
def send_email(
subject: str, sender: str, recipients: list, text_body: str, html_body=""
subject: str,
sender: str,
recipients: list,
text_body: str,
html_body="",
bcc=(),
attachments=(),
):
"""
Send an email
Send an email. _All_ ScoDoc mails SHOULD be sent using this function.
If html_body is specified, build a multipart message with HTML content,
else send a plain text email.
attachements: list of dict { 'filename', 'mimetype', 'data' }
"""
msg = Message(subject, sender=sender, recipients=recipients)
msg = Message(subject, sender=sender, recipients=recipients, bcc=bcc)
msg.body = text_body
msg.html = html_body
if attachments:
for attachment in attachments:
msg.attach(
attachment["filename"], attachment["mimetype"], attachment["data"]
)
send_message(msg)
def send_message(msg):
def send_message(msg: Message):
"""Send a message.
All ScoDoc emails MUST be sent by this function.
In mail debug mode, addresses are discarded and all mails are sent to the
specified debugging address.
"""
if hasattr(g, "scodoc_dept"):
# on est dans un département, on peut accéder aux préférences
email_test_mode_address = sco_preferences.get_preference(
"email_test_mode_address"
)
if email_test_mode_address:
# Mode spécial test: remplace les adresses de destination
orig_to = msg.recipients
orig_cc = msg.cc
orig_bcc = msg.bcc
msg.recipients = [email_test_mode_address]
msg.cc = None
msg.bcc = None
msg.subject = "[TEST SCODOC] " + msg.subject
msg.body = (
f"""--- Message ScoDoc dérouté pour tests ---
Adresses d'origine:
to : {orig_to}
cc : {orig_cc}
bcc: {orig_bcc}
---
\n\n"""
+ msg.body
)
Thread(
target=send_async_email, args=(current_app._get_current_object(), msg)
).start()

View File

@ -35,7 +35,7 @@ from flask import request
from flask_login import current_user
import app.scodoc.sco_utils as scu
from app import log
from app import scodoc_flash_status_messages
from app.scodoc import html_sidebar
import sco_version
@ -153,13 +153,14 @@ def sco_header(
"Main HTML page header for ScoDoc"
from app.scodoc.sco_formsemestre_status import formsemestre_page_title
scodoc_flash_status_messages()
# Get head message from http request:
if not head_message:
if request.method == "POST":
head_message = request.form.get("head_message", "")
elif request.method == "GET":
head_message = request.args.get("head_message", "")
params = {
"page_title": page_title or sco_version.SCONAME,
"no_side_bar": no_side_bar,

View File

@ -1040,13 +1040,19 @@ def mail_bulletin(formsemestre_id, I, pdfdata, filename, recipient_addr):
bcc = copy_addr.strip()
else:
bcc = ""
msg = Message(subject, sender=sender, recipients=recipients, bcc=[bcc])
msg.body = hea
# Attach pdf
msg.attach(filename, scu.PDF_MIMETYPE, pdfdata)
log("mail bulletin a %s" % recipient_addr)
email.send_message(msg)
email.send_email(
subject,
sender,
recipients,
bcc=[bcc],
text_body=hea,
attachments=[
{"filename": filename, "mimetype": scu.PDF_MIMETYPE, "data": pdfdata}
],
)
def _formsemestre_bulletinetud_header_html(

View File

@ -245,6 +245,7 @@ PREF_CATEGORIES = (
),
("pe", {"title": "Avis de poursuites d'études"}),
("edt", {"title": "Connexion avec le logiciel d'emplois du temps"}),
("debug", {"title": "Tests / mise au point"}),
)
@ -1859,6 +1860,19 @@ class BasePreferences(object):
"category": "edt",
},
),
(
"email_test_mode_address",
{
"title": "Adresse de test",
"initvalue": "",
"explanation": """si cette adresse est indiquée, TOUS les mails
envoyés par ScoDoc de ce département vont aller vers elle
AU LIEU DE LEUR DESTINATION NORMALE !""",
"size": 30,
"category": "debug",
"only_global": True,
},
),
)
self.prefs_name = set([x[0] for x in self.prefs_definition])

View File

@ -1,7 +1,7 @@
# -*- mode: python -*-
# -*- coding: utf-8 -*-
SCOVERSION = "9.1.71"
SCOVERSION = "9.1.72"
SCONAME = "ScoDoc"