From 5a050fb36de92452760077fdb9565cef5c6a5462 Mon Sep 17 00:00:00 2001 From: Ross Date: Wed, 25 Mar 2026 22:12:33 +0000 Subject: [PATCH] Add custom template filter 'has_group' and update question_link_header for improved user permission checks --- rapids/templates/rapids/question_link_header.html | 3 ++- rapids/templatetags/__init__.py | 0 rapids/templatetags/rapids_extras.py | 12 ++++++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 rapids/templatetags/__init__.py create mode 100644 rapids/templatetags/rapids_extras.py diff --git a/rapids/templates/rapids/question_link_header.html b/rapids/templates/rapids/question_link_header.html index b3d6c8f8..980a2cf1 100644 --- a/rapids/templates/rapids/question_link_header.html +++ b/rapids/templates/rapids/question_link_header.html @@ -6,12 +6,13 @@ Delete Edit Answers Add Note + {% load rapids_extras %} {% if request.user.is_superuser %} Admin Edit User answers {% endif %} {% comment %} Migrate to shorts (authors / rapid_checkers / superusers) {% endcomment %} - {% if request.user.is_superuser or request.user in question.author.all or request.user.groups.filter(name='rapid_checker').exists %} + {% if request.user.is_superuser or request.user in question.author.all or request.user|has_group:"rapid_checker" %} Migrate to Shorts {% endif %} {% if exam %} diff --git a/rapids/templatetags/__init__.py b/rapids/templatetags/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/rapids/templatetags/rapids_extras.py b/rapids/templatetags/rapids_extras.py new file mode 100644 index 00000000..f980512c --- /dev/null +++ b/rapids/templatetags/rapids_extras.py @@ -0,0 +1,12 @@ +from django import template + +register = template.Library() + +@register.filter(name='has_group') +def has_group(user, group_name): + if user is None: + return False + try: + return user.groups.filter(name=group_name).exists() + except Exception: + return False