altough i'm not sure if thread locals are that clean .. i guess it's a
better way ..
thanks .. i was searching for something like that .. i wonder why i
haven't found that site :(
it seems there are tons more documentation in the wiki as i've
thought ;)

cu,
  herbert

On Feb 17, 3:22 pm, "Honza Král" <[EMAIL PROTECTED]> wrote:
> This doesn't seem very clean to me...
>
> I also need to have logged-in user available in my models, so I have
> written a custom middleware that stores the user in thread_locals:
>
> import threading
> _thread_locals = threading.local()
>
> def get_current_user():
>     return getattr( _thread_locals, 'user', None )
>
> class RequestMiddleware( object ):
>
>     def process_request( self, request ):
>
>         _thread_locals.user = request.user
>
> then, I can use get_current_user() anywhere in my application and it
> will provide me with the current user, for some fields I even have
> something like:
>
>   user = models.ForeignKey( User, default=get_current_user )
>
> I cannot take credit for this, I found it on
>
> http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser
>
> On 2/17/07, kahless <[EMAIL PROTECTED]> wrote:
>
>
>
>
>
> > hi,
>
> > i am creating a very simple board and wanted to add a method to my
> > models which would check if there are any new posts in a thread /
> > forum for a given user.. for this to work .. i obviously need the
> > currently logged in user in my models ..
>
> > but i didn't want to iterate through all my models in my view, since i
> > used the django.views.generic.list_detail.object_list which does the
> > paging and everything for me ..
>
> > so i thought the easiest way would be to allow adding of custom
> > initializers.. which would be called once a model was created by
> > django.db.models.query.QuerySet .. so i played around a little until
> > it worked for me:http://yourhell.com/~kahless/query_initializers_patch.diff
>
> > the usage is quite simple:
> > class MyModelInitializer(object):
> >   def __init__(self, request):
> >     self.request = request
>
> >   def init_model(self, model):
> >     model.do_init( self, self.request.user )
>
> > (my models would have a 'do_init' method which takes the user and
> > stores it internally)
>
> > the object_list would then get a queryset argument in my view like:
> > queryset =
> > Post.objects.filter( ..... ).add_initializer( MyModelInitializer( request ) 
> > )
>
> > and my templates could simply call {% if thread.hasNewPosts %} ...
>
> > so .. is this a clean solution ? or can this be done differently ?
> > and .. would there any chance this patch could find it's way into
> > django or does it break any main design philosophy ?
>
> > - i'm very new to python and django alike .. so there might be an
> > obvious other solution.
>
> > thanks & cu,
> >   Herbert Poul
>
> --
> Honza Kr?l
> E-Mail: [EMAIL PROTECTED]
> ICQ#:   107471613
> Phone:  +420 606 678585


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to