Merge pull request #1057 from modoboa/fix/request_accessor

Make sure signal is not shared between threads.
This commit is contained in:
Antoine Nguyen
2017-03-02 11:44:58 +01:00
committed by GitHub
2 changed files with 19 additions and 11 deletions
+14 -9
View File
@@ -2,7 +2,6 @@
"""Custom middlewares."""
from threading import local
from django.http import HttpResponseRedirect
@@ -12,7 +11,7 @@ from modoboa.lib.web_utils import (
_render_error, ajax_response, render_to_json_response
)
_local_store = local()
from . import singleton
class AjaxLoginRedirect(object):
@@ -45,20 +44,26 @@ class CommonExceptionCatcher(object):
)
class RequestCatcherMiddleware(object):
"""Simple middleware to store the current request."""
class RequestCatcherMiddleware(singleton.Singleton):
"""Simple middleware to store the current request.
FIXME: the Singleton hack is used to make tests work. I don't know
why but middlewares are not dropped between test case runs so more
than one instance can be listening to the request_accessor signal
and we don't want that!
"""
def __init__(self):
_local_store.request = None
request_accessor.connect(self)
self._request = None
request_accessor.signal.connect(self)
def process_request(self, request):
_local_store.request = request
self._request = request
def process_response(self, request, response):
"""Empty store."""
_local_store.request = None
self._request = None
return response
def __call__(self, **kwargs):
return _local_store.request
return self._request
+5 -2
View File
@@ -1,13 +1,16 @@
"""Custom signals."""
from threading import local
import django.dispatch
request_accessor = django.dispatch.Signal()
request_accessor = local()
request_accessor.signal = django.dispatch.Signal()
def get_request():
"""Get the current request from anywhere."""
request = request_accessor.send(None)
request = request_accessor.signal.send(None)
if request:
return request[0][1]
return None