| 1 |
############################################################################## |
|---|
| 2 |
# |
|---|
| 3 |
# Copyright (c) 2005-2006 Nuxeo and Contributors. |
|---|
| 4 |
# All Rights Reserved. |
|---|
| 5 |
# |
|---|
| 6 |
# This software is subject to the provisions of the Zope Public License, |
|---|
| 7 |
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. |
|---|
| 8 |
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED |
|---|
| 9 |
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|---|
| 10 |
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS |
|---|
| 11 |
# FOR A PARTICULAR PURPOSE. |
|---|
| 12 |
# |
|---|
| 13 |
############################################################################## |
|---|
| 14 |
""" |
|---|
| 15 |
|
|---|
| 16 |
$Id$ |
|---|
| 17 |
""" |
|---|
| 18 |
__docformat__ = "reStructuredText" |
|---|
| 19 |
|
|---|
| 20 |
from urllib import quote, unquote |
|---|
| 21 |
|
|---|
| 22 |
from cpsskins import minjson as json |
|---|
| 23 |
|
|---|
| 24 |
class ClientStorage(object): |
|---|
| 25 |
"""A client-side storage (the information is stored in cookies) |
|---|
| 26 |
""" |
|---|
| 27 |
def __init__(self, id, request): |
|---|
| 28 |
self.storage_id = u'cpsskins_local_storage_%s' % id |
|---|
| 29 |
self.request = request |
|---|
| 30 |
if self.data is None: |
|---|
| 31 |
self.data = {} |
|---|
| 32 |
|
|---|
| 33 |
def getData(self): |
|---|
| 34 |
"""Get data from a local storage. |
|---|
| 35 |
""" |
|---|
| 36 |
value = self.request.cookies.get(self.storage_id) |
|---|
| 37 |
if value is not None: |
|---|
| 38 |
return json.read(unquote(value)) |
|---|
| 39 |
return None |
|---|
| 40 |
|
|---|
| 41 |
def setData(self, data): |
|---|
| 42 |
"""Set data in the local storage. |
|---|
| 43 |
""" |
|---|
| 44 |
value = quote(json.write(data)) |
|---|
| 45 |
self.request.response.setCookie(self.storage_id, value, path='/') |
|---|
| 46 |
|
|---|
| 47 |
def __setitem__(self, k, v): |
|---|
| 48 |
data = self.data |
|---|
| 49 |
data[k] = v |
|---|
| 50 |
self.data = data |
|---|
| 51 |
|
|---|
| 52 |
def __getitem__(self, k): |
|---|
| 53 |
data = self.data |
|---|
| 54 |
if data is None: |
|---|
| 55 |
return None |
|---|
| 56 |
return data.get(k) |
|---|
| 57 |
|
|---|
| 58 |
def keys(self): |
|---|
| 59 |
data = self.data |
|---|
| 60 |
if data is None: |
|---|
| 61 |
return [] |
|---|
| 62 |
return data.keys() |
|---|
| 63 |
|
|---|
| 64 |
data = property(getData, setData) |
|---|
| 65 |
|
|---|