About async version of the login_required decorator

Hi guys, this is the ticket and this is my pull request. I seem to be confused. Is it right that I need to make the login_required support the async view function rather than develop a new alogin_required ?

I think that if we make the login_required support the async view function, it will return a sync function, if the view function is a sync function, and it will return a coroutine function, if the view function is a coroutine function. In that case, the question is, a non-io sync view function will be decorated as a sync function with database io. I think this is unreasonable. How do you guys think about this? Do we need to continue to make the login_required support the async view function? Feel free to chat.

Hi @HappyDingning.

Thanks for continuing the effort here. :gift:

Absolutely though[*], we want to adapt the existing decorators to handle both types of view, rather than duplicating the API here.

Using iscoroutinefunction branching is often minimal, but even if extensive it’s much better that we encapsulate that, rather than expect the user to constantly make decisions over imports depending on which kind of view they’re writing.

We want decorators that are general purpose. user_passes_test is the perfect example. By not making that flexible enough to handle either case, we’d be forced to duplicate the whole API down for the dependent decorators. That’s not great IMO. (Yes, async introduces the coloured functions problem, but no reason to just surrender :stuck_out_tongue_winking_eye:)

I can imagine the complaint coming up that in so and so case I need to do so and so, which needs to be async only… — I suspect that’s pretty rare TBH. In such cases Write a custom check isn’t too much to ask. (login_required for example is await request.auser().is_authenticated — its hardly the end of the world if I have a performance critical async-only-fragile bit of code in play…)

I hope that makes sense?

Kind regards,
Carlton

[*]: short of some clear demonstration of non-viability that’s not in sight as yet.

OK, thank you for your patient reply, I am convinced by you. I will modify my pull request to make
the login_required support async views.

1 Like

Thanks @HappyDingning! :gift:

Please do ping me if you want any input. (Though you seem to be doing well on your own. :wink:)