Commits

offline committed de2bc41

Redirect exception added

Comments (0)

Files changed (2)

annoying/exceptions.py

+class Redirect(Exception):
+    def __init__(self, *args, **kwargs):
+        self.args = args
+        self.kwargs = kwargs
+

annoying/middlewares.py

 
 from django.conf import settings
 from django.views.static import serve
+from django.shortcuts import redirect
 
+from .exceptions import Redirect
 
 
 class StaticServe(object):
             match = self.regex.search(request.path)
             if match:
                 return serve(request, match.group(1), settings.MEDIA_ROOT)
+
+
+class RedirectMiddleware(object):
+    """
+    You must add this middleware to MIDDLEWARE_CLASSES list,
+    to make work Redirect exception. All arguments passed to
+    Redirect will be passed to django built in redirect function.
+    """
+    def process_exception(self, request, exception):
+        if not isinstance(exception, Redirect):
+            return
+        return redirect(*exception.args, **exception.kwargs)