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
|
import jinja2
|
||||||
from json import dumps, JSONEncoder
|
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 flask_babel import get_locale
|
||||||
from babel import Locale
|
from babel import Locale
|
||||||
from werkzeug.routing import HTTPException, RoutingException
|
from werkzeug.routing import HTTPException, RoutingException
|
||||||
|
@ -36,12 +36,10 @@ def slugify(value):
|
||||||
return re.sub(r'[-\s]+', '-', value)
|
return re.sub(r'[-\s]+', '-', value)
|
||||||
|
|
||||||
|
|
||||||
def send_email(mail_message, flash_success="", flash_error=""):
|
def send_email(mail_message):
|
||||||
"""Send an email using Flask-Mail, returning False if there was an error.
|
"""Send an email using Flask-Mail, with proper error handling.
|
||||||
|
|
||||||
Optionally display a "flash alert" message to the user. The flash
|
Return True if everything went well, and False if there was an error.
|
||||||
message is different depending on whether we could successfully send
|
|
||||||
the email or not.
|
|
||||||
"""
|
"""
|
||||||
# Since Python 3.4, SMTPException and socket.error are actually
|
# Since Python 3.4, SMTPException and socket.error are actually
|
||||||
# identical, but this was not the case before. Also, it is more clear
|
# 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:
|
try:
|
||||||
current_app.mail.send(mail_message)
|
current_app.mail.send(mail_message)
|
||||||
except (smtplib.SMTPException, socket.error):
|
except (smtplib.SMTPException, socket.error):
|
||||||
if flash_error:
|
|
||||||
flash(flash_error, category="danger")
|
|
||||||
return False
|
return False
|
||||||
# Email was sent successfully
|
# Email was sent successfully
|
||||||
if flash_success:
|
|
||||||
flash(flash_success, category="success")
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -247,8 +247,12 @@ def create_project():
|
||||||
msg = Message(message_title,
|
msg = Message(message_title,
|
||||||
body=message_body,
|
body=message_body,
|
||||||
recipients=[project.contact_email])
|
recipients=[project.contact_email])
|
||||||
success = send_email(msg, _("A reminder email has just been sent to you"))
|
success = send_email(msg)
|
||||||
if not success:
|
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
|
# Display the error as a simple "info" alert, because it's
|
||||||
# not critical and doesn't prevent using the project.
|
# not critical and doesn't prevent using the project.
|
||||||
flash(
|
flash(
|
||||||
|
@ -279,19 +283,20 @@ def remind_password():
|
||||||
body=render_localized_template("password_reminder", project=project),
|
body=render_localized_template("password_reminder", project=project),
|
||||||
recipients=[project.contact_email],
|
recipients=[project.contact_email],
|
||||||
)
|
)
|
||||||
success = send_email(
|
success = send_email(remind_message)
|
||||||
remind_message,
|
|
||||||
"",
|
|
||||||
_(
|
|
||||||
"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."
|
|
||||||
),
|
|
||||||
)
|
|
||||||
if success:
|
if success:
|
||||||
return redirect(url_for(".password_reminder_sent"))
|
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",
|
||||||
|
)
|
||||||
|
# Fall-through: we stay on the same page and display the form again
|
||||||
return render_template("password_reminder.html", form=form)
|
return render_template("password_reminder.html", form=form)
|
||||||
|
|
||||||
|
|
||||||
|
@ -422,18 +427,20 @@ def invite():
|
||||||
body=message_body,
|
body=message_body,
|
||||||
recipients=[email.strip()
|
recipients=[email.strip()
|
||||||
for email in form.emails.data.split(",")])
|
for email in form.emails.data.split(",")])
|
||||||
success = send_email(
|
success = send_email(msg)
|
||||||
msg,
|
|
||||||
_("Your invitations have been sent"),
|
|
||||||
_(
|
|
||||||
"Sorry, there was an error while trying to send the invitation emails. "
|
|
||||||
"Please check the email configuration of the server "
|
|
||||||
"or contact the administrator."
|
|
||||||
),
|
|
||||||
)
|
|
||||||
if success:
|
if success:
|
||||||
|
flash(_("Your invitations have been sent"), category="success")
|
||||||
return redirect(url_for(".list_bills"))
|
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",
|
||||||
|
)
|
||||||
|
# Fall-through: we stay on the same page and display the form again
|
||||||
return render_template("send_invites.html", form=form)
|
return render_template("send_invites.html", form=form)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue