[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/session/ The ISession interface and the according implementation in

Bernd Dorn bernd.dorn at fhv.at
Thu Sep 21 13:10:09 EDT 2006


Log message for revision 70290:
  The ISession interface and the according implementation in
  zope.app.session now define a get method. This method can be
  used to check if a specific session key is defined without
  automatically creating it.
  

Changed:
  U   Zope3/trunk/src/zope/app/session/interfaces.py
  U   Zope3/trunk/src/zope/app/session/session.py

-=-
Modified: Zope3/trunk/src/zope/app/session/interfaces.py
===================================================================
--- Zope3/trunk/src/zope/app/session/interfaces.py	2006-09-21 13:35:13 UTC (rev 70289)
+++ Zope3/trunk/src/zope/app/session/interfaces.py	2006-09-21 17:10:08 UTC (rev 70290)
@@ -114,7 +114,11 @@
         
         """
 
+    def get(product_id, default=None):
+        """Return the relevant ISessionPkgData or default if not
+        available"""
 
+
 class ISessionData(IReadMapping, IMapping):
     """Storage for a particular product id's session data
     

Modified: Zope3/trunk/src/zope/app/session/session.py
===================================================================
--- Zope3/trunk/src/zope/app/session/session.py	2006-09-21 13:35:13 UTC (rev 70289)
+++ Zope3/trunk/src/zope/app/session/session.py	2006-09-21 17:10:08 UTC (rev 70290)
@@ -328,6 +328,59 @@
     def __init__(self, request):
         self.client_id = str(IClientId(request))
 
+    def _sdc(self, pkg_id):
+        # Locate the ISessionDataContainer by looking up the named
+        # Utility, and falling back to the unnamed one.
+        try:
+            return getUtility(ISessionDataContainer, pkg_id)
+        except ComponentLookupError:
+            return getUtility(ISessionDataContainer)
+
+    def get(self, pkg_id, default=None):
+
+        """See zope.app.session.interfaces.ISession
+
+            >>> import tests
+            >>> request = tests.setUp(PersistentSessionDataContainer)
+
+           If we use get we get None returned as default if the pkg_id
+           is not there.
+           
+            >>> session = Session(request).get('not.there')
+            >>> session is None
+            True
+            
+           This method is lazy and does not create the session data.
+            >>> session = Session(request).get('not.there')
+            >>> session is None
+            True
+
+           The __getitem__ method instead creates the data.
+            >>> session = Session(request)['not.there']
+            >>> session is None
+            False
+            >>> session = Session(request).get('not.there')
+            >>> session is None
+            False
+            >>> tests.tearDown()
+
+        """
+
+
+        # The ISessionDataContainer contains two levels:
+        # ISessionDataContainer[client_id] == ISessionData
+        # ISessionDataContainer[client_id][pkg_id] == ISessionPkgData
+        sdc = self._sdc(pkg_id)
+        try:
+            sd = sdc[self.client_id]
+        except KeyError:
+            return None
+        try:
+            return sd[pkg_id]
+        except KeyError:
+            return None
+
+
     def __getitem__(self, pkg_id):
         """See zope.app.session.interfaces.ISession
 
@@ -370,14 +423,8 @@
             >>> tests.tearDown()
 
         """
+        sdc = self._sdc(pkg_id)
 
-        # First locate the ISessionDataContainer by looking up
-        # the named Utility, and falling back to the unnamed one.
-        try:
-            sdc = getUtility(ISessionDataContainer, pkg_id)
-        except ComponentLookupError:
-            sdc = getUtility(ISessionDataContainer)
-
         # The ISessionDataContainer contains two levels:
         # ISessionDataContainer[client_id] == ISessionData
         # ISessionDataContainer[client_id][pkg_id] == ISessionPkgData



More information about the Zope3-Checkins mailing list