From aeb765dcd7eda12f7edd34c296afa47008990013 Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Tue, 8 Feb 2022 11:39:08 +0100 Subject: [PATCH] Use built-in flask.flash to implement messages --- ereuse_devicehub/messages.py | 22 ++++++++----------- .../templates/ereuse_devicehub/base_site.html | 8 +++---- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/ereuse_devicehub/messages.py b/ereuse_devicehub/messages.py index 29412060..ba3bf089 100644 --- a/ereuse_devicehub/messages.py +++ b/ereuse_devicehub/messages.py @@ -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): diff --git a/ereuse_devicehub/templates/ereuse_devicehub/base_site.html b/ereuse_devicehub/templates/ereuse_devicehub/base_site.html index b0baf853..5bd1994c 100644 --- a/ereuse_devicehub/templates/ereuse_devicehub/base_site.html +++ b/ereuse_devicehub/templates/ereuse_devicehub/base_site.html @@ -176,10 +176,10 @@
{% block messages %} - {% for message in session['_messages'] %} -