Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#29220 closed New feature (invalid)

Auth: change user_passes_test func, new arg arg_position add

Reported by: Oscar Lopez Owned by: nobody
Component: contrib.auth Version: 1.11
Severity: Normal Keywords: Auth, user_passes_test, decorator
Cc: oscarjoselopez26@…, Oscar Lopez Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description (last modified by Oscar Lopez)

I had problems Add my decorations user_passes_test functions, because decorator _wrapped_view assume, the first arg is the request var parameter, when i call user_passes_test from a view into a class, that not work, it take the arg self look like a arg request

example:
@user_passes_test(role_client_check)

def index(self, request):

Fatal error: Class don't have attribute user.

The next new change work for Django 1.11
but i see that Django 2.X file, doesn't change a lot

Now i can call the auth:
@user_passes_test(role_client_check, arg_position=0)

def index(self, request):

arg_position can be specific when the first argument isn't "request"
arg_position is a integer with the position of request on view function

I created my own user_passes_test function to solve this feature.

Attachments (1)

decorators.py (3.1 KB ) - added by Oscar Lopez 7 years ago.
\django\contrib\auth\decorators.py just one file update

Download all attachments as: .zip

Change History (6)

by Oscar Lopez, 7 years ago

Attachment: decorators.py added

\django\contrib\auth\decorators.py just one file update

comment:1 by Oscar Lopez, 7 years ago

Description: modified (diff)
Type: UncategorizedNew feature

comment:2 by Oscar Lopez, 7 years ago

Cc: Oscar Lopez added

comment:3 by Nick Pope, 7 years ago

Resolution: invalid
Status: newclosed

It looks like you have misunderstood how this works.

When using a class-based view (CBV) you need to use UserPassesTestMixin instead of the @user_passes_test decorator.

(Alternatively, you can use @method_decorator to make this work - see https://stackoverflow.com/q/8082670/ - but the using the mixin is straightforward.)

Version 0, edited 7 years ago by Nick Pope (next)

comment:4 by Oscar Lopez, 7 years ago

Oh Yes, i'm Sorry!

in reply to:  3 comment:5 by Oscar Lopez, 7 years ago

Replying to Nick Pope:

It looks like you have misunderstood how this works.

When using a class-based view (CBV) you need to use UserPassesTestMixin instead of the @user_passes_test decorator.

(Alternatively, you can use @method_decorator to make this work - see https://stackoverflow.com/q/8082670/ - but using the mixin is straightforward.)

Oh y tried django.contrib.auth.mixins.UserPassesTestMixin but this work only when using class-based views, im going to try the stackoverflow method

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