mirror of
https://github.com/spiral-project/ihatemoney.git
synced 2025-05-06 21:11:49 +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
9383b5aa74
commit
8eca9fb7ec
2 changed files with 34 additions and 33 deletions
|
@ -12,7 +12,7 @@ import socket
|
||||||
|
|
||||||
from babel import Locale
|
from babel import Locale
|
||||||
from babel.numbers import get_currency_name, get_currency_symbol
|
from babel.numbers import get_currency_name, get_currency_symbol
|
||||||
from flask import current_app, flash, redirect, render_template
|
from flask import current_app, redirect, render_template
|
||||||
from flask_babel import get_locale, lazy_gettext as _
|
from flask_babel import get_locale, lazy_gettext as _
|
||||||
import jinja2
|
import jinja2
|
||||||
from werkzeug.routing import HTTPException, RoutingException
|
from werkzeug.routing import HTTPException, RoutingException
|
||||||
|
@ -30,12 +30,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
|
||||||
|
@ -43,12 +41,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
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -308,8 +308,12 @@ def create_project():
|
||||||
msg = Message(
|
msg = Message(
|
||||||
message_title, body=message_body, recipients=[project.contact_email]
|
message_title, body=message_body, 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(
|
||||||
|
@ -339,19 +343,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)
|
||||||
|
|
||||||
|
|
||||||
|
@ -598,18 +603,20 @@ def invite():
|
||||||
body=message_body,
|
body=message_body,
|
||||||
recipients=[email.strip() for email in form.emails.data.split(",")],
|
recipients=[email.strip() 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