Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #9474: user_passes_test may now be applied multiple times.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10328 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit e6ad4fb901c4d3bbaff1ad01bb8cc93852c20e1e 1 parent c398566
@jacobian jacobian authored
View
13 django/contrib/auth/decorators.py
@@ -56,8 +56,19 @@ def __init__(self, view_func, test_func, login_url=None, redirect_field_name=RED
self.test_func = test_func
self.login_url = login_url
self.redirect_field_name = redirect_field_name
- update_wrapper(self, view_func)
+ # We can't blindly apply update_wrapper because it udpates __dict__ and
+ # if the view function is already a _CheckLogin object then
+ # self.test_func and friends will get stomped. However, we also can't
+ # *not* update the wrapper's dict because then view function attributes
+ # don't get updated into the wrapper. So we need to split the
+ # difference: don't let update_wrapper update __dict__, but then update
+ # the (parts of) __dict__ that we care about ourselves.
+ update_wrapper(self, view_func, updated=())
+ for k in view_func.__dict__:
+ if k not in self.__dict__:
+ self.__dict__[k] = view_func.__dict__[k]
+
def __get__(self, obj, cls=None):
view_func = self.view_func.__get__(obj, cls)
return _CheckLogin(view_func, self.test_func, self.login_url, self.redirect_field_name)
View
31 tests/regressiontests/decorators/tests.py
@@ -29,6 +29,7 @@ def fully_decorated(request):
fully_decorated = never_cache(fully_decorated)
# django.contrib.auth.decorators
+# Apply user_passes_test twice to check #9474
fully_decorated = user_passes_test(lambda u:True)(fully_decorated)
fully_decorated = login_required(fully_decorated)
fully_decorated = permission_required('change_world')(fully_decorated)
@@ -54,3 +55,33 @@ def test_attributes(self):
self.assertEquals(fully_decorated.__name__, 'fully_decorated')
self.assertEquals(fully_decorated.__doc__, 'Expected __doc__')
self.assertEquals(fully_decorated.__dict__['anything'], 'Expected __dict__')
+
+ def test_user_passes_test_composition(self):
+ """
+ Test that the user_passes_test decorator can be applied multiple times
+ (#9474).
+ """
+ def test1(user):
+ user.decorators_applied.append('test1')
+ return True
+
+ def test2(user):
+ user.decorators_applied.append('test2')
+ return True
+
+ def callback(request):
+ return request.user.decorators_applied
+
+ callback = user_passes_test(test1)(callback)
+ callback = user_passes_test(test2)(callback)
+
+ class DummyUser(object): pass
+ class DummyRequest(object): pass
+
+ request = DummyRequest()
+ request.user = DummyUser()
+ request.user.decorators_applied = []
+ response = callback(request)
+
+ self.assertEqual(response, ['test2', 'test1'])
+
Please sign in to comment.
Something went wrong with that request. Please try again.