Re: New proposal for `user_passes_test` decorator logic

2021-11-29 Thread Iago González Rodríguez
Hi Adam,

Thanks for your response.

The only thing that makes the solution more complicated is the fact that I 
created a user_passes_test function to not harm anyone who is already using 
that function. In order to simplify this, we could replace  user_passes_test 
with 
the request_passes_test function.

def request_passes_test(test_func, login_url=None, 
redirect_field_name=REDIRECT_FIELD_NAME):
"""
Decorator for views that checks that the request passes the given test,
redirecting to the log-in page if necessary. The test should be a 
callable
that takes the request object and returns True if the request passes.
"""
def decorator(view_func):
@wraps(view_func)
def _wrapped_view(request, *args, **kwargs):
if test_func(request):
return view_func(request, *args, **kwargs)
path = request.build_absolute_uri()
resolved_login_url = resolve_url(login_url or 
settings.LOGIN_URL)
# If the login url is the same scheme and net location then just
# use the path as the "next" url.
login_scheme, login_netloc = urlparse(resolved_login_url)[:2]
current_scheme, current_netloc = urlparse(path)[:2]
if ((not login_scheme or login_scheme == current_scheme) and
(not login_netloc or login_netloc == current_netloc)):
path = request.get_full_path()
from django.contrib.auth.views import redirect_to_login
return redirect_to_login(
path, resolved_login_url, redirect_field_name)
return _wrapped_view
return decorator 

This function would allow to write the exact same test functions, but 
changing user to request.user. For example:

def login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME, 
login_url=None): 
 """ 
Decorator for views that checks that the user is logged in, redirecting 
to the log-in page if necessary. 
"""
actual_decorator = request_passes_test( 
lambda r: r.user.is_authenticated, 
login_url=login_url, 
redirect_field_name=redirect_field_name 
) 
if function: 
return actual_decorator(function) 
return actual_decorator 

It is much simpler to change user to request.user rather than writing a 
custom request_passes_test function in every project that anyone wants to 
use it, and also it is more generic while user_passes_test is much more 
specific for certain use cases. There are some use cases where having the 
request variable instead of the user can be much more useful, for example 
if you want to use the Django message framework to send an error message 
when certain user does not pass a test. Does it make sense that there is no 
option to show a message to the user when they fail a test and redirect 
them to login without showing them any information?

In my current project I already have created my own request_passes_test 
decorator, but honestly I think more people would benefit from having this 
decorator in the source code (at least +30 people according to this post 

).

I hope more people can participate on this topic and give their opinion.

Thanks again for your time.

Best regards,
Iago.

El sábado, 27 de noviembre de 2021 a las 12:04:24 UTC+1, Adam Johnson 
escribió:

> Hi Iago
>
> Thanks for writing to the list.
>
> The proposal feels like complicating the decorator too much to me. If you 
> simplify the logic for the redirect for your particular use case, you can 
> implement your own decorator in a handful of lines. I think that’s 
> sufficient.
>
> Adam
>
> On Fri, 19 Nov 2021 at 10:40, Iago González Rodríguez  
> wrote:
>
>> Hi everyone, this is my first proposal so please don't be too hard on me 
>> :)
>>
>> *Context:*
>> I was implementing one of my first websites using Django, and when I 
>> tried to add the logic for restricting access of certain views to some 
>> users, I discovered this `user_passes_test` decorator, which is a pretty 
>> awesome decorator. But then in my test function I needed to use the Django 
>> messages framework to send a message when the user cannot access the view, 
>> but I realized that `user_passes_test` only sends the user object, and 
>> there is no option to pass the request object.
>>
>> I searched my problem on the Internet and I found a few related issues:
>> 1) How to pass Django request object in user_passes_test... - 
>> Stackoverflow 
>> 
>> 2) Show error message when decorator fails - Stackoverflow 
>> 
>> And I didn't search more because I understood that there is only one 
>> solution right now: overwrite

Re: Can I help?

2021-11-29 Thread 'Adam Johnson' via Django developers (Contributions to Django itself)
Welcome!

There are many different ways to contribute to Django - the forum,
blogging, translating, documenting, writing code, and more. Our
Contributing Guide can help you get started with many of these:
https://docs.djangoproject.com/en/stable/internals/contributing/

If you’re looking to work with the code base (for documentation or code),
check out the “Advice for New Contributors” section:
https://docs.djangoproject.com/en/stable/internals/contributing/new-contributors/
. Then see if you can work through the “Writing Your First Patch” tutorial:
https://docs.djangoproject.com/en/stable/intro/contributing/ .

If you get stuck or have questions, post back here or in the “Mentorship”
section on the forum:
https://forum.djangoproject.com/c/internals/mentorship/10

Hope that helps,

Adam

On Sun, 28 Nov 2021 at 13:41, Allen Jonathan 
wrote:

> Dear Django Contributors,
>
> Hello! My name is Allen Jonathan. I'd love to contribute to the Django
> framework. I have a decent knowledge of python and a little of Django too.
> I'm a fast learner and can learn on the go.  Please guide me on how to get
> started (even if its documentation or fixing easy bugs).
>
> Best regards,
> Allen Jonathan.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django developers (Contributions to Django itself)" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-developers+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-developers/81dfad1d-777c-49ca-bacf-430d1c325d8bn%40googlegroups.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/CAMyDDM3_kZ-PXVEFgYGQ6jnAD%2BHR7pE_9uN9Drdnyf0CvC-gTw%40mail.gmail.com.