From 01c6623a9be1d9a0f5dc6a63f644124e7184b0bb Mon Sep 17 00:00:00 2001 From: niv-openerp Date: Tue, 4 Jun 2013 15:34:50 +0200 Subject: [PATCH] Puts the request in a thread-local stack bzr revid: nicolas.vanhoren@openerp.com-20130604133450-j7mhtn2r9wbur5o7 --- addons/web/http.py | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/addons/web/http.py b/addons/web/http.py index 7e10ccc71cd..e851826a629 100644 --- a/addons/web/http.py +++ b/addons/web/http.py @@ -349,6 +349,31 @@ def httprequest(f): f.exposed = 'http' return f +#---------------------------------------------------------- +# Local storage of requests +#---------------------------------------------------------- +_thlocal = threading.local() + +class RequestProxy(object): + def __getattr__(self, name): + return getattr(_thlocal.stack[-1], name) + def __setattr__(self, name, val): + return setattr(_thlocal.stack[-1], name, val) + def __delattr__(self, name): + return delattr(_thlocal.stack[-1], name) + @classmethod + def set_request(cls, request): + class with_obj: + def __enter__(self): + if getattr(_thlocal, "stack", None) is None: + _thlocal.stack = [] + _thlocal.stack.append(request) + def __exit__(self, *args): + _thlocal.stack.pop() + return with_obj() + +request = RequestProxy() + #---------------------------------------------------------- # Controller registration with a metaclass #---------------------------------------------------------- @@ -610,10 +635,18 @@ class Root(object): exposed = getattr(method, 'exposed', False) if exposed == 'json': _logger.debug("Dispatch json to %s %s %s", ps, c, method_name) - return lambda request: JsonRequest(request).dispatch(method) + def fct(request): + req = JsonRequest(request) + with RequestProxy.set_request(req): + return req.dispatch(method) + return fct elif exposed == 'http': _logger.debug("Dispatch http to %s %s %s", ps, c, method_name) - return lambda request: HttpRequest(request).dispatch(method) + def fct(request): + req = HttpRequest(request) + with RequestProxy.set_request(req): + return req.dispatch(method) + return fct if method_name != "index": method_name = "index" continue