From 44f554c2b412d0ad24823d11e65d890c444c1c2c Mon Sep 17 00:00:00 2001 From: Tomasz Kwiatkowski <41270545+geoff128@users.noreply.github.com> Date: Fri, 29 Nov 2024 23:45:14 +0100 Subject: [PATCH] Move user_badge to forum module (#432) --- oioioi/contests/templatetags/get_user_name.py | 30 --------------- oioioi/forum/templates/forum/category.html | 1 + .../forum/thread-element-footer.html | 1 + oioioi/forum/templatetags/__init__.py | 0 oioioi/forum/templatetags/user_badge.py | 37 +++++++++++++++++++ 5 files changed, 39 insertions(+), 30 deletions(-) create mode 100644 oioioi/forum/templatetags/__init__.py create mode 100644 oioioi/forum/templatetags/user_badge.py diff --git a/oioioi/contests/templatetags/get_user_name.py b/oioioi/contests/templatetags/get_user_name.py index 2bb04b02c..7e83200ef 100644 --- a/oioioi/contests/templatetags/get_user_name.py +++ b/oioioi/contests/templatetags/get_user_name.py @@ -4,8 +4,6 @@ from oioioi.base.utils import get_user_display_name from oioioi.contests.utils import can_see_personal_data, is_contest_basicadmin -from oioioi.forum.utils import is_forum_moderator -from django.utils.translation import gettext_lazy as _ register = template.Library() @@ -79,31 +77,3 @@ def public_name(parser, token): def full_name(parser, token): (user, asvar) = _get_name(parser, token, "full_name") return FullNameUserInfoLinkNode(user, asvar) - - -class BadgeNode(Node): - ''' - Returns a badge for a user if they are a moderator. - ''' - def __init__(self, target_user, asvar): - self.target_user = Variable(target_user) - self.asvar = asvar - - def render(self, context): - user = self.target_user.resolve(context) - if is_forum_moderator(context['request'], user): - badge = _(" (moderator)") - else: - badge = "" - - if self.asvar: - context[self.asvar] = str(badge) - return "" - else: - return str(badge) - - -@register.tag -def user_badge(parser, token): - (user, asvar) = _get_name(parser, token, "user_badge") - return BadgeNode(user, asvar) diff --git a/oioioi/forum/templates/forum/category.html b/oioioi/forum/templates/forum/category.html index ddc9ec902..46c6362af 100644 --- a/oioioi/forum/templates/forum/category.html +++ b/oioioi/forum/templates/forum/category.html @@ -3,6 +3,7 @@ {% load check_perm %} {% load get_user_name %} {% load pagination_tags %} +{% load user_badge %} {% block title %}{% trans "Category" %}: {{ category.name }}{% endblock %} diff --git a/oioioi/forum/templates/forum/thread-element-footer.html b/oioioi/forum/templates/forum/thread-element-footer.html index 8977a021e..cb59e1c22 100644 --- a/oioioi/forum/templates/forum/thread-element-footer.html +++ b/oioioi/forum/templates/forum/thread-element-footer.html @@ -1,6 +1,7 @@ {% load i18n %} {% load check_perm %} {% load get_user_name %} +{% load user_badge %} {% url 'forum_post_edit' contest_id=contest.id category_id=category.id thread_id=thread.id post_id=post.id as forum_post_edit_url %} {% url 'forum_post_delete' contest_id=contest.id category_id=category.id thread_id=thread.id post_id=post.id as forum_post_delete_url %} diff --git a/oioioi/forum/templatetags/__init__.py b/oioioi/forum/templatetags/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/oioioi/forum/templatetags/user_badge.py b/oioioi/forum/templatetags/user_badge.py new file mode 100644 index 000000000..f9aed5926 --- /dev/null +++ b/oioioi/forum/templatetags/user_badge.py @@ -0,0 +1,37 @@ +from django import template +from django.template import Node, Variable +from django.utils.translation import gettext_lazy as _ + +from oioioi.contests.templatetags.get_user_name import _get_name +from oioioi.forum.utils import is_forum_moderator + +register = template.Library() + + +class BadgeNode(Node): + ''' + Returns a badge for a user if they are a moderator. + ''' + + def __init__(self, target_user, asvar): + self.target_user = Variable(target_user) + self.asvar = asvar + + def render(self, context): + user = self.target_user.resolve(context) + if is_forum_moderator(context['request'], user): + badge = _(" (moderator)") + else: + badge = "" + + if self.asvar: + context[self.asvar] = str(badge) + return "" + else: + return str(badge) + + +@register.tag +def user_badge(parser, token): + (user, asvar) = _get_name(parser, token, "user_badge") + return BadgeNode(user, asvar)