Often it is very useful to have flash/status messages of several
different types, for example to be able to differentiate between
informational messages, warnings and errors. To do that I made
a simple extension to WebHelpers to add category support to its
Flash class. The change is completely backwards compatible, so if
you do not need categories you will never notice them.

Patch attached.

Wichert.

-- 
Wichert Akkerman <wich...@wiggy.net>    It is simple to make things.
http://www.wiggy.net/                   It is hard to make things simple.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to pylons-discuss@googlegroups.com
To unsubscribe from this group, send email to 
pylons-discuss+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---

diff -wurN WebHelpers-0.6.4/webhelpers/pylonslib.py WebHelpers/webhelpers/pylonslib.py
--- WebHelpers-0.6.4/webhelpers/pylonslib.py	2008-12-01 23:59:25.000000000 +0100
+++ WebHelpers/webhelpers/pylonslib.py	2008-12-19 20:52:10.000000000 +0100
@@ -9,6 +9,17 @@
 # modules should be importable on any Python system for the standard
 # regression tests.
 
+class Message(object):
+    def __init__(self, category, message):
+        self.category=category
+        self.message=message
+
+    def __str__(self):
+        return self.message
+
+    __unicode__ = __str__
+
+
 class Flash(object):
     """Accumulate a list of messages to show at the next page request.
 
@@ -64,13 +75,15 @@
     def __init__(self, session_key="flash"):
         self.session_key = session_key
 
-    def __call__(self, message):
+    def __call__(self, message, category="notice"):
+        if category not in [ "warning", "notice", "error", "success" ]:
+            raise ValueError(category)
         from pylons import session
-        session.setdefault(self.session_key, []).append(message)
+        session.setdefault(self.session_key, []).append((category, message))
         session.save()
 
     def pop_messages(self):
         from pylons import session
         messages = session.pop(self.session_key, [])
         session.save()
-        return messages
+        return [Message(*m) for m in messages]

Reply via email to