|
1
|
from django import template |
|
2
|
|
|
3
|
register = template.Library() |
|
4
|
|
|
5
|
|
|
6
|
@register.simple_tag(takes_context=True) |
|
7
|
def has_perm(context, perm_string): |
|
8
|
"""Check if the current user has a specific permission. Usage: {% has_perm 'projects.view_project' as can_view %}""" |
|
9
|
user = context.get("user") or context["request"].user |
|
10
|
if not user or not user.is_authenticated: |
|
11
|
return False |
|
12
|
if user.is_superuser: |
|
13
|
return True |
|
14
|
return user.has_perm(perm_string) |
|
15
|
|