From 2d04e39274ca35488dad90893852c983fb6cb636 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Sun, 24 Apr 2022 15:41:28 +0200 Subject: [PATCH] templatetags: introduce |has_role_uuid (#64495) --- debian/debian_config_common.py | 4 +--- hobo/context_processors.py | 2 +- hobo/templatetags/hobo.py | 12 ++++++++++++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/debian/debian_config_common.py b/debian/debian_config_common.py index e5f96c0..7b6eb5e 100644 --- a/debian/debian_config_common.py +++ b/debian/debian_config_common.py @@ -359,9 +359,7 @@ if PROJECT_NAME != 'wcs': if 'authentic2' not in INSTALLED_APPS: MELLON_ADAPTER = ('hobo.multitenant.mellon.MellonAdapter',) -if PROJECT_NAME in ('wcs', 'combo'): - TEMPLATES[0]['OPTIONS'].setdefault('builtins', []).append('hobo.templatetags.hobo') - +TEMPLATES[0]['OPTIONS'].setdefault('builtins', []).append('hobo.templatetags.hobo') if 'authentic2' not in INSTALLED_APPS: MELLON_DEFAULT_ASSERTION_CONSUMER_BINDING = 'artifact' diff --git a/hobo/context_processors.py b/hobo/context_processors.py index 1861999..288f952 100644 --- a/hobo/context_processors.py +++ b/hobo/context_processors.py @@ -144,7 +144,7 @@ def theme_base(request): # * theme_base will get evaluated when encountered in a template and return # a template string downloaded from settings.THEME_SKELETON_URL. # - # * theme_404 is identical but dedicated to 404 erorr pages. + # * theme_404 is identical but dedicated to 404 error pages. # # Both those variables are to be used by "slave" sites, authentic, # w.c.s., etc. diff --git a/hobo/templatetags/hobo.py b/hobo/templatetags/hobo.py index 10e6fa1..6521251 100644 --- a/hobo/templatetags/hobo.py +++ b/hobo/templatetags/hobo.py @@ -15,6 +15,7 @@ # along with this program. If not, see . import decimal +import sys from django import template from django.utils.translation import get_language @@ -59,3 +60,14 @@ def as_numeral_currency(number): return num2words(unlazy(number), lang=get_language(), to='currency') except (TypeError, ValueError, decimal.InvalidOperation): return '' + + +@register.filter +def has_role_uuid(user, role_uuid): + if not user or not user.is_authenticated: + return False + if 'authentic2' in sys.modules: + return user.roles.filter(uuid=role_uuid).exists() + elif 'wcs' in sys.modules: + return role_uuid in user.roles + return user.groups.filter(role__uuid=role_uuid).exists()