Friday, March 27, 2015

Re: Global access to request.user

There is a reason that state is passed around from method to method,
rather than being stored as a global, because this is the correct way
to do things.

However, it sounds like the architectural design of your code is
flawed, as throwing around the request object indicates that you don't
have modularity. If you only need the user object, then why not
replace all instances of "request=request" with "user=request.user".
The alternative is to have some magic threadlocals which stores the
current user, but this is a particularly disgusting way of writing
code and I could not recommend it at all.

It would be better if you could explain a little more about how/where
this request object is being passed around, then we'd be in a better
position to give you the correct advice.

Cal


On Thu, Mar 26, 2015 at 10:38 AM, guettli <hv@tbz-pariv.de> wrote:
> I have a large legacy application which passes around the request to nearly
> every method.
>
> Reading this source is no fun.
>
> In about 95% of all cases "request" is not used, but "request.user" is
> needed for
> permission checking.
>
> Example: there is a drop down box with the list of possible actions the user
> can perform now.
> The list of actions is filtered according to the logged in request.user.
>
> I want to get rid of the ugly passing around "request" from method to
> method.
>
> I have some ideas in mind, but won't post them today. I am afraid that if I
> would,
> the discussion would be only pro/contra my solution. But I guess there is
> a third way :-)
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscribe@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/fc2c1d65-fa3d-461b-9846-3802cf153e15%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHKQagFg6LZ18gRKWxccPiv0VkMsA8pKFt%2BF%2BzgiPx5J-65BqA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment