""" Allows short-curcuiting of ALL remaining middleware by attaching the
@shortcircuitmiddleware decorator as the TOP LEVEL decorator of a view.
Example settings.py:
MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'myapp.middleware.shortcircuit.ShortCircuitMiddleware',
    'myapp.middleware.package.MostOfTheTimeMiddleware',
)
Example view to exclude from MostOfTheTimeMiddleware (and any subsequent):
@shortcircuitmiddleware
def myview(request):
    ...
"""

def shortcircuitmiddleware(f):
    """ view decorator, the sole purpose to is 'rename' the function
    '_shortcircuitmiddleware' """

def _shortcircuitmiddleware(*args, **kwargs):
    return f(*args, **kwargs)
return _shortcircuitmiddleware


class ShortCircuitMiddleware(object):
    """ Middleware; looks for a view function named '_shortcircuitmiddleware'
    and short-circuits. Relies on the fact that if you return an HttpResponse
    from a view, it will short-circuit other middleware, see:
    https://docs.djangoproject.com/en/dev/topics/http/middleware/
     """

def process_view(self, request, view_func, view_args, view_kwargs):
    if view_func.__name__ == '_shortcircuitmiddleware':
        return view_func(request, *view_args, **view_kwargs)
return None
Non-global middleware in Django