Opened 8 years ago

Closed 8 years ago

Last modified 5 years ago

#9474 closed (fixed)

user_passes_test decorator cannot be applied twice

Reported by: Manuel Saelices Owned by: Manuel Saelices
Component: contrib.auth Version: 1.0
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Look at this code:

from django.contrib.auth.decorators import user_passes_test

def decorator1(view_func=None):
    def _test_func(user, *args, **kwargs):
        print 'decorator1 test func'
        return True
    return user_passes_test(_test_func)(view_func)

def decorator2(view_func=None):
    def _test_func(user, *args, **kwargs):
        print 'decorator2 test func'
        return True
    return user_passes_test(_test_func)(view_func)

@decorator1
@decorator2
def book_list(request):
    books = Book.objects.all()
    return render_to_response('books/book_list.html', {'books': books})

If you call book_list view, decorated both with decorator1 and decorator2, only decorator2 will be applied. For this exampled, only will appear in django console decorator2 test func.

This is for the use of update_wrapper function in _CheckLogin class in django/contrib/auth/decorators.py, that override __dict__ of function wrapped. Second decorator definition override self.view_func attribute of the other decorator (both are _CheckLogin objects).

Look at the patch for more details.

Attachments (1)

user_passes_test_decorator_r9295.diff (664 bytes) - added by Manuel Saelices 8 years ago.
Patch that fixes issue

Download all attachments as: .zip

Change History (6)

Changed 8 years ago by Manuel Saelices

Patch that fixes issue

comment:1 Changed 8 years ago by Jacob

milestone: 1.1
Triage Stage: UnreviewedAccepted

comment:2 Changed 8 years ago by Bob Thomas

Has patch: set

comment:3 Changed 8 years ago by Jacob

Resolution: fixed
Status: newclosed

(In [10328]) Fixed #9474: user_passes_test may now be applied multiple times.

comment:4 Changed 8 years ago by Jacob

(In [10329]) [1.0.X] Fixed #9474: user_passes_test may now be applied multiple times. Backport of r10328 from trunk.

comment:5 Changed 5 years ago by Jacob

milestone: 1.1

Milestone 1.1 deleted

Note: See TracTickets for help on using tickets.
Back to Top