Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[1.1.X] Fixed #13093 -- Updated some decorators and the decorator_fro…

…m_middleware function to allow callable classes to be decorated. Thanks to Brian Neal for the report.

Backport of r12762 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@12763 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 9137c54353f3356e0f65e0218016e92c5bc9cc43 1 parent 705d6cc
@freakboy3742 freakboy3742 authored
View
13 django/utils/decorators.py
@@ -2,9 +2,16 @@
import types
try:
- from functools import wraps
+ from functools import wraps, WRAPPER_ASSIGNMENTS
except ImportError:
- from django.utils.functional import wraps # Python 2.3, 2.4 fallback.
+ from django.utils.functional import wraps, WRAPPER_ASSIGNMENTS # Python 2.3, 2.4 fallback.
+
+def available_attrs(fn):
+ """
+ Return the list of functools-wrappable attributes on a callable.
+ This is required as a workaround for http://bugs.python.org/issue3445.
+ """
+ return tuple(a for a in WRAPPER_ASSIGNMENTS if hasattr(fn, a))
def decorator_from_middleware(middleware_class):
"""
@@ -57,5 +64,5 @@ def _wrapped_view(request, *args, **kwargs):
if result is not None:
return result
return response
- return wraps(view_func)(_wrapped_view)
+ return wraps(view_func, assigned=available_attrs(view_func))(_wrapped_view)
return _decorator_from_middleware
View
6 django/views/decorators/cache.py
@@ -16,7 +16,7 @@
except ImportError:
from django.utils.functional import wraps # Python 2.3, 2.4 fallback.
-from django.utils.decorators import decorator_from_middleware
+from django.utils.decorators import decorator_from_middleware, available_attrs
from django.utils.cache import patch_cache_control, add_never_cache_headers
from django.middleware.cache import CacheMiddleware
@@ -31,7 +31,7 @@ def _cache_controlled(request, *args, **kw):
patch_cache_control(response, **kwargs)
return response
- return wraps(viewfunc)(_cache_controlled)
+ return wraps(viewfunc, assigned=available_attrs(viewfunc))(_cache_controlled)
return _cache_controller
@@ -44,4 +44,4 @@ def _wrapped_view_func(request, *args, **kwargs):
response = view_func(request, *args, **kwargs)
add_never_cache_headers(response)
return response
- return wraps(view_func)(_wrapped_view_func)
+ return wraps(view_func, assigned=available_attrs(view_func))(_wrapped_view_func)
View
4 django/views/decorators/http.py
@@ -11,7 +11,7 @@
from datetime import timedelta
from email.Utils import formatdate
-from django.utils.decorators import decorator_from_middleware
+from django.utils.decorators import decorator_from_middleware, available_attrs
from django.utils.http import parse_etags, quote_etag
from django.middleware.http import ConditionalGetMiddleware
from django.http import HttpResponseNotAllowed, HttpResponseNotModified, HttpResponse
@@ -35,7 +35,7 @@ def inner(request, *args, **kwargs):
if request.method not in request_method_list:
return HttpResponseNotAllowed(request_method_list)
return func(request, *args, **kwargs)
- return wraps(func)(inner)
+ return wraps(func, assigned=available_attrs(func))(inner)
return decorator
require_GET = require_http_methods(["GET"])
View
5 django/views/decorators/vary.py
@@ -4,6 +4,7 @@
from django.utils.functional import wraps # Python 2.3, 2.4 fallback.
from django.utils.cache import patch_vary_headers
+from django.utils.decorators import available_attrs
def vary_on_headers(*headers):
"""
@@ -21,7 +22,7 @@ def inner_func(*args, **kwargs):
response = func(*args, **kwargs)
patch_vary_headers(response, headers)
return response
- return wraps(func)(inner_func)
+ return wraps(func, assigned=available_attrs(func))(inner_func)
return decorator
def vary_on_cookie(func):
@@ -37,4 +38,4 @@ def inner_func(*args, **kwargs):
response = func(*args, **kwargs)
patch_vary_headers(response, ('Cookie',))
return response
- return wraps(func)(inner_func)
+ return wraps(func, assigned=available_attrs(func))(inner_func)
View
6 tests/regressiontests/utils/decorators.py
@@ -11,3 +11,9 @@ def test_process_view_middleware(self):
Test a middleware that implements process_view.
"""
self.client.get('/utils/xview/')
+
+ def test_callable_process_view_middleware(self):
+ """
+ Test a middleware that implements process_view, operating on a callable class.
+ """
+ self.client.get('/utils/class_xview/')
View
1  tests/regressiontests/utils/urls.py
@@ -4,4 +4,5 @@
urlpatterns = patterns('',
(r'^xview/$', views.xview),
+ (r'^class_xview/$', views.class_xview),
)
View
7 tests/regressiontests/utils/views.py
@@ -8,3 +8,10 @@
def xview(request):
return HttpResponse()
xview = xview_dec(xview)
+
+
+class ClassXView(object):
+ def __call__(self, request):
+ return HttpResponse()
+
+class_xview = xview_dec(ClassXView())
Please sign in to comment.
Something went wrong with that request. Please try again.