Re: Middleware for models

2010-02-13 Thread Shawn Milochik
Karol, Check this out: http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser I used this so I could get the current user anywhere. My use for this is to know the request.user when the post_save signal fires so I can log the attributes of a model each time it's saved and know who did it

Re: Middleware for models

2010-02-13 Thread pbzRPA
Ok, I think I have sorted out my queryset problem, I wrote a custom manager: class UserBranchModelManager(models.Manager): def for_request(self, request): if settings.DO_BRANCH_FILTER: return super(UserBranchModelManager, self).get_query_set().filter(branch__pk = request.us

Re: Middleware for models

2010-02-13 Thread pbzRPA
Sorry one more thing, the above filters my querysets but then comes the get() method on each model. I use urls with pk's so a user could just try to change the url to a pk that does not belong to them and view it. To filter this out I run yet another function: def relation_branch_validation(reques

Re: Middleware for models

2010-02-13 Thread pbzRPA
Just so you have an idea what I am trying to do. :) I have a custom middleware that adds a userBranchID variable to each request according to the users branch in the User model. Now in each view when I use any queryset I have to do the following: def filter_branch(request, queryset): if settin

Re: Middleware for models

2010-02-13 Thread pbzRPA
Hi Shawn, I use Managers often to limit my querysets and add additional fields that I want dynamically calculated using sql. I just don't see how I can get the request object to the models.Manager so it knows what to filter by (user.uid). :( I know I could do a filter in each view queryset.object

Re: Middleware for models

2010-02-12 Thread Shawn Milochik
It looks like you might want to create a custom Manager class. As you alluded to, you definitely don't want to have to assume that every function that touches your models is doing all the required checking. http://docs.djangoproject.com/en/1.1/topics/db/managers/ I didn't really know what a Man