Opened 7 years ago

Closed 6 years ago

Last modified 4 years ago

#9474 closed (fixed)

user_passes_test decorator cannot be applied twice

Reported by: msaelices Owned by: msaelices
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 msaelices 7 years ago.
Patch that fixes issue

Download all attachments as: .zip

Change History (6)

Changed 7 years ago by msaelices

Patch that fixes issue

comment:1 Changed 6 years ago by jacob

  • milestone set to 1.1
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 6 years ago by bthomas

  • Has patch set

comment:3 Changed 6 years ago by jacob

  • Resolution set to fixed
  • Status changed from new to closed

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

comment:4 Changed 6 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 4 years ago by jacob

  • milestone 1.1 deleted

Milestone 1.1 deleted

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