From: Fredrik Tolf Date: Fri, 27 May 2011 06:14:32 +0000 (+0200) Subject: Added a per-session class-based handler wrapper. X-Git-Url: http://git.dolda2000.com/gitweb/?a=commitdiff_plain;h=d997912890e3b41898eecbae93c87ec2927c826c;p=wrw.git Added a per-session class-based handler wrapper. --- diff --git a/wrw/__init__.py b/wrw/__init__.py index 5f7aacb..61c6e04 100644 --- a/wrw/__init__.py +++ b/wrw/__init__.py @@ -1,7 +1,7 @@ __all__ = ["request", "wsgiwrap", "restart", "cookie", "formdata"] from req import request -from util import wsgiwrap +from util import wsgiwrap, persession from dispatch import restart import cookie from form import formdata diff --git a/wrw/util.py b/wrw/util.py index 22131ea..86836bd 100644 --- a/wrw/util.py +++ b/wrw/util.py @@ -1,6 +1,21 @@ -import req, dispatch +import req, dispatch, session def wsgiwrap(callable): def wrapper(env, startreq): return dispatch.handle(req.request(env), startreq, callable) return wrapper + +def persession(data = None): + def dec(callable): + def wrapper(req): + sess = session.get(req) + if callable not in sess: + if data is None: + sess[callable] = callable() + else: + if data not in sess: + sess[data] = data() + sess[callable] = callable(data) + return sess[callable].handle(req) + return wrapper + return dec