summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2007-07-05 11:10:27 +0000
committerMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2007-07-05 11:10:27 +0000
commit05de38151239b0a89f5aaec6808bbc5318cee5d1 (patch)
tree682ad710dedf3ec4ecfd1eb368bbd273d479df19
parent79e914e4967adc3030be7044f502e37bbb4ba88e (diff)
Fixed #1015 -- Fixed decorator_from_middleware to return a real decorator 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
-rw-r--r--django/utils/decorators.py25
1 files changed, 24 insertions, 1 deletions
diff --git a/django/utils/decorators.py b/django/utils/decorators.py
index 1c6cc8c7de..54014a82b5 100644
--- a/django/utils/decorators.py
+++ b/django/utils/decorators.py
@@ -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)