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 curses import ERR
from flask import session
from flask import flash, session
DEBUG = 10
INFO = 20
@ -26,21 +23,20 @@ DEFAULT_TAGS = {
}
DEFAULT_ICONS = {
DEBUG: 'tools',
INFO: 'info-circle',
SUCCESS: 'check-circle',
WARNING: 'exclamation-triangle',
ERROR: 'exclamation-octagon',
DEFAULT_TAGS[DEBUG]: 'tools',
DEFAULT_TAGS[INFO]: 'info-circle',
DEFAULT_TAGS[SUCCESS]: 'check-circle',
DEFAULT_TAGS[WARNING]: 'exclamation-triangle',
DEFAULT_TAGS[ERROR]: 'exclamation-octagon',
}
def add_message(level, message):
messages = session.get('_messages', [])
icon = DEFAULT_ICONS[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):

View file

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