mirror of
https://github.com/jwinterm/modoboa.git
synced 2026-10-18 05:51:26 +02:00
Make sure request is local to current thread.
This is a temp. fix, the code using get_request() needs a rewrite...
This commit is contained in:
@@ -2,17 +2,14 @@
|
||||
|
||||
"""Custom middlewares."""
|
||||
|
||||
|
||||
from django.http import HttpResponseRedirect
|
||||
|
||||
from modoboa.lib.exceptions import ModoboaException
|
||||
from modoboa.lib.signals import request_accessor
|
||||
from modoboa.lib import signals as lib_signals
|
||||
from modoboa.lib.web_utils import (
|
||||
_render_error, ajax_response, render_to_json_response
|
||||
)
|
||||
|
||||
from . import singleton
|
||||
|
||||
|
||||
class AjaxLoginRedirect(object):
|
||||
|
||||
@@ -44,26 +41,13 @@ class CommonExceptionCatcher(object):
|
||||
)
|
||||
|
||||
|
||||
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):
|
||||
self._request = None
|
||||
request_accessor.signal.connect(self)
|
||||
class RequestCatcherMiddleware(object):
|
||||
"""Simple middleware to store the current request."""
|
||||
|
||||
def process_request(self, request):
|
||||
self._request = request
|
||||
lib_signals.set_current_request(request)
|
||||
|
||||
def process_response(self, request, response):
|
||||
"""Empty store."""
|
||||
self._request = None
|
||||
lib_signals.set_current_request(None)
|
||||
return response
|
||||
|
||||
def __call__(self, **kwargs):
|
||||
return self._request
|
||||
|
||||
@@ -2,15 +2,16 @@
|
||||
|
||||
from threading import local
|
||||
|
||||
import django.dispatch
|
||||
_local_store = local()
|
||||
|
||||
request_accessor = local()
|
||||
request_accessor.signal = django.dispatch.Signal()
|
||||
|
||||
def set_current_request(request):
|
||||
"""Store current request."""
|
||||
_local_store.request = request
|
||||
|
||||
|
||||
def get_request():
|
||||
"""Get the current request from anywhere."""
|
||||
request = request_accessor.signal.send(None)
|
||||
if request:
|
||||
return request[0][1]
|
||||
return None
|
||||
if "request" not in _local_store.__dict__:
|
||||
return None
|
||||
return _local_store.request
|
||||
|
||||
Reference in New Issue
Block a user