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.
* Attachment "decorators_sol1.py" added.
Solution 1
* Attachment "decorators_sol2.py" added.
Solution 2
* Attachment "decorators_sol3.py" added.
Solution 3
* cc: Iago González (added)
* needs_better_patch: 0 => 1
* needs_tests: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/33299#comment:1>
* 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>