Add session data management module and TALES adapter
authorThierry Florac <tflorac@ulthar.net>
Fri, 12 Nov 2010 14:54:48 +0100
changeset 66 cff370b82f25
parent 65 c262ddb4d656
child 67 040090e1aa8f
Add session data management module and TALES adapter
ztfy/utils/session.py
ztfy/utils/tal/configure.zcml
ztfy/utils/tal/interfaces.py
ztfy/utils/tal/session.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ztfy/utils/session.py	Fri Nov 12 14:54:48 2010 +0100
@@ -0,0 +1,40 @@
+### -*- coding: utf-8 -*- ####################################################
+##############################################################################
+#
+# Copyright (c) 2008-2010 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+
+__docformat__ = "restructuredtext"
+
+# import standard packages
+
+# import Zope3 interfaces
+from zope.app.session.interfaces import ISession
+
+# import local interfaces
+
+# import Zope3 packages
+
+# import local packages
+from security import unproxied
+
+
+def getData(request, app, key, default=None):
+    """Get data associated with a given session"""
+    session = ISession(request)[app]
+    return session.get(key, default)
+
+
+def setData(request, app, key, value):
+    """Set data associated to a given session"""
+    session = ISession(request)[app]
+    session[key] = unproxied(value)
--- a/ztfy/utils/tal/configure.zcml	Wed Nov 10 07:34:24 2010 +0100
+++ b/ztfy/utils/tal/configure.zcml	Fri Nov 12 14:54:48 2010 +0100
@@ -26,4 +26,10 @@
 		provides="zope.traversing.interfaces.IPathAdapter"
 		for="zope.publisher.interfaces.browser.IBrowserRequest" />
 
+	<adapter
+		name="session"
+		factory=".session.SessionDataTalesAdapter"
+		provides="zope.traversing.interfaces.IPathAdapter"
+		for="zope.publisher.interfaces.browser.IBrowserRequest" />
+
 </configure>
\ No newline at end of file
--- a/ztfy/utils/tal/interfaces.py	Wed Nov 10 07:34:24 2010 +0100
+++ b/ztfy/utils/tal/interfaces.py	Fri Nov 12 14:54:48 2010 +0100
@@ -71,3 +71,10 @@
 
     def __getattr__(attr):
         """Get request data for given attribute"""
+
+
+class ISessionDataTalesAPI(Interface):
+    """Session data TALES namespace interface"""
+
+    def __getattr__(attr):
+        """Get session data for given app and key"""
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ztfy/utils/tal/session.py	Fri Nov 12 14:54:48 2010 +0100
@@ -0,0 +1,45 @@
+### -*- coding: utf-8 -*- ####################################################
+##############################################################################
+#
+# Copyright (c) 2008-2010 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+
+__docformat__ = "restructuredtext"
+
+# import standard packages
+
+# import Zope3 interfaces
+from zope.tales.interfaces import ITALESFunctionNamespace
+
+# import local interfaces
+from interfaces import ISessionDataTalesAPI
+
+# import Zope3 packages
+from zope.interface import implements
+
+# import local packages
+from ztfy.utils.session import getData
+
+
+class SessionDataTalesAdapter(object):
+
+    implements(ISessionDataTalesAPI, ITALESFunctionNamespace)
+
+    def __init__(self, context):
+        self.context = context
+
+    def setEngine(self, engine):
+        self.request = engine.vars['request']
+
+    def __getattr__(self, attr):
+        app, key = attr.split(',')
+        return getData(self.context, app, key)