Skip to content

Instantly share code, notes, and snippets.

@isDipesh
Last active June 24, 2016 12:47
Show Gist options
  • Save isDipesh/6625339 to your computer and use it in GitHub Desktop.
Save isDipesh/6625339 to your computer and use it in GitHub Desktop.
Django template tag to check to see if the currently logged in user belongs to one or more groups. Supports {% else %} block. Mod of http://djangosnippets.org/snippets/2736/ to support multi-word group names (with single/double quotes, e.g. {% ifusergroup 'Store Keeper' %}).
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 one or more groups
Requires the Django authentication contrib app and middleware.
Usage: {% ifusergroup Admins %} ... {% endifusergroup %}, or
{% ifusergroup Admins Clients Programmers Managers %} ... {% else %} ... {% endifusergroup %}
"""
try:
tokensp = token.split_contents()
groups = []
groups += tokensp[1:]
except ValueError:
raise template.TemplateSyntaxError("Tag 'ifusergroup' requires at least 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(groups, nodelist_true, nodelist_false)
class GroupCheckNode(template.Node):
def __init__(self, groups, nodelist_true, nodelist_false):
self.groups = groups
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)
allowed = False
for checkgroup in self.groups:
if checkgroup.startswith('"') and checkgroup.endswith('"'):
checkgroup = checkgroup[1:-1]
if checkgroup.startswith("'") and checkgroup.endswith("'"):
checkgroup = checkgroup[1:-1]
try:
group = Group.objects.get(name=checkgroup)
except Group.DoesNotExist:
break
if group in user.groups.all():
allowed = True
break
if allowed:
return self.nodelist_true.render(context)
else:
return self.nodelist_false.render(context)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment