Turbogears migration: flash messages

Thomas De Schampheleire patrickdepinguin at gmail.com
Mon Feb 29 19:02:18 UTC 2016


Hello Alessandro,

In parallel with making the test framework work under Turbogears, I
also had a look at flash messages. Currently, we were using the Flash
class from webhelpers.pylonslib.

I noticed that Turbogears2 has a TGFlash class, and tried to use it.
However, while I could store and retrieve a single flash message, I
could not retrieve multiple ones. Only the last one would be shown.

Here is a rough attempt:

diff --git a/kallithea/controllers/home.py b/kallithea/controllers/home.py
--- a/kallithea/controllers/home.py
+++ b/kallithea/controllers/home.py
@@ -67,6 +67,10 @@ class HomeController(BaseController):
         #json used to render the grid
         c.data = json.dumps(repos_data)

+        from kallithea.lib import helpers as h
+        h.flash('Some message', 'warning')
+        h.flash('Some other message', 'error')
+
         return render('/index.html')

     @LoginRequired()
diff --git a/kallithea/lib/helpers.py b/kallithea/lib/helpers.py
--- a/kallithea/lib/helpers.py
+++ b/kallithea/lib/helpers.py
@@ -40,7 +40,7 @@ from webhelpers.html.tags import auto_di
 from webhelpers.html.tools import auto_link, button_to, highlight, \
     js_obfuscate, mail_to, strip_links, strip_tags, tag_re
 from webhelpers.number import format_byte_size, format_bit_size
-from webhelpers.pylonslib import Flash as _Flash
+from tg.flash import TGFlash as _Flash
 from webhelpers.pylonslib.secure_form import secure_form, authentication_token
 from webhelpers.text import chop_at, collapse, convert_accented_entities, \
     convert_misc_entities, lchop, plural, rchop, remove_formatting, \
@@ -404,17 +405,18 @@ class Flash(_Flash):

         logf('Flash %s: %s', category, message)

-        super(Flash, self).__call__(message, category, ignore_duplicate)
+        super(Flash, self).__call__(message, category)

     def pop_messages(self):
         """Return all accumulated messages and delete them from the session.

         The return value is a list of ``Message`` objects.
         """
-        from tg import session
-        messages = session.pop(self.session_key, [])
-        session.save()
-        return [_Message(*m) for m in messages]
+        #from tg import session
+        #messages = session.pop(self.session_key, [])
+        #session.save()
+        #return [_Message(*m) for m in messages]
+        pass

 flash = Flash()

diff --git a/kallithea/templates/base/flash_msg.html
b/kallithea/templates/base/flash_msg.html
--- a/kallithea/templates/base/flash_msg.html
+++ b/kallithea/templates/base/flash_msg.html
@@ -1,4 +1,4 @@
+${tg.flash_obj.render('flash', use_js=False)}
 <div class="flash_msg"
     <% messages = h.flash.pop_messages() %>
     % if messages:
         % for message in messages:



Can you confirm that multiple flash messages are not supported this
way by TGFlash? Should we wrap around TGFlash in some way, or is it
missing functionality we should really add in tg, or should we use it
differently?

Thanks for your input,
Thomas


More information about the kallithea-general mailing list