<NME> middleware.py
<BEF> """Custom middlewares."""

"""Custom middlewares."""

from django.http import HttpResponseRedirect

from modoboa.lib.exceptions import ModoboaException
    _render_error, ajax_response, render_to_json_response
)
    _render_error, ajax_response, render_to_json_response
)

from . import singleton


class AjaxLoginRedirect(object):
                response.status_code = 278
        return response


class CommonExceptionCatcher(MiddlewareMixin):
    """Modoboa exceptions catcher."""

    def process_exception(self, request, exception):
        if not isinstance(exception, ModoboaException):
            return None

        if request.is_ajax() or "/api/" in request.path:
            if exception.http_code is None:
                return ajax_response(
                    request, status="ko", respmsg=smart_text(exception),
                    norefresh=True
                )
            return render_to_json_response(
                smart_text(exception), status=exception.http_code
            )
        return _render_error(
            request, user_context={"error": smart_text(exception)}
        )


class RequestCatcherMiddleware(MiddlewareMixin):
        )


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.connect(self)

    def process_request(self, request):
        self._request = request

    def process_response(self, request, response):
        """Empty self._request."""
        self._request = None
        return response

    def __call__(self, **kwargs):
        return self._request

<MSG> Use local thread storage.

see #1015

<DFF> @@ -2,6 +2,8 @@
 
 """Custom middlewares."""
 
+from threading import local
+
 from django.http import HttpResponseRedirect
 
 from modoboa.lib.exceptions import ModoboaException
@@ -10,7 +12,7 @@ from modoboa.lib.web_utils import (
     _render_error, ajax_response, render_to_json_response
 )
 
-from . import singleton
+_local_store = local()
 
 
 class AjaxLoginRedirect(object):
@@ -43,26 +45,20 @@ 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!
-    """
+class RequestCatcherMiddleware(object):
+    """Simple middleware to store the current request."""
 
     def __init__(self):
-        self._request = None
+        _local_store.request = None
         request_accessor.connect(self)
 
     def process_request(self, request):
-        self._request = request
+        _local_store.request = request
 
     def process_response(self, request, response):
-        """Empty self._request."""
-        self._request = None
+        """Empty store."""
+        _local_store.request = None
         return response
 
     def __call__(self, **kwargs):
-        return self._request
+        return _local_store.request

Use local thread storage.