Commits

Mike Orr  committed 66e9189

Merge SessionObject into PyramidBeakerSessionObject, add Message class.

  • Participants
  • Parent commits 314e452

Comments (0)

Files changed (1)

File pyramid_beaker_flash/__init__.py

 from beaker import cache
-from beaker.session import SessionObject as BeakerSessionObject
+from beaker.session import SessionObject
 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
+class Message(object):
+    """A message returned by ``PyramidBeakerSessionObject.pop_messages()``.
+
+    ``Message`` objects have two public attributes: ``category`` and 
+    ``message``.  Calling ``str`` on the instance returns the message text.
+    """
+
+    def __init__(self, category, message):
+        self.category = category
+        self.message = message
+
+    def __str__(self):
+        return self.message
+
 
 def BeakerSessionFactoryConfig(**options):
     """ Return a Pyramid session factory using Beaker session settings
                             ('Set-Cookie', headers['cookie_out']))
             request.add_response_callback(session_callback)
 
+        # Flash API
+
+        @call_save
+        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 = self._get_queue_name(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
+                        return
+            q.append(new_message_tuple)
+
+        @call_save
+        def pop_messages(self, queue=''):
+            """Pop all messages from the specified queue.
+            
+            Return a list of ``Message`` objects.
+            """
+            q_name = self._get_queue_name(queue)
+            try:
+                q = self[q_name]
+            except KeyError:
+                return []
+            messages = [Message(*m) for m in q]
+            del self[q_name][:]
+            return messages
+
+        def _get_queue_name(self, queue_arg):
+            if queue_arg:
+                return "_messages_" + queue_arg
+            return "_messages"
+
         # ISession API
 
         @property