From 05de38151239b0a89f5aaec6808bbc5318cee5d1 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Thu, 5 Jul 2007 11:10:27 +0000 Subject: [PATCH] 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 --- django/utils/decorators.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) 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)