Author: jmorliaguet
Date: Thu Jun  8 20:32:56 2006
New Revision: 3361

Added:
   cpsskins/branches/paris-sprint-2006/clientstorage.py   (contents, props 
changed)
Modified:
   cpsskins/branches/paris-sprint-2006/utils.py

Log:

- added an API to simplify access the client-side session storage.



Added: cpsskins/branches/paris-sprint-2006/clientstorage.py
==============================================================================
--- (empty file)
+++ cpsskins/branches/paris-sprint-2006/clientstorage.py        Thu Jun  8 
20:32:56 2006
@@ -0,0 +1,55 @@
+##############################################################################
+#
+# Copyright (c) 2005-2006 Nuxeo and Contributors.
+# 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.
+#
+##############################################################################
+"""
+
+$Id$
+"""
+__docformat__ = "reStructuredText"
+
+from urllib import quote, unquote
+
+from cpsskins import minjson as json
+from cpsskins.utils import getRequest
+
+class ClientStorage(object):
+    """A client-side storage (the information is stored in cookies)
+    """
+    def __init__(self, id):
+        self.storage_id = u'cpsskins_local_storage_%s' % id
+        self.request = getRequest()
+
+    def getData(self):
+        """Get data from a local storage.
+        """
+        value = self.request.cookies.get(self.storage_id)
+        if value is not None:
+            return json.read(unquote(value))
+        return None
+
+    def setData(self, data):
+        """Set data in the local storage.
+        """
+        value = quote(json.write(data))
+        self.request.response.setCookie(self.storage_id, value, path='/')
+
+    def __setitem__(self, k, v):
+        data = self.data
+        data[k] = v
+        self.data = data
+
+    def __getitem__(self, k):
+        return self.data[k]
+
+    data = property(getData, setData)
+

Modified: cpsskins/branches/paris-sprint-2006/utils.py
==============================================================================
--- cpsskins/branches/paris-sprint-2006/utils.py        (original)
+++ cpsskins/branches/paris-sprint-2006/utils.py        Thu Jun  8 20:32:56 2006
@@ -20,7 +20,9 @@
 import re
 
 from zope.component import getSiteManager, getGlobalSiteManager
+from zope.publisher.interfaces import IRequest
 from zope.traversing.api import getParent
+from zope.security.management import getInteraction
 
 from cpsskins.thememanager import IThemeManagementFolder
 
@@ -57,6 +59,13 @@
 def getClientStorageId(id):
     return u'cpsskins_local_storage_%s' % id
 
+def getRequest():
+    interaction = getInteraction()
+    for p in interaction.participations:
+        if IRequest.providedBy(p):
+            return p
+        raise RuntimeError("No IRequest in interaction")
+
 def addThemeSkeleton(context):
     # for testing purposes
     from cpsskins.elements.interfaces import IDisplayable, IFormattable
-- 
http://lists.nuxeo.com/mailman/listinfo/z3lab-checkins

Reply via email to