Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Jacob Kaplan-Moss authored April 01, 2009
13  django/contrib/auth/decorators.py
@@ -56,8 +56,19 @@ def __init__(self, view_func, test_func, login_url=None, redirect_field_name=RED
56 56
         self.test_func = test_func
57 57
         self.login_url = login_url
58 58
         self.redirect_field_name = redirect_field_name
59  
-        update_wrapper(self, view_func)
60 59
         
  60
+        # We can't blindly apply update_wrapper because it udpates __dict__ and 
  61
+        # if the view function is already a _CheckLogin object then 
  62
+        # self.test_func and friends will get stomped. However, we also can't 
  63
+        # *not* update the wrapper's dict because then view function attributes
  64
+        # don't get updated into the wrapper. So we need to split the
  65
+        # difference: don't let update_wrapper update __dict__, but then update
  66
+        # the (parts of) __dict__ that we care about ourselves.
  67
+        update_wrapper(self, view_func, updated=())
  68
+        for k in view_func.__dict__:
  69
+            if k not in self.__dict__:
  70
+                self.__dict__[k] = view_func.__dict__[k]
  71
+
61 72
     def __get__(self, obj, cls=None):
62 73
         view_func = self.view_func.__get__(obj, cls)
63 74
         return _CheckLogin(view_func, self.test_func, self.login_url, self.redirect_field_name)
31  tests/regressiontests/decorators/tests.py
@@ -29,6 +29,7 @@ def fully_decorated(request):
29 29
 fully_decorated = never_cache(fully_decorated)
30 30
 
31 31
 # django.contrib.auth.decorators
  32
+# Apply user_passes_test twice to check #9474
32 33
 fully_decorated = user_passes_test(lambda u:True)(fully_decorated)
33 34
 fully_decorated = login_required(fully_decorated)
34 35
 fully_decorated = permission_required('change_world')(fully_decorated)
@@ -54,3 +55,33 @@ def test_attributes(self):
54 55
             self.assertEquals(fully_decorated.__name__, 'fully_decorated')
55 56
         self.assertEquals(fully_decorated.__doc__, 'Expected __doc__')
56 57
         self.assertEquals(fully_decorated.__dict__['anything'], 'Expected __dict__')
  58
+
  59
+    def test_user_passes_test_composition(self):
  60
+        """
  61
+        Test that the user_passes_test decorator can be applied multiple times
  62
+        (#9474).
  63
+        """
  64
+        def test1(user):
  65
+            user.decorators_applied.append('test1')
  66
+            return True
  67
+            
  68
+        def test2(user):
  69
+            user.decorators_applied.append('test2')
  70
+            return True
  71
+            
  72
+        def callback(request):
  73
+            return request.user.decorators_applied
  74
+
  75
+        callback = user_passes_test(test1)(callback)
  76
+        callback = user_passes_test(test2)(callback)
  77
+        
  78
+        class DummyUser(object): pass
  79
+        class DummyRequest(object): pass
  80
+        
  81
+        request = DummyRequest()
  82
+        request.user = DummyUser()
  83
+        request.user.decorators_applied = []
  84
+        response = callback(request)
  85
+        
  86
+        self.assertEqual(response, ['test2', 'test1'])
  87
+        

0 notes on commit e6ad4fb

Please sign in to comment.
Something went wrong with that request. Please try again.