changeset 7938:0e42ac1a358b

helpers: replace webhelpers.flash with own implementation webhelpers is dead. One small function implements pretty much the same functionality, using the same session key so tests still pass, but also very simple and without external dependencies. It could be implemented with a class and different methods for adding, getting and clearing. But internally, it would probably have pretty much the same helper function has here. So let's just avoid the unnecessary complexity and keep it simple.
author Mads Kiilerich <mads@kiilerich.com>
date Thu, 07 Nov 2019 02:38:47 +0100
parents 3d6994af1189
children 397fe11d089e
files kallithea/lib/helpers.py
diffstat 1 files changed, 22 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/lib/helpers.py	Mon Oct 21 01:10:29 2019 +0200
+++ b/kallithea/lib/helpers.py	Thu Nov 07 02:38:47 2019 +0100
@@ -38,7 +38,6 @@
 from webhelpers2.html.tags import submit, text, textarea
 from webhelpers2.number import format_byte_size
 from webhelpers2.text import chop_at, truncate, wrap_paragraphs
-from webhelpers.pylonslib import Flash
 
 from kallithea.config.routing import url
 from kallithea.lib.annotate import annotate_highlight
@@ -442,6 +441,26 @@
         return escape(safe_unicode(self.message))
 
 
+def _session_flash_messages(append=None, clear=False):
+    """Manage a message queue in tg.session: return the current message queue
+    after appending the given message, and possibly clearing the queue."""
+    key = 'flash'
+    from tg import session
+    if key in session:
+        flash_messages = session[key]
+    else:
+        if append is None:  # common fast path - also used for clearing empty queue
+            return []  # don't bother saving
+        flash_messages = []
+        session[key] = flash_messages
+    if append is not None and append not in flash_messages:
+        flash_messages.append(append)
+    if clear:
+        session.pop(key, None)
+    session.save()
+    return flash_messages
+
+
 def flash(message, category=None, logf=None):
     """
     Show a message to the user _and_ log it through the specified function
@@ -459,7 +478,7 @@
 
     logf('Flash %s: %s', category, message)
 
-    _flash(message, category, True)
+    _session_flash_messages(append=(category, message))
 
 
 def pop_flash_messages():
@@ -467,13 +486,7 @@
 
     The return value is a list of ``Message`` objects.
     """
-    from tg import session
-    messages = session.pop(_flash.session_key, [])
-    session.save()
-    return [_Message(*m) for m in messages]
-
-
-_flash = Flash()
+    return [_Message(*m) for m in _session_flash_messages(clear=True)]
 
 
 age = lambda x, y=False: _age(x, y)