[Zope3-checkins] CVS: Zope3/src/zope/app/utilities -
configure.zcml:1.4.20.2 session.py:1.1.2.2 session.stx:1.1.2.2
Stuart Bishop
zen at shangri-la.dropbear.id.au
Sun Feb 8 22:09:34 EST 2004
Update of /cvs-repository/Zope3/src/zope/app/utilities
In directory cvs.zope.org:/tmp/cvs-serv27663/src/zope/app/utilities
Modified Files:
Tag: ozzope-session-branch
configure.zcml session.py session.stx
Log Message:
Work in progress - API solidifying
=== Zope3/src/zope/app/utilities/configure.zcml 1.4.20.1 => 1.4.20.2 ===
--- Zope3/src/zope/app/utilities/configure.zcml:1.4.20.1 Sat Feb 7 22:19:03 2004
+++ Zope3/src/zope/app/utilities/configure.zcml Sun Feb 8 22:08:59 2004
@@ -2,18 +2,13 @@
xmlns="http://namespaces.zope.org/zope"
xmlns:browser="http://namespaces.zope.org/browser">
- <!-- Session Templates -->
+ <!-- Session machinery -->
<content class=".session.CookieBrowserIdManager">
- <implements
- interface="zope.app.interfaces.utilities.session.IBrowserIdManager" />
- <implements
- interface="zope.app.interfaces.utilities.session.ICookieBrowserIdManager"
- />
<implements
interface="zope.app.interfaces.services.utility.ILocalUtility" />
<implements
- interface="zope.app.interfaces.annotation.IAttributeAnnotatable" />
+ interface="zope.app.interfaces.annotation.IAttributeAnnotatable" />
<require
interface="zope.app.interfaces.utilities.session.ICookieBrowserIdManager"
permission="zope.Public" />
@@ -22,19 +17,24 @@
permission="zope.ManageContent" />
</content>
- <!-- XXX: Do we want this too?
- <utility
- name="Cookie Browser Id Manager"
- factory=".session.CookieBrowserIdManager"
- provides="zope.app.interfaces.utilities.session.IBrowserIdManager"
- permission="zope.Public" />
- -->
+ <content class=".session.SessionData">
+ <allow interface="zope.app.interfaces.utilities.session.IFullMapping" />
+ </content>
- <adapter
- for="zope.app.interfaces.utilities.session.IBrowserIdManager"
- provides="zope.app.interfaces.utilities.session.IBrowserId"
- factory=".session.getBrowserId"
- permission="zope.Public" />
+ <content class=".session.PersistentSessionDataContainer">
+ <implements
+ interface="zope.app.interfaces.utilities.session.ISessionDataContainer"/>
+ <implements
+ interface="zope.app.interfaces.services.utility.ILocalUtility" />
+ <implements
+ interface="zope.app.interfaces.annotation.IAttributeAnnotatable" />
+ <require
+ interface="zope.app.interfaces.utilities.session.ISessionDataContainer"
+ permission="zope.Public" />
+ <require
+ set_schema="zope.app.interfaces.utilities.session.ISessionDataContainer"
+ permission="zope.ManageContent" />
+ </content>
<!-- Mutable Schema -->
=== Zope3/src/zope/app/utilities/session.py 1.1.2.1 => 1.1.2.2 ===
--- Zope3/src/zope/app/utilities/session.py:1.1.2.1 Sat Feb 7 22:19:03 2004
+++ Zope3/src/zope/app/utilities/session.py Sun Feb 8 22:08:59 2004
@@ -1,6 +1,6 @@
##############################################################################
#
-# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# Copyright (c) 2004 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
@@ -11,7 +11,6 @@
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
-
"""Simplistic session service implemented using cookies.
This is more of a demonstration than a full implementation, but it should
@@ -19,7 +18,8 @@
"""
# System imports
-import sha, time, string, random, hmac
+import sha, time, string, random, hmac, logging
+from UserDict import IterableUserDict
# Zope3 imports
from persistence import Persistent
@@ -28,10 +28,12 @@
from zope.component import getService
from zope.interface import implements
from zope.app import zapi
+from zodb.btrees.OOBTree import OOBTree
# Sibling imports
-from zope.app.interfaces.utilities.session import IBrowserIdManager, IBrowserId
-from zope.app.interfaces.utilities.session import ICookieBrowserIdManager
+from zope.app.interfaces.utilities.session import \
+ IBrowserIdManager, IBrowserId, ICookieBrowserIdManager, \
+ ISessionDataContainer, ISession, IFullMapping, BrowserIdManager
from zope.app.interfaces.container import IContained
cookieSafeTrans = string.maketrans("+/", "-.")
@@ -40,6 +42,7 @@
"""Encode SHA digest for cookie."""
return s.encode("base64")[:-2].translate(cookieSafeTrans)
+
class BrowserId(str):
"""A browser id"""
implements(IBrowserId)
@@ -125,7 +128,61 @@
self.setRequestId(request, sid)
return sid
-def getBrowserId(request):
- ''' Get the browser id for the given request, setting it if necessary '''
- bim = zapi.getUtility(None, IBrowserIdManager)
- return bim.getBrowserId(request)
+
+class PersistentSessionDataContainer(Persistent, IterableUserDict):
+ ''' A SessionDataContainer that stores data in the ZODB '''
+ __parent__ = __name__ = None
+ implements(ISessionDataContainer, IContained)
+
+ def __init__(self):
+ self.data = OOBTree()
+
+class SessionData(Persistent, IterableUserDict):
+ ''' Mapping nodes in the ISessionDataContainer tree '''
+ implements(IFullMapping)
+ def __init__(self):
+ self.data = OOBTree()
+ def __setitem__(self, key, value):
+ self.data[key] = value
+ self.data._p_changed = 1
+ def __delitem__(self, key):
+ del self.data[key]
+ self.data._p_changed = 1
+
+class Session(IterableUserDict):
+ implements(ISession)
+ def __init__(self, data_manager, browser_id, product_id):
+ browser_id = str(browser_id)
+ product_id = str(product_id)
+ try:
+ data = data_manager[browser_id]
+ except KeyError:
+ data_manager[browser_id] = SessionData()
+ data_manager[browser_id][product_id] = SessionData()
+ self.data = data_manager[browser_id][product_id]
+ else:
+ try:
+ self.data = data[product_id]
+ except KeyError:
+ data[product_id] = SessionData()
+ self.data = data[product_id]
+
+
+def getSession(context, request, product_id, session_data_container=None):
+ ''' Retrieve an ISession. session_data_container defaults to
+ an ISessionDataContainer utility registered with the name product_id
+ '''
+ if session_data_container is None:
+ dc = zapi.getUtility(context, ISessionDataContainer, product_id)
+ elif ISessionDataContainer.isImplementedBy(session_data_container):
+ dc = session_data_container
+ else:
+ dc = zapi.getUtility(
+ context, ISessionDataContainer, session_data_container
+ )
+
+ bim = zapi.getUtility(context, IBrowserIdManager, BrowserIdManager)
+ browser_id = bim.getBrowserId(request)
+ return Session(dc, browser_id, product_id)
+
+
=== Zope3/src/zope/app/utilities/session.stx 1.1.2.1 => 1.1.2.2 ===
--- Zope3/src/zope/app/utilities/session.stx:1.1.2.1 Sat Feb 7 22:19:03 2004
+++ Zope3/src/zope/app/utilities/session.stx Sun Feb 8 22:08:59 2004
@@ -18,7 +18,6 @@
>>> browser_id = getAdapter(request, IBrowserId))
- >>> default_dm = getUtility(None, ISessionDataContainer)
>>> explicit_dm = getUtility(None, ISessionDataContainer,
... 'zopeproducts.fooprod')
>>> session = Session(explicit_dm, browser_id, 'zopeproducts.foorprod')
@@ -44,5 +43,5 @@
TODO
----
Do we want to provide one or more 'default' ISessionDataContainer's out of the
-box (eg. 'memory' and 'zodb', or 'persistant' and 'transient')?
+box (eg. 'persistant' and 'transient')?
More information about the Zope3-Checkins
mailing list