1. Ben Bangert
  2. WebHelpers


Mike Orr  committed 27641bb

Flash categories may be specified in constructor. Patch by Eli Collins.

  • Participants
  • Parent commits 415205f
  • Branches trunk

Comments (0)

Files changed (2)


View file
  • Ignore whitespace
   - Moved to its own module.
   - Changed ``Flash.__html__()`` implementation.
+  - Categories may be specified in constructor. Patch by Eli Collins.
 * webhelpers.pylonslib.grid:

File webhelpers/pylonslib/flash.py

View file
  • Ignore whitespace
 messages below a certain level; it returns all messages set. 
 You can change the standard categories by overriding the ``.categories``
-and ``.default_category`` class attributes.
+and ``.default_category`` class attributes, or by providing alternate
+values using constructor keywords.
 Note that messages are _not_ grouped by category, nor is it possible to 
 pop one category of messages while leaving the others.  If you want to 
     # Default category if none is specified.
     default_category = "notice"
-    def __init__(self, session_key="flash"):
+    def __init__(self, session_key="flash", categories=None, default_category=None):
         """Instantiate a ``Flash`` object.
         ``session_key`` is the key to save the messages under in the user's
-        session.
+        session. 
+        ``categories`` is an optional list which overrides the default list 
+        of categories. 
+        ``default_category`` overrides the default category used for messages 
+        when none is specified.
         self.session_key = session_key
+        if categories is not None:
+            self.categories = categories
+        if default_category is not None:
+            self.default_category = default_category
+        if self.categories and self.default_category not in self.categories:
+            raise ValueError("unrecognized default category %r" % (self.default_category,))
     def __call__(self, message, category=None):
         """Add a message to the session.
         if not category:
             category = self.default_category
         elif self.categories and category not in self.categories:
-            raise ValueError("unrecognized category '%s'" % category)
+            raise ValueError("unrecognized category %r" % (category,))
         # Don't store Message objects in the session, to avoid unpickling
         # errors in edge cases.
         from pylons import session