Changed to use werkzeug's LocalStack
bzr revid: nicolas.vanhoren@openerp.com-20130606162020-oemq8eyuxr47a2e1
This commit is contained in:
parent
12e3ce123f
commit
a3e430ec63
|
@ -355,27 +355,19 @@ def httprequest(f):
|
|||
#----------------------------------------------------------
|
||||
# Local storage of requests
|
||||
#----------------------------------------------------------
|
||||
_thlocal = threading.local()
|
||||
from werkzeug.local import LocalStack
|
||||
|
||||
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(object):
|
||||
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_stack = LocalStack()
|
||||
|
||||
request = RequestProxy()
|
||||
def set_request(request):
|
||||
class with_obj(object):
|
||||
def __enter__(self):
|
||||
_request_stack.push(request)
|
||||
def __exit__(self, *args):
|
||||
_request_stack.pop()
|
||||
return with_obj()
|
||||
|
||||
request = _request_stack()
|
||||
|
||||
#----------------------------------------------------------
|
||||
# Controller registration with a metaclass
|
||||
|
@ -659,16 +651,16 @@ class Root(object):
|
|||
if exposed == 'json':
|
||||
_logger.debug("Dispatch json to %s %s %s", ps, c, method_name)
|
||||
def fct(request):
|
||||
req = JsonRequest(request)
|
||||
with RequestProxy.set_request(req):
|
||||
return req.dispatch(method)
|
||||
_req = JsonRequest(request)
|
||||
with set_request(_req):
|
||||
return request.dispatch(method)
|
||||
return fct
|
||||
elif exposed == 'http':
|
||||
_logger.debug("Dispatch http to %s %s %s", ps, c, method_name)
|
||||
def fct(request):
|
||||
req = HttpRequest(request)
|
||||
with RequestProxy.set_request(req):
|
||||
return req.dispatch(method)
|
||||
_req = HttpRequest(request)
|
||||
with set_request(_req):
|
||||
return request.dispatch(method)
|
||||
return fct
|
||||
if method_name != "index":
|
||||
method_name = "index"
|
||||
|
|
|
@ -3,7 +3,7 @@ import mock
|
|||
import unittest2
|
||||
import openerp.addons.web.controllers.main
|
||||
from openerp.addons.web.http import request as req
|
||||
from openerp.addons.web.http import RequestProxy
|
||||
from openerp.addons.web.http import _request_stack
|
||||
|
||||
class TestDataSetController(unittest2.TestCase):
|
||||
def setUp(self):
|
||||
|
|
|
@ -3,7 +3,7 @@ import collections
|
|||
import mock
|
||||
import unittest2
|
||||
from openerp.addons.web.http import request as req
|
||||
from openerp.addons.web.http import RequestProxy
|
||||
from openerp.addons.web.http import set_request
|
||||
|
||||
from ..controllers import main
|
||||
|
||||
|
@ -15,7 +15,7 @@ class Placeholder(object):
|
|||
class LoadTest(unittest2.TestCase):
|
||||
def setUp(self):
|
||||
self.menu = main.Menu()
|
||||
self.tmp_req = RequestProxy.set_request(mock.Mock())
|
||||
self.tmp_req = set_request(mock.Mock())
|
||||
self.tmp_req.__enter__()
|
||||
|
||||
# Have self.request.session.model() return a different mock object for
|
||||
|
|
Loading…
Reference in New Issue