From e6ad4fb901c4d3bbaff1ad01bb8cc93852c20e1e Mon Sep 17 00:00:00 2001 From: Jacob Kaplan-Moss Date: Wed, 1 Apr 2009 16:17:38 +0000 Subject: [PATCH] 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 --- django/contrib/auth/decorators.py | 13 +++++++++- tests/regressiontests/decorators/tests.py | 31 +++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/django/contrib/auth/decorators.py b/django/contrib/auth/decorators.py index 1371c62eea..b80aca10a3 100644 --- a/django/contrib/auth/decorators.py +++ b/django/contrib/auth/decorators.py @@ -56,8 +56,19 @@ class _CheckLogin(object): 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) diff --git a/tests/regressiontests/decorators/tests.py b/tests/regressiontests/decorators/tests.py index 0c434772f8..3c58637f1a 100644 --- a/tests/regressiontests/decorators/tests.py +++ b/tests/regressiontests/decorators/tests.py @@ -29,6 +29,7 @@ fully_decorated = cache_control(private=True)(fully_decorated) 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 @@ class DecoratorsTest(TestCase): 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']) +