--- a/src/ztfy/utils/property.py Thu Oct 25 11:39:05 2012 +0200
+++ b/src/ztfy/utils/property.py Thu Oct 25 18:57:12 2012 +0200
@@ -23,6 +23,8 @@
# import Zope3 packages
# import local packages
+from ztfy.utils.request import queryRequest, getRequestData, setRequestData
+from ztfy.utils.session import getSessionData, setSessionData
class cached(object):
@@ -63,3 +65,46 @@
return self
obj.__dict__[self.__name__] = result = self.fget(obj)
return result
+
+
+_marker = object()
+
+class request_property(object):
+ """Define a request cached property"""
+
+ def __init__(self, fget, key=None):
+ self.fget = fget
+ if key is None:
+ key = "%s.%s" % (fget.__module__, fget.__name__)
+ self.key = key
+
+ def __get__(self, obj, cls):
+ if obj is None:
+ return self
+ request = queryRequest()
+ data = getRequestData(self.key, request, _marker)
+ if data is _marker:
+ data = self.fget(obj)
+ setRequestData(self.key, data, request)
+ return data
+
+
+class session_property(object):
+ """Define a property for which computed value is stored into session"""
+
+ def __init__(self, fget, app, key=None):
+ self.fget = fget
+ self.app = app
+ if key is None:
+ key = "%s.%s" % (fget.__module__, fget.__name__)
+ self.key = key
+
+ def __get__(self, obj, cls):
+ if obj is None:
+ return self
+ request = queryRequest()
+ data = getSessionData(request, self.app, self.key, _marker)
+ if data is _marker:
+ data = self.fget(obj)
+ setSessionData(request, self.app, self.key, data)
+ return data
--- a/src/ztfy/utils/session.py Thu Oct 25 11:39:05 2012 +0200
+++ b/src/ztfy/utils/session.py Thu Oct 25 18:57:12 2012 +0200
@@ -33,8 +33,12 @@
session = ISession(request)[app]
return session.get(key, default)
+getSessionData = getData
+
def setData(request, app, key, value):
"""Set data associated to a given session"""
session = ISession(request)[app]
session[key] = unproxied(value)
+
+setSessionData = setData