<NME> middleware.py
<BEF> # coding: utf-8
import re
from django.http import Http404, HttpResponseRedirect
from modoboa.core.models import Extension
from modoboa.lib.exceptions import ModoboaException
from modoboa.lib.web_utils import (
    _render_error, ajax_response, render_to_json_response
)
from modoboa.lib.exceptions import ModoboaException


class ExtControlMiddleware(object):
        if request.is_ajax():
            if isinstance(response, HttpResponseRedirect):
                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):
    """Simple middleware to store the current request."""

    def process_request(self, request):
        lib_signals.set_current_request(request)

    def process_response(self, request, response):
        """Empty store."""
        lib_signals.set_current_request(None)
        return response
        return render_to_json_response(
            unicode(exception), status=exception.http_code
        )

<MSG> Record current user instead of an old one.

fix #526

<DFF> @@ -1,4 +1,7 @@
 # coding: utf-8
+"""
+Custom middlewares.
+"""
 import re
 from django.http import Http404, HttpResponseRedirect
 from modoboa.core.models import Extension
@@ -7,6 +10,7 @@ from modoboa.lib.webutils import (
     _render_error, ajax_response, render_to_json_response
 )
 from modoboa.lib.exceptions import ModoboaException
+from modoboa.lib.signals import request_accessor
 
 
 class ExtControlMiddleware(object):
@@ -50,3 +54,19 @@ class CommonExceptionCatcher(object):
         return render_to_json_response(
             unicode(exception), status=exception.http_code
         )
+
+
+class RequestCatcherMiddleware(object):
+    """
+    Simple middleware to store the current request.
+    """
+
+    def __init__(self):
+        self._request = None
+        request_accessor.connect(self)
+
+    def process_request(self, request):
+        self._request = request
+
+    def __call__(self, **kwargs):
+        return self._request

Record current user instead of an old one.