Commits

Mike Orr committed 27641bb

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

Comments (0)

Files changed (2)

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

webhelpers/pylonslib/flash.py

 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
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.