Skip to content

Commit

Permalink
show username and group icons in navbar
Browse files Browse the repository at this point in the history
  • Loading branch information
tykling committed May 26, 2024
1 parent 654d91f commit e45d4d5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/templates/base.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{% load static %}
{% load django_bootstrap5 %}
{% load django_htmx %}
{% load bma_utils %}
<!DOCTYPE html>
<html lang="en">
<head>
Expand Down Expand Up @@ -91,7 +92,12 @@
{% if user.is_anonymous %}
<li class="nav-item"><a class="nav-link{% if request.resolver_match.url_name == "account_login" %} active{% endif %}" href="{% url 'account_login' %}">Login</a></li>
{% else %}
<li class="nav-item"><a class="nav-link{% if request.resolver_match.url_name == "account_logout" %} active{% endif %}" href="{% url 'account_logout' %}">Logout</a></li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">{% get_group_icons %} {{ request.user.username }}</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown">
<li><a class="dropdown-item" href="{% url 'account_logout' %}">Logout</a></li>
</ul>
</li>
{% endif %}

</ul>
Expand Down
23 changes: 23 additions & 0 deletions src/utils/templatetags/bma_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""Various utility template tags for the BMA project."""

from django import template
from django.conf import settings
from django.template.context import RequestContext
from django.utils.safestring import mark_safe

register = template.Library()


@register.simple_tag(takes_context=True)
def get_group_icons(
context: RequestContext,
) -> str:
"""Return icons representing group memberships."""
output = ""
if settings.BMA_CREATOR_GROUP_NAME in context["request"].user.groups.values_list("name", flat=True):
output += '<i class="fa-solid fa-user-ninja"></i> '
if settings.BMA_MODERATOR_GROUP_NAME in context["request"].user.groups.values_list("name", flat=True):
output += '<i class="fa-solid fa-user-shield"></i> '
if settings.BMA_CURATOR_GROUP_NAME in context["request"].user.groups.values_list("name", flat=True):
output += '<i class="fa-solid fa-user-astronaut"></i> '
return mark_safe(output) # noqa: S308

0 comments on commit e45d4d5

Please sign in to comment.