Use built-in flask.flash to implement messages

This commit is contained in:
Santiago L 2022-02-08 11:39:08 +01:00
parent 574ac41239
commit aeb765dcd7
2 changed files with 13 additions and 17 deletions

View File

@ -1,7 +1,4 @@
from audioop import add from flask import flash, session
from curses import ERR
from flask import session
DEBUG = 10 DEBUG = 10
INFO = 20 INFO = 20
@ -26,21 +23,20 @@ DEFAULT_TAGS = {
} }
DEFAULT_ICONS = { DEFAULT_ICONS = {
DEBUG: 'tools', DEFAULT_TAGS[DEBUG]: 'tools',
INFO: 'info-circle', DEFAULT_TAGS[INFO]: 'info-circle',
SUCCESS: 'check-circle', DEFAULT_TAGS[SUCCESS]: 'check-circle',
WARNING: 'exclamation-triangle', DEFAULT_TAGS[WARNING]: 'exclamation-triangle',
ERROR: 'exclamation-octagon', DEFAULT_TAGS[ERROR]: 'exclamation-octagon',
} }
def add_message(level, message): def add_message(level, message):
messages = session.get('_messages', [])
icon = DEFAULT_ICONS[level]
level_tag = DEFAULT_TAGS[level] level_tag = DEFAULT_TAGS[level]
if '_message_icon' not in session:
session['_message_icon'] = DEFAULT_ICONS
messages.append({'level': level_tag, 'icon': icon, 'content': message}) flash(message, level_tag)
def debug(message): def debug(message):

View File

@ -176,10 +176,10 @@
<main id="main" class="main"> <main id="main" class="main">
{% block messages %} {% block messages %}
{% for message in session['_messages'] %} {% for level, message in get_flashed_messages(with_categories=true) %}
<div class="alert alert-{{ message.level}} alert-dismissible fade show" role="alert"> <div class="alert alert-{{ level}} alert-dismissible fade show" role="alert">
<i class="bi bi-{{ message.icon }} me-1"></i> <i class="bi bi-{{ session['_message_icon'][level]}} me-1"></i>
{{ message.content }} {{ message }}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button> <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div> </div>
{% endfor %} {% endfor %}