Commits

Mike Orr committed 314e452

Initial implementation.

Comments (0)

Files changed (1)

pyramid_beaker_flash/__init__.py

 from beaker import cache
-from beaker.session import SessionObject
+from beaker.session import SessionObject as BeakerSessionObject
 from beaker.util import coerce_session_params
 
 from pyramid.interfaces import ISession
 from zope.interface import implements
 
+class SessionObject(BeakerSessionObject):
+    def flash(self, message, category='notice', ignore_duplicate=False,
+        queue=''):
+        """Add a message to the specified queue.
+        """
+        # Don't store Message objects in the session, to avoid unpickling
+        # errors in edge cases.
+        q_name = "_messages" + queue
+        q = self.setdefault(q_name, [])
+        new_message_tuple = (category, message)
+        if ignore_duplicate:
+            for i, old_message_tuple in enumerate(q):
+                if old_message_tuple[1] == message:
+                    if old_message_tuple[0] != category:
+                        q[i] = new_message_tuple
+                        self.save()
+                    return
+        q.append(new_message_tuple)
+        self.save()
+
+    def pop_messages(self, queue=''):
+        """Pop all messages from the specified queue.
+        
+        Return a list of ``Message`` objects.
+        """
+        q_name = "_messages" + queue
+        try:
+            q = self[q_name]
+        except KeyError:
+            return []
+        messages = [Message(*m) for m in q]
+        del self[q_name][:]
+        self.save()
+        return messages
+
 def BeakerSessionFactoryConfig(**options):
     """ Return a Pyramid session factory using Beaker session settings
     supplied directly as ``**options``"""