Login

User groups template tag

Author:
hijinks
Posted:
June 18, 2009
Language:
Python
Version:
1.0
Tags:
tag templatetag user auth group
Score:
4 (after 4 ratings)

This tag builds on top of the ifusergroup/else tag, fixes a small bug and introduces support for else blocks. This adds a way to provide multiple groups via group1|group2|group3

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from django import template
from django.template import resolve_variable, NodeList
from django.contrib.auth.models import Group

register = template.Library()

@register.tag()
def ifusergroup(parser, token):
    """ Check to see if the currently logged in user belongs to a specific
    group. Requires the Django authentication contrib app and middleware.

    Usage: {% ifusergroup Admins %} ... {% endifusergroup %}, or
           {% ifusergroup Admins|Group1|Group2 %} ... {% endifusergroup %}, or
           {% ifusergroup Admins %} ... {% else %} ... {% endifusergroup %}

    """
    try:
        tag, group = token.split_contents()
    except ValueError:
        raise template.TemplateSyntaxError("Tag 'ifusergroup' requires 1 argument.")
    
    nodelist_true = parser.parse(('else', 'endifusergroup'))
    token = parser.next_token()
    
    if token.contents == 'else':
        nodelist_false = parser.parse(('endifusergroup',))
        parser.delete_first_token()
    else:
        nodelist_false = NodeList()
    
    return GroupCheckNode(group, nodelist_true, nodelist_false)


class GroupCheckNode(template.Node):
    def __init__(self, group, nodelist_true, nodelist_false):
        self.group = group
        self.nodelist_true = nodelist_true
        self.nodelist_false = nodelist_false
    def render(self, context):
        user = resolve_variable('user', context)
        
        if not user.is_authenticated():
            return self.nodelist_false.render(context)
            
        try:
            for group in self.group.split("|"):
                
                if Group.objects.get(name=group) in user.groups.all():
                    return self.nodelist_true.render(context)

        except Group.DoesNotExist:
            return self.nodelist_false.render(context)
            
        
        return self.nodelist_false.render(context)

More like this

  1. User group template tag with "else" block support by GomoX 7 years, 7 months ago
  2. User groups template tag with "else" block support (Allow checking several groups) by br0th3r 2 years, 11 months ago
  3. User group template tag by jdriscoll 7 years, 9 months ago
  4. User groups template tag by davea 3 years, 5 months ago
  5. Template tag: Group variables into list by Killarny 6 years ago

Comments

stringfellow (on February 28, 2011):

A change at line 36 would help for names with spaces:

if group.startswith('"') and group.endswith('"'): self.group = group[1:-1] else: self.group = group

#

Please login first before commenting.