[Checkins] SVN: z3c.flashmessage/trunk/ ``SessionMessageSource`` implicitly created sessions when the client was reading the messages from the source. Changed internal API so reading no longer creates a session when it not yet exists.

Michael Howitz mh at gocept.com
Thu Oct 28 08:44:40 EDT 2010


Log message for revision 117996:
  ``SessionMessageSource`` implicitly created sessions when the client was reading the messages from the source. Changed internal API so reading no longer creates a session when it not yet exists.
  
  

Changed:
  U   z3c.flashmessage/trunk/CHANGES.txt
  U   z3c.flashmessage/trunk/src/z3c/flashmessage/sources.py

-=-
Modified: z3c.flashmessage/trunk/CHANGES.txt
===================================================================
--- z3c.flashmessage/trunk/CHANGES.txt	2010-10-28 12:40:53 UTC (rev 117995)
+++ z3c.flashmessage/trunk/CHANGES.txt	2010-10-28 12:44:39 UTC (rev 117996)
@@ -5,13 +5,14 @@
 1.3 (unreleased)
 ================
 
-- Nothing changed yet.
+- ``SessionMessageSource`` implicitly created sessions when the client was
+  reading the messages from the source. Changed internal API so reading no
+  longer creates a session when it not yet exists.
 
-
 1.2 (2010-10-19)
 ================
 
-* Removed test dependency on zope.app.zcmlfiles.
+* Removed test dependency on `zope.app.zcmlfiles`.
 
 
 1.1 (2010-10-02)
@@ -23,8 +24,8 @@
 1.0 (2007-12-06)
 ================
 
-* Updated dependency to `zope.session` instead of `zope.app.session` to get rid of
-  deprecation warnings.
+* Updated dependency to `zope.session` instead of `zope.app.session` to get
+  rid of deprecation warnings.
 
 
 1.0b2 (2007-09-12)

Modified: z3c.flashmessage/trunk/src/z3c/flashmessage/sources.py
===================================================================
--- z3c.flashmessage/trunk/src/z3c/flashmessage/sources.py	2010-10-28 12:40:53 UTC (rev 117995)
+++ z3c.flashmessage/trunk/src/z3c/flashmessage/sources.py	2010-10-28 12:44:39 UTC (rev 117996)
@@ -18,7 +18,8 @@
     """An (abstract) base class that stores messages
     in a list.
 
-    Sub-classes have to define the attribute `_storage`.
+    Sub-classes have to define the method
+    `_get_storage(self, for_write=False)`.
 
     """
 
@@ -31,35 +32,58 @@
             # message for him. This is allowed by the API for convenience.
             message = z3c.flashmessage.message.Message(message, type=type)
         message.source = self
-        self._storage.append(message)
+        self._get_storage(for_write=True).append(message)
 
     def list(self, type=None):
         """Return all messages of the given type from this source."""
-        for message in self._storage:
+        for message in self._get_storage(for_write=False):
             if type is None or message.type == type:
                 yield message
 
     def delete(self, message):
         """Remove the given message from the source."""
-        self._storage.remove(message)
+        self._get_storage(for_write=True).remove(message)
 
+    def _get_storage(self, for_write=False):
+        """Return the storage which must have a list API.
 
+        When `for_write` is True the caller want's to write to the storage.
+
+        To be implemented in concreate sub classes
+
+        """
+
+
 class SessionMessageSource(ListBasedMessageSource):
+    """Source which stores its data in the session of the user."""
 
-    @property
-    def _storage(self):
+    _pkg_id = 'z3c.flashmessage'
+
+    def _get_storage(self, for_write=False):
         request = zope.security.management.getInteraction().participations[0]
-        session = zope.session.interfaces.ISession(
-            request)['z3c.flashmessage']
-        messages = session.setdefault('messages',
-                                      persistent.list.PersistentList())
-        return messages
+        session = zope.session.interfaces.ISession(request)
+        if for_write:
+            # Creating a new session when it does not exist yet.
+            session_data = session[self._pkg_id]
+        else:
+            # Making sure we do *not* create a new session when it not exists:
+            session_data = session.get(self._pkg_id, {})
+        return session_data.setdefault('messages',
+                                       persistent.list.PersistentList())
 
 
 class RAMMessageSource(ListBasedMessageSource):
+    """Source which stores its data in RAM.
 
+    Caution: This source is not able to store messages for individual users.
+
+    """
+
     zope.interface.implements(z3c.flashmessage.interfaces.IMessageSource)
 
     def __init__(self):
         super(RAMMessageSource, self).__init__()
         self._storage = []
+
+    def _get_storage(self, for_write=False):
+        return self._storage



More information about the checkins mailing list