mirror of
https://github.com/spiral-project/ihatemoney.git
synced 2025-05-01 18:52:23 +02:00
Simplify send_email(): don't flash messages within this function
Since we have only three places that call send_email() and already several special cases, it's simpler to just check the return value and flash messages within the calling code.
This commit is contained in:
parent
8d9dc6a61f
commit
a4638be144
2 changed files with 34 additions and 33 deletions
|
@ -11,7 +11,7 @@ from io import BytesIO, StringIO
|
|||
|
||||
import jinja2
|
||||
from json import dumps, JSONEncoder
|
||||
from flask import flash, redirect, current_app, render_template
|
||||
from flask import redirect, current_app, render_template
|
||||
from flask_babel import get_locale
|
||||
from babel import Locale
|
||||
from werkzeug.routing import HTTPException, RoutingException
|
||||
|
@ -36,12 +36,10 @@ def slugify(value):
|
|||
return re.sub(r'[-\s]+', '-', value)
|
||||
|
||||
|
||||
def send_email(mail_message, flash_success="", flash_error=""):
|
||||
"""Send an email using Flask-Mail, returning False if there was an error.
|
||||
def send_email(mail_message):
|
||||
"""Send an email using Flask-Mail, with proper error handling.
|
||||
|
||||
Optionally display a "flash alert" message to the user. The flash
|
||||
message is different depending on whether we could successfully send
|
||||
the email or not.
|
||||
Return True if everything went well, and False if there was an error.
|
||||
"""
|
||||
# Since Python 3.4, SMTPException and socket.error are actually
|
||||
# identical, but this was not the case before. Also, it is more clear
|
||||
|
@ -49,12 +47,8 @@ def send_email(mail_message, flash_success="", flash_error=""):
|
|||
try:
|
||||
current_app.mail.send(mail_message)
|
||||
except (smtplib.SMTPException, socket.error):
|
||||
if flash_error:
|
||||
flash(flash_error, category="danger")
|
||||
return False
|
||||
# Email was sent successfully
|
||||
if flash_success:
|
||||
flash(flash_success, category="success")
|
||||
return True
|
||||
|
||||
|
||||
|
|
|
@ -247,8 +247,12 @@ def create_project():
|
|||
msg = Message(message_title,
|
||||
body=message_body,
|
||||
recipients=[project.contact_email])
|
||||
success = send_email(msg, _("A reminder email has just been sent to you"))
|
||||
if not success:
|
||||
success = send_email(msg)
|
||||
if success:
|
||||
flash(
|
||||
_("A reminder email has just been sent to you"), category="success"
|
||||
)
|
||||
else:
|
||||
# Display the error as a simple "info" alert, because it's
|
||||
# not critical and doesn't prevent using the project.
|
||||
flash(
|
||||
|
@ -279,19 +283,20 @@ def remind_password():
|
|||
body=render_localized_template("password_reminder", project=project),
|
||||
recipients=[project.contact_email],
|
||||
)
|
||||
success = send_email(
|
||||
remind_message,
|
||||
"",
|
||||
success = send_email(remind_message)
|
||||
if success:
|
||||
return redirect(url_for(".password_reminder_sent"))
|
||||
else:
|
||||
flash(
|
||||
_(
|
||||
"Sorry, there was an error while sending you an email "
|
||||
"with password reset instructions. "
|
||||
"Please check the email configuration of the server "
|
||||
"or contact the administrator."
|
||||
),
|
||||
category="danger",
|
||||
)
|
||||
if success:
|
||||
return redirect(url_for(".password_reminder_sent"))
|
||||
|
||||
# Fall-through: we stay on the same page and display the form again
|
||||
return render_template("password_reminder.html", form=form)
|
||||
|
||||
|
||||
|
@ -422,18 +427,20 @@ def invite():
|
|||
body=message_body,
|
||||
recipients=[email.strip()
|
||||
for email in form.emails.data.split(",")])
|
||||
success = send_email(
|
||||
msg,
|
||||
_("Your invitations have been sent"),
|
||||
success = send_email(msg)
|
||||
if success:
|
||||
flash(_("Your invitations have been sent"), category="success")
|
||||
return redirect(url_for(".list_bills"))
|
||||
else:
|
||||
flash(
|
||||
_(
|
||||
"Sorry, there was an error while trying to send the invitation emails. "
|
||||
"Please check the email configuration of the server "
|
||||
"or contact the administrator."
|
||||
),
|
||||
category="danger",
|
||||
)
|
||||
if success:
|
||||
return redirect(url_for(".list_bills"))
|
||||
|
||||
# Fall-through: we stay on the same page and display the form again
|
||||
return render_template("send_invites.html", form=form)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue