From d1f70c6c18e44a82ded765f4d53668ea3e5d34a1 Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Wed, 21 Dec 2011 07:39:18 +0100 Subject: [PATCH] Added a utility class for session data storage. --- wrw/__init__.py | 2 +- wrw/util.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/wrw/__init__.py b/wrw/__init__.py index aec9825..30936b6 100644 --- a/wrw/__init__.py +++ b/wrw/__init__.py @@ -1,6 +1,6 @@ __all__ = ["wsgiwrap", "restart", "cookie", "formdata"] -from util import wsgiwrap, persession +from util import wsgiwrap, persession, sessiondata from dispatch import restart import cookie from form import formdata diff --git a/wrw/util.py b/wrw/util.py index 09b9e36..3da6053 100644 --- a/wrw/util.py +++ b/wrw/util.py @@ -19,3 +19,35 @@ def persession(data = None): return sess[callable].handle(req) return wrapper return dec + +class sessiondata(object): + @classmethod + def get(cls, req): + sess = cls.sessdb().get(req) + with sess.lock: + try: + return sess[cls] + except KeyError: + ret = cls(req) + ret._is_dirty = [False] + sess[cls] = ret + return ret + + @classmethod + def sessdb(cls): + return session.default + + def sessfrozen(self): + self._is_dirty[:] = [False] + + def sessdirty(self): + return self._is_dirty[0] + + def __setattr__(self, name, value): + if hasattr(self, "_is_dirty"): + self._is_dirty[:] = [True] + super(sessiondata, self).__setattr__(name, value) + + def __delattr__(self, name): + super(sessiondata, self).__delattr__(name) + self._is_dirty[:] = [True] -- 2.11.0