[Django] #33299: Create new decorator `request_passes_test` or modify `user_passes_test` to send `request` parameter

22 views
Skip to first unread message

Django

unread,
Nov 18, 2021, 6:15:05 AM11/18/21
to django-...@googlegroups.com
#33299: Create new decorator `request_passes_test` or modify `user_passes_test` to
send `request` parameter
-------------------------------------+-------------------------------------
Reporter: Iago | Owner: nobody
González |
Type: New | Status: new
feature |
Component: | Version: 3.2
contrib.auth | Keywords: user_passes_test,
Severity: Normal | request, Auth
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-------------------------------------+-------------------------------------
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!

--
Ticket URL: <https://code.djangoproject.com/ticket/33299>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 18, 2021, 6:15:41 AM11/18/21
to django-...@googlegroups.com
#33299: Create new decorator `request_passes_test` or modify `user_passes_test` to
send `request` parameter
-------------------------------------+-------------------------------------
Reporter: Iago González | Owner: nobody
Type: New feature | Status: new
Component: contrib.auth | Version: 3.2
Severity: Normal | Resolution:
Keywords: user_passes_test, | Triage Stage:
request, Auth | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Iago González):

* Attachment "decorators_sol1.py" added.

Solution 1

Django

unread,
Nov 18, 2021, 6:15:54 AM11/18/21
to django-...@googlegroups.com
#33299: Create new decorator `request_passes_test` or modify `user_passes_test` to
send `request` parameter
-------------------------------------+-------------------------------------
Reporter: Iago González | Owner: nobody
Type: New feature | Status: new
Component: contrib.auth | Version: 3.2
Severity: Normal | Resolution:
Keywords: user_passes_test, | Triage Stage:
request, Auth | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Iago González):

* Attachment "decorators_sol2.py" added.

Solution 2

Django

unread,
Nov 18, 2021, 6:16:08 AM11/18/21
to django-...@googlegroups.com
#33299: Create new decorator `request_passes_test` or modify `user_passes_test` to
send `request` parameter
-------------------------------------+-------------------------------------
Reporter: Iago González | Owner: nobody
Type: New feature | Status: new
Component: contrib.auth | Version: 3.2
Severity: Normal | Resolution:
Keywords: user_passes_test, | Triage Stage:
request, Auth | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Iago González):

* Attachment "decorators_sol3.py" added.

Solution 3

Django

unread,
Nov 18, 2021, 6:17:29 AM11/18/21
to django-...@googlegroups.com
#33299: Create new decorator `request_passes_test` or modify `user_passes_test` to
send `request` parameter
-------------------------------------+-------------------------------------
Reporter: Iago González | Owner: nobody
Type: New feature | Status: new
Component: contrib.auth | Version: 3.2
Severity: Normal | Resolution:
Keywords: user_passes_test, | Triage Stage:
request, Auth | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1

Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Iago González):

* cc: Iago González (added)
* needs_better_patch: 0 => 1
* needs_tests: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/33299#comment:1>

Django

unread,
Nov 18, 2021, 6:56:54 AM11/18/21
to django-...@googlegroups.com
#33299: Add @request_passes_test decorator.

-------------------------------------+-------------------------------------
Reporter: Iago González | Owner: nobody
Type: New feature | Status: closed
Component: contrib.auth | Version: 3.2
Severity: Normal | Resolution: wontfix

Keywords: user_passes_test, | Triage Stage:
request, Auth | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* needs_better_patch: 1 => 0
* resolution: => wontfix
* needs_tests: 1 => 0


Comment:

> 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
[https://docs.djangoproject.com/en/stable/internals/contributing/bugs-and-
features/#requesting-features follow the guidelines with regards to
requesting features].

--
Ticket URL: <https://code.djangoproject.com/ticket/33299#comment:2>

Reply all
Reply to author
Forward
0 new messages