Skip to content

Instantly share code, notes, and snippets.

@hseritt
Created February 25, 2017 12:51
Show Gist options
  • Save hseritt/da57d60aa5259d3edde781e67334aae0 to your computer and use it in GitHub Desktop.
Save hseritt/da57d60aa5259d3edde781e67334aae0 to your computer and use it in GitHub Desktop.
Custom tag library to test group membership.
"""
Custom tag library to test group membership.
"""
from django import template
from django.contrib.auth.models import Group
from apps.contacts.models import Contact
register = template.Library()
@register.filter(name='is_in_group')
def is_in_group(user, group_name):
"""
Returns True if user is in a group.
"""
group = Group.objects.get(name=group_name)
return group in user.groups.all()
@register.filter(name='is_in_customer_org')
def is_in_customer_org(user):
"""
Returns True is user/contact belongs to an organization that is a customer type.
"""
contact = Contact.objects.get(user=user)
return contact.organization.org_type == 'Customer'
@register.filter(name='is_in_owner_org')
def is_in_owner_org(user):
"""
Returns True if user/contact is a member of an owner type organization.
"""
contact = Contact.objects.get(user=user)
return contact.organization.org_type == 'Owner'
@register.filter(name='is_in_partner_org')
def is_in_partner_org(user):
"""
Returns True if user/contact is a member of a partner type organization.
"""
contact = Contact.objects.get(user=user)
return contact.organization.org_type == 'Partner'
@register.filter(name='is_in_vendor_org')
def is_in_vendor_org(user):
"""
Returns True if user/contact is a member of a vendor type organization.
"""
contact = Contact.objects.get(user=user)
return contact.organization.org_type == 'Vendor'
@register.filter(name='has_project_read_access')
def has_project_read_access(user, project):
"""
Returns True if user is in a vendor org and is a member of a project.
"""
access = False
if is_in_vendor_org(user):
if user in project.members.all():
access = True
else:
access = False
else:
access = True
return access
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment