[Zope-CVS] CVS: Products/ZSQLSessionDataManager - CHANGES.txt:1.2
ZSQLSessionDataManager.py:1.2 version.txt:1.2
Tres Seaver
tseaver at zope.com
Tue May 3 17:18:26 EDT 2005
Update of /cvs-repository/Products/ZSQLSessionDataManager
In directory cvs.zope.org:/tmp/cvs-serv1607
Modified Files:
CHANGES.txt ZSQLSessionDataManager.py version.txt
Log Message:
- Merge changes from 1.3 tarball.
=== Products/ZSQLSessionDataManager/CHANGES.txt 1.1.1.3 => 1.2 ===
--- Products/ZSQLSessionDataManager/CHANGES.txt:1.1.1.3 Fri Apr 15 10:32:38 2005
+++ Products/ZSQLSessionDataManager/CHANGES.txt Tue May 3 17:18:25 2005
@@ -1,5 +1,15 @@
ZSQLSessionDataManager Product Changelog
+ ZSQLSessionDataManager 1.3 (2005/04/20)
+
+ - CVS tag: 'ZSQLSessionDataManager-1_3'.
+
+ - Made SessionDataProxy declare formally its implementation of
+ a whole lot more interfaces.
+
+ - 'ZSQLSessionDataManager.manage_beforeDelete' now removes
+ the sessions table.
+
ZSQLSessionDataManager 1.2 (2005/04/15)
- CVS tag: 'ZSQLSessionDataManager-1_2'.
=== Products/ZSQLSessionDataManager/ZSQLSessionDataManager.py 1.1.1.3 => 1.2 ===
--- Products/ZSQLSessionDataManager/ZSQLSessionDataManager.py:1.1.1.3 Fri Apr 15 10:32:38 2005
+++ Products/ZSQLSessionDataManager/ZSQLSessionDataManager.py Tue May 3 17:18:25 2005
@@ -25,8 +25,10 @@
from ZPublisher.BeforeTraverse import registerBeforeTraverse, \
unregisterBeforeTraverse
-from Products.Sessions.BrowserIdManager import isAWellFormedBrowserId, \
- getNewBrowserId, BROWSERID_MANAGER_NAME
+from Products.Transience.TransienceInterfaces import ItemWithId, Transient, \
+ DictionaryLike, TTWDictionary, ImmutablyValuedMappingOfPickleableObjects,\
+ TransientItemContainer
+from Products.Sessions.BrowserIdManager import BROWSERID_MANAGER_NAME
from Products.Sessions.SessionPermissions import *
from Products.Sessions import SessionInterfaces
from Products.Sessions.SessionDataManager import SessionDataManagerErr, \
@@ -155,11 +157,11 @@
date = time.time() - (age * 24 * 60 * 60)
date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(date))
sql = "select count(*) from %s where lastmod < '%s'"
- r = c.query(sql%(self.tableName, date), max_rows=1)
+ r = c.query(sql%(self.getTableName(), date), max_rows=1)
num = r[1][0][0]
sql = "delete from %s where lastmod < '%s'"
- r = c.query(sql%(self.tableName, date))
+ r = c.query(sql%(self.getTableName(), date))
if REQUEST is not None:
return self.manage_sessiondatamgr(self, REQUEST,
manage_tabs_message='%s sessions deleted.'%num)
@@ -198,8 +200,7 @@
# drop existing table
if self.tableName is not None:
- sql = "drop table %s"
- c.query(sql%self.tableName)
+ c.query("drop table %s"%self.tableName)
# create session table
self.tableName = tableName
@@ -209,6 +210,10 @@
)'''
c.query(sql%self.tableName)
+ def manage_beforeDelete(self):
+ c = self._getSessionDataConnection()
+ c.query("drop table %s"%self.getTableName())
+
security.declareProtected(CHANGE_DATAMGR_PERM, 'getTableName')
def getTableName(self):
""" """
@@ -224,7 +229,7 @@
def _hasSessionDataObject(self, key):
""" """
c = self._getSessionDataConnection()
- c.query('select count(*) from %s where key=%%s'%self.tableName,
+ c.query('select count(*) from %s where key=%%s'%self.getTableName(),
(key, ))
return c.fetchone()[0]
@@ -245,13 +250,12 @@
if self.obpath is None:
err = 'Session data container is unspecified in %s' % self.getId()
LOG('Session Tracking', WARNING, err)
- raise SessionIdManagerErr, err
+ raise SessionDataManagerErr, err
try:
return self.restrictedTraverse(self.obpath)()
except:
- raise
raise SessionDataManagerErr, (
- "External session data container '%s' not found." %
+ "External session data connection '%s' not found." %
string.join(self.obpath,'/')
)
@@ -286,59 +290,153 @@
Globals.InitializeClass(ZSQLSessionDataManager)
+# Encoders and decoders for the session data going into the RDBMS database.
+decode_key = lambda x: marshal.loads(base64.decodestring(x))
+encode_key = lambda x: string.strip(base64.encodestring(marshal.dumps(x)))
+decode_value = lambda x: cPickle.loads(base64.decodestring(x))
+encode_value = lambda x: string.strip(base64.encodestring(cPickle.dumps(x)))
class SessionDataProxy(Implicit):
+ __implements__ = (ItemWithId, # randomly generate an id
+ Transient,
+ DictionaryLike,
+ TTWDictionary,
+ ImmutablyValuedMappingOfPickleableObjects
+ )
+
+ security = ClassSecurityInfo()
+ security.setDefaultAccess('allow')
+ security.declareObjectPublic()
+
def __init__(self, connection, browser_id):
self.connection = connection
self.browser_id = browser_id
+ # -----------------------------------------------------------------
+ # DictionaryLike
+ #
+ def keys(self):
+ sql = "select key from %s where bid='%s'"
+ r = self.connection.query(sql%(self.getTableName(), self.browser_id))
+ l = [decode_key(key[0]) for key in r[1]]
+ return l
+
+ def values(self):
+ sql = "select value from %s where bid='%s'"
+ r = self.connection.query(sql%(self.getTableName(), self.browser_id))
+ l = [decode_value(value[0]) for value in r[1]]
+ return l
+
+ def items(self):
+ sql = "select key, value from %s where bid='%s'"
+ r = self.connection.query(sql%(self.getTableName(), self.browser_id))
+ l = [(decode_key(key[0]), decode_value(value[0]))
+ for key, value in r[1]]
+ return l
+
+ _marker = []
+ def get(self, key, default=_marker):
+ if self.has_key(key):
+ return self[key]
+ elif default is self._marker:
+ return None
+ return default
+
def has_key(self, key):
- qkey = base64.encodestring(marshal.dumps(key)).strip()
+ qkey = encode_key(key)
sql = "select count(*) from %s where bid='%s' and key='%s'"
- r = self.connection.query(sql%(self.tableName, self.browser_id,
+ r = self.connection.query(sql%(self.getTableName(), self.browser_id,
qkey), max_rows=1)
return r[1][0][0]
+ def clear(self):
+ self.invalidate()
+
+ def update(self, d):
+ for k in d.keys():
+ self[k] = d[k]
+
+
+ # -----------------------------------------------------------------
+ # ImmutablyValuedMappingOfPickleableObjects (what a mouthful!)
+ #
+
def __getitem__(self, key):
- qkey = base64.encodestring(marshal.dumps(key)).strip()
+ qkey = encode_key(key)
sql = "select value from %s where bid='%s' and key='%s'"
- r = self.connection.query(sql%(self.tableName, self.browser_id,
+ r = self.connection.query(sql%(self.getTableName(), self.browser_id,
qkey), max_rows=1)
if not r or not r[1]:
raise KeyError, key
- value = cPickle.loads(base64.decodestring(r[1][0][0]))
+ value = decode_value(r[1][0][0])
return value
- _marker = []
- def get(self, key, default=_marker):
- if self.has_key(key):
- return self[key]
- elif default is self._marker:
- return None
- return default
-
def __setitem__(self, key, value):
- qkey = base64.encodestring(marshal.dumps(key)).strip()
- qvalue = base64.encodestring(cPickle.dumps(value)).strip()
+ qkey = encode_key(key)
+ qvalue = encode_value(value)
lastmod = time.strftime('%Y-%m-%d %H:%M:%S')
if self.has_key(key):
sql = """update %s set value='%s', lastmod='%s'
where bid='%s' and key='%s'"""
- self.connection.query(sql%(self.tableName, qvalue, lastmod,
+ self.connection.query(sql%(self.getTableName(), qvalue, lastmod,
self.browser_id, qkey))
else:
sql = """insert into %s (bid, key, value, lastmod) values
('%s', '%s', '%s', '%s')"""
- self.connection.query(sql%(self.tableName, self.browser_id,
+ self.connection.query(sql%(self.getTableName(), self.browser_id,
qkey, qvalue, lastmod))
set = __setitem__
def __delitem__(self, key):
- qkey = base64.encodestring(marshal.dumps(key)).strip()
+ qkey = encode_key(key)
sql = "delete from %s where bid='%s' and key='%s'"
- self.connection.query(sql%(self.tableName, self.browser_id, qkey))
+ self.connection.query(sql%(self.getTableName(), self.browser_id, qkey))
+
+
+ # -----------------------------------------------------------------
+ # TTWDictionary
+ #
+
+ set = __setitem__
+ __guarded_setitem__ = __setitem__
+ __guarded_delitem__ = __delitem__
+ delete = __delitem__
+
+
+ # -----------------------------------------------------------------
+ # Transient
+ #
def invalidate(self):
sql = "delete from %s where bid='%s'"
- self.connection.query(sql%(self.tableName, self.browser_id))
+ self.connection.query(sql%(self.getTableName(), self.browser_id))
+
+ def isValid(self):
+ sql = """select count(*) from %s where bid='%s'"""
+ r = self.connection.query(sql%(self.getTableName(), self.browser_id))
+ return r[1][0][0]
+
+ def getLastModified(self):
+ sql = """select max(lastmod) from %s where bid='%s'"""
+ r = self.connection.query(sql%(self.getTableName(), self.browser_id))
+ return r[1][0][0]
+
+ def setLastModified(self):
+ sql = """update %s set lastmod='%s' where bid='%s'"""
+ self.connection.query(sql%(self.getTableName(), lastmod,
+ self.browser_id))
+
+ def getContainerKey(self):
+ return self.browser_id
+
+ def getLastAccessed(self):
+ raise NotImplemented
+
+ def setLastAccessed(self):
+ raise NotImplemented
+
+ def getCreated(self):
+ raise NotImplemented
+
+Globals.InitializeClass(SessionDataProxy)
=== Products/ZSQLSessionDataManager/version.txt 1.1.1.3 => 1.2 ===
--- Products/ZSQLSessionDataManager/version.txt:1.1.1.3 Fri Apr 15 10:32:38 2005
+++ Products/ZSQLSessionDataManager/version.txt Tue May 3 17:18:25 2005
@@ -1 +1 @@
-ZSQLSessionDataManager-1.2
+ZSQLSessionDataManager-1.3
More information about the Zope-CVS
mailing list