Opened 2 years ago

Closed 2 years ago

#33299 closed New feature (wontfix)

Add @request_passes_test decorator.

Reported by: Iago González Owned by: nobody
Component: contrib.auth Version: 3.2
Severity: Normal Keywords: user_passes_test, request, Auth
Cc: Iago González Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

The user_passes_test is a little bit limited when used for restricting the views. For example: you cannot use the Django messages framework, because you cannot access the request object.

I suggest creating a request_passes_test to allow the creation of more advanced restrictions and logic.

Other developers that use Django also need this feature:
https://stackoverflow.com/questions/11872560/how-to-pass-django-request-object-in-user-passes-test-decorator-callable-functio
https://stackoverflow.com/questions/16375023/show-error-message-when-decorator-fails

I will add three solutions for this problem. These were quick solutions not tested, so they can have some errors; but I'm willing to open a Pull Request and improve the better approach. My favourite one is the solution 1, because it uses an abstract function that could be reused for creating both user_passes_test and request_passes_test. The other 2 solutions are uglier, but I add them just in case.

Thanks!

Attachments (3)

decorators_sol1.py (3.8 KB ) - added by Iago González 2 years ago.
Solution 1
decorators_sol2.py (2.9 KB ) - added by Iago González 2 years ago.
Solution 2
decorators_sol3.py (4.1 KB ) - added by Iago González 2 years ago.
Solution 3

Download all attachments as: .zip

Change History (5)

by Iago González, 2 years ago

Attachment: decorators_sol1.py added

Solution 1

by Iago González, 2 years ago

Attachment: decorators_sol2.py added

Solution 2

by Iago González, 2 years ago

Attachment: decorators_sol3.py added

Solution 3

comment:1 by Iago González, 2 years ago

Cc: Iago González added
Needs tests: set
Patch needs improvement: set

comment:2 by Mariusz Felisiak, 2 years ago

Needs tests: unset
Patch needs improvement: unset
Resolution: wontfix
Status: newclosed
Summary: Create new decorator `request_passes_test` or modify `user_passes_test` to send `request` parameterAdd @request_passes_test decorator.

The user_passes_test is a little bit limited when used for restricting the views.

Thanks for this ticket, however I wouldn't say it's limited. It works as documented and intended, user_passes_test checks whether an user passes test or not.

This is a proposition to add a new decorator that will do the same but for a request. I don't see a particular need to include this with Django. It won't be used by Django itself, and can be implemented in your project if you need it.

Please first start a discussion on the DevelopersMailingList, where you'll reach a wider audience and see what other think, and follow the guidelines with regards to requesting features.

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