Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #1015 -- Fixed decorator_from_middleware to return a real decor…

…ator even

when arguments are given. This looks a bit ugly, but it's fully backwards
compatible and all the extra work is done at import time, so it shouldn't have
any real performance impact.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@5619 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 05de38151239b0a89f5aaec6808bbc5318cee5d1 1 parent 79e914e
Malcolm Tredinnick malcolmt authored
Showing with 24 additions and 1 deletion.
  1. +24 −1 django/utils/decorators.py
25 django/utils/decorators.py
View
@@ -1,12 +1,35 @@
"Functions that help with dynamically creating decorators for views."
+import types
+
def decorator_from_middleware(middleware_class):
"""
Given a middleware class (not an instance), returns a view decorator. This
lets you use middleware functionality on a per-view basis.
"""
- def _decorator_from_middleware(view_func, *args, **kwargs):
+ def _decorator_from_middleware(*args, **kwargs):
+ has_func = True
+ try:
+ view_func = kwargs.pop('view_func')
+ except KeyError:
+ if len(args):
+ view_func, args = args[0], args[1:]
+ else:
+ has_func = False
+ if not (has_func and isinstance(view_func, types.FunctionType)):
+ # For historical reasons, these decorators are also called as
+ # dec(func, *args) instead of dec(*args)(func). This branch handles
+ # the backwards compatibility.
+ if has_func:
+ args = (view_func,) + args
+ middleware = middleware_class(*args, **kwargs)
+
+ def decorator_func(fn):
+ return _decorator_from_middleware(fn, *args, **kwargs)
+ return decorator_func
+
middleware = middleware_class(*args, **kwargs)
+
def _wrapped_view(request, *args, **kwargs):
if hasattr(middleware, 'process_request'):
result = middleware.process_request(request)
Please sign in to comment.
Something went wrong with that request. Please try again.