From af773a0afd6fb89258f425efd861b68167504cd6 Mon Sep 17 00:00:00 2001 From: shagi Date: Wed, 15 Nov 2023 10:19:54 +0100 Subject: [PATCH] feat: new templatetag to check if user has role --- gas/templatetags/gas_tags.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/gas/templatetags/gas_tags.py b/gas/templatetags/gas_tags.py index 786e0d8..f773416 100644 --- a/gas/templatetags/gas_tags.py +++ b/gas/templatetags/gas_tags.py @@ -5,6 +5,7 @@ from django import template from django.utils.html import mark_safe from ..sites import site +from ..models import UserRole register = template.Library() @@ -59,3 +60,10 @@ def pagination(request, page): @register.filter def to_json(data): return mark_safe(json.dumps(data)) + + +@register.filter +def has_role(user, role): + if user.is_superuser: + return True + return UserRole.objects.filter(role__in=(role, "admins"), user=user).exists()