Add support for different categories of "flash alerts"

It is now possible to specify a category when calling flash() to change
the color of the alert box.  The default category is interpreted as
"success" for backwards compatibility.  For now, we only use the
additional "danger" category to show errors.

The available categories are the Bootstrap contextual classes listed here:

  https://getbootstrap.com/docs/4.0/components/alerts/
This commit is contained in:
Baptiste Jonglez 2020-04-26 22:27:47 +02:00
parent d9dc38947c
commit f70b000a00
2 changed files with 10 additions and 15 deletions

View file

@ -125,9 +125,13 @@
</div> </div>
<div class="messages"> <div class="messages">
{% for message in get_flashed_messages() %} {% for category, message in get_flashed_messages(with_categories=true) %}
<div class="flash alert alert-success">{{ message }}</div> {% if category == "message" %}{# Default category for flash(msg) #}
{% endfor %} <div class="flash alert alert-success">{{ message }}</div>
{% else %}
<div class="flash alert alert-{{ category }}">{{ message }}</div>
{% endif %}
{% endfor %}
</div> </div>
{% block footer %} {% block footer %}

View file

@ -309,19 +309,10 @@ def create_project():
try: try:
current_app.mail.send(msg) current_app.mail.send(msg)
except SMTPRecipientsRefused: except SMTPRecipientsRefused:
msg_compl = "Problem sending mail. " flash(_("Error while sending reminder email"), category="danger")
# TODO: destroy the project and cancel instead?
else:
msg_compl = ""
# redirect the user to the next step (invite) # redirect the user to the next step (invite)
flash( flash(_("The project identifier is %(project)s", project=project.id))
_(
"%(msg_compl)sThe project identifier is %(project)s",
msg_compl=msg_compl,
project=project.id,
)
)
return redirect(url_for(".list_bills", project_id=project.id)) return redirect(url_for(".list_bills", project_id=project.id))
return render_template("create_project.html", form=form) return render_template("create_project.html", form=form)
@ -393,7 +384,7 @@ def edit_project():
return redirect(url_for("main.list_bills")) return redirect(url_for("main.list_bills"))
except ValueError: except ValueError:
flash(_("Invalid JSON"), category="error") flash(_("Invalid JSON"), category="danger")
# Edit form # Edit form
if edit_form.validate_on_submit(): if edit_form.validate_on_submit():