# HG changeset patch # User Thierry Florac # Date 1289570088 -3600 # Node ID cff370b82f25299f43711ec4bf3352f7b3bc1042 # Parent c262ddb4d656acaf36825762c6e453235a29a9cb Add session data management module and TALES adapter diff -r c262ddb4d656 -r cff370b82f25 ztfy/utils/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 +# 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) diff -r c262ddb4d656 -r cff370b82f25 ztfy/utils/tal/configure.zcml --- 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" /> + + \ No newline at end of file diff -r c262ddb4d656 -r cff370b82f25 ztfy/utils/tal/interfaces.py --- 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""" diff -r c262ddb4d656 -r cff370b82f25 ztfy/utils/tal/session.py --- /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 +# 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)