Checking group permissions in template code

7 views
Skip to first unread message

vida

unread,
May 31, 2007, 11:22:26 AM5/31/07
to Django developers
After reading the docs and looking for posts relating this problem
here and in .users, I found no "standard" way of checking for a
user's group permissions in the templates.

So what I did is to add the following method to auth.models.User
(yeah, yeah, it *should* be in appname.models.UserProfile):

def group_permissions(self):
class GroupPermissions:
def __init__(self, permissions):
for perm in permissions:
setattr(self, perm, True)

def __str__(self):
return 'The user belongs to the following groups: %s' % \
', '.join([k for k, v in self.__dict__.iteritems()])

return GroupPermissions(self.get_group_permissions())

which lets me write something like the following in my templates:
{% if user.group_permissions.permission_name %} draw custom menues for
this particular group here {% endif %}

So, is there already a way to do this and I missed it? If not, given
that it is apparently not advisable (is it?) to extend the
auth.models.User class, how about we add it?

-Facundo.

Russell Keith-Magee

unread,
May 31, 2007, 6:18:29 PM5/31/07
to django-d...@googlegroups.com
On 5/31/07, vida <facun...@gmail.com> wrote:
>
> After reading the docs and looking for posts relating this problem
> here and in .users, I found no "standard" way of checking for a
> user's group permissions in the templates.

Double posting to two groups like this is _really_ bad form. On top of
this, the django-developers group is intended for discussion of the
internals of Django itself.

If you're asking a "How do I do this" questions, django-users is the
place to do it.

Yours
Russ Magee %-)

vida

unread,
May 31, 2007, 7:25:24 PM5/31/07
to Django developers

On May 31, 6:18 pm, "Russell Keith-Magee" <freakboy3...@gmail.com>
wrote:

I meant to CC this group when I posted to users.

It's both a *how do I do this?*, and a *if there isn't a way already,
how about we do it this one?* thingy, so I thought it'd be appropriate
for the two groups. I think that much is apparent.

Thanks for the netiquette lesson thou :)

-Facundo.

Russell Keith-Magee

unread,
May 31, 2007, 8:39:52 PM5/31/07
to django-d...@googlegroups.com
On 6/1/07, vida <facun...@gmail.com> wrote:
>
> On May 31, 6:18 pm, "Russell Keith-Magee" <freakboy3...@gmail.com>
> > If you're asking a "How do I do this" questions, django-users is the
> > place to do it.
>
> I meant to CC this group when I posted to users.
>
> It's both a *how do I do this?*, and a *if there isn't a way already,
> how about we do it this one?* thingy, so I thought it'd be appropriate
> for the two groups. I think that much is apparent.

It's not. If you don't know how to do something, ask on django-users.
Proposing a solution for a problem is exceedingly premature when you
have already admitted that you don't know if a solution already
exists.

CC'ing the dev list is no better - your message is sent regardless of
how it is sent, and the group archive doesn't distinguish between To
and CC.

If, as a result of asking your original question, a design issue
arises, and needs internal discussion, _then_ start the second thread.
In many cases, the second thread won't even be required - all the
django-developers read django-users too, so quite often raising the
issue in the users list will get the problem addressed.

Sorry if this seems like I'm coming down hard on you personally - it's
not intended as a personal attack. We (the developers) need to keep a
hard line on issues like this or the value in having a low-volume
developer-only list evaporates.

Yours,
Russ Magee %-)

Reply all
Reply to author
Forward
0 new messages