Anonymous avatar Anonymous committed 47f7a81

Add ``ignore_duplicate`` argument, contributed by Martin Lulek (ergo)

Comments (0)

Files changed (3)

 
   - New function ``deprecate``.
 
+* webhelpers.pylonslib.flash:
+
+  - New argument ``ignore_duplicate`` to prevent adding the same message
+    multiple times.
+
 * webhelpers.number:
 
   - New functions ``format_data_size``, ``format_byte_size``, and

tests/test_pylonslib_flash.py

         eq_(len(messages), 0)
         messages2 = flash.pop_messages()
         eq_(len(messages2), 0)
+
+    def test_duplicate_messages(self):
+        MESSAGE = "First message."
+        CATEGORY = "warning"
+        CATEGORY2 = "error"
+        flash = Flash()
+        flash(MESSAGE, CATEGORY, True)
+        flash(MESSAGE, CATEGORY, True)
+        flash(MESSAGE, CATEGORY2, True)
+        messages = flash.pop_messages()
+        eq_(len(messages), 1)
+        eq_(messages[0].message, MESSAGE)
+        eq_(messages[0].category, CATEGORY2)

webhelpers/pylonslib/flash.py

         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):
+    def __call__(self, message, category=None, ignore_duplicate=False):
         """Add a message to the session.
 
         ``message`` is the message text.
         ``category`` is the message's category. If not specified, the default
         category will be used.  Raise ``ValueError`` if the category is not
         in the list of allowed categories.
+        
+        If ``ignore_duplicate`` is true, don't add the message if another
+        message with identical text has already been added. If the new
+        message has a different category than the original message, change the
+        original message to the new category.
+        
         """
         if not category:
             category = self.default_category
             raise ValueError("unrecognized category %r" % (category,))
         # Don't store Message objects in the session, to avoid unpickling
         # errors in edge cases.
+        new_message_tuple = (category, message)
         from pylons import session
-        session.setdefault(self.session_key, []).append((category, message))
+        messages = session.setdefault(self.session_key, [])
+        # ``messages`` is a mutable list, so changes to the local variable are
+        # reflected in the session.
+        if ignore_duplicate:
+            for i, m in enumerate(messages):
+                if m[1] == message:
+                    if m[0] != category:
+                        messages[i] = new_message_tuple
+                        session.save()
+                    return    # Original message found, so exit early.
+        messages.append(new_message_tuple)
         session.save()
 
     def pop_messages(self):
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.