Opened 4 years ago
Closed 4 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)
Change History (5)
by , 4 years ago
| Attachment: | decorators_sol1.py added |
|---|
comment:1 by , 4 years ago
| Cc: | added |
|---|---|
| Needs tests: | set |
| Patch needs improvement: | set |
comment:2 by , 4 years ago
| Needs tests: | unset |
|---|---|
| Patch needs improvement: | unset |
| Resolution: | → wontfix |
| Status: | new → closed |
| Summary: | Create new decorator `request_passes_test` or modify `user_passes_test` to send `request` parameter → Add @request_passes_test decorator. |
The
user_passes_testis 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.
Solution 1