Christoph Zwerschke wrote:
> Janzert schrieb:
>> 2. Why not something like the following (untested code)?
>>      @property
>>      def permissions(self):
>>          p_q = session.query(Permission).join(['tg_group', 'tg_user'])
>>          p_q.filter_by(user_id = self.user_id)
>>      return p_q.all()
> 
> I would write something like that in a long statement, without the 
> arbitrary p_q variable and avoid using hard coded table names.
> 
> The other problem is that this code doesn't work at all.
> 

Like I said it was untested. :/

> Also, the method must return a set, not a list with duplicates.
> 
> There are basically two ways to collect the permissions via SQL.
> 
> Either you make the two joins, creating all possible ways a user can 
> have a certain permission, and then remove the duplicates, like that:
> 
>      def permissions(self):
>          return set(Permission.query.join(Permission.groups,
>              Group.users).filter_by(user_id=self.user_id).all())
> 
> Or you create the query using exist clauses, so that the query does not 
> produce any duplicates in the first place:
> 
>      def permissions(self):
>          return set(Permission.query.filter(Permission.groups.any(
>              Group.users.any(User.user_id==self.user_id))).all())
> 
> The problem with this latter solution only works with SA >= 0.4 (and the 
> former solution even only with SA >= 0.5).
> 
> (Also, both solutions do a last join with the user table that is 
> actually unnecessary. I currently see no elegant way to avoid this. It 
> does not harm much, though.)
> 
> -- Christoph
> 

My understanding and testing seem to show that an inner join doesn't
return duplicates. Wrapping the result in a set doesn't seem necessary.

After playing around with SA 0.3.10, 0.4.6 and 0.5.0beta1 and taking
into account your comments I came up with this.

     def permissions(self):
         return Permission.query.join(["groups",
             "users"]).filter_by(user_id=self.user_id).all()
     permissions = property(permissions)

I tested by setting up a user belonging to two groups with each of them
containing two permissions. One of the permissions was unique to the
group and one was the same in both groups.

I put a diff from a TG 1.0.5 quickstarted project to what I used for
testing at http://paste.turbogears.org/paste/3056 and the full project
at http://janzert.com/files/jointest.zip

If this seems the right way to go I can create a trac issue and try to
work up a real patch.

Janzert


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

Reply via email to