Skip to content

Commit

Permalink
Merge pull request #1155 from TOMToolkit/1154-update-navbar-integrati…
Browse files Browse the repository at this point in the history
…on-point-to-include-partials-on-the-right-side-of-the-bar

have nav_items use dictionary instead of list
  • Loading branch information
jchate6 authored Jan 28, 2025
2 parents 6b29298 + 0663293 commit cb5dd23
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 16 deletions.
2 changes: 1 addition & 1 deletion tom_alerts/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ def nav_items(self):
Integration point for adding items to the navbar.
This method should return a list of partial templates to be included in the navbar.
"""
return ['tom_alerts/partials/navbar_link.html']
return [{'partial': 'tom_alerts/partials/navbar_link.html'}]
5 changes: 5 additions & 0 deletions tom_common/templates/auth/partials/app_user_lists.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% load user_extras %}
{% for user_list in user_lists_to_display %}
{% show_individual_app_partial user_list %}
<hr/>
{% endfor %}
1 change: 1 addition & 0 deletions tom_common/templates/auth/user_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{% load user_extras %}
{% block title %}User Management{% endblock %}
{% block content %}
{% include_app_user_lists %}
{% group_list %}
<hr/>
{% user_list %}
Expand Down
2 changes: 1 addition & 1 deletion tom_common/templates/tom_common/partials/app_profiles.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<div class="row">
<div class="col-6">
{% for profile in profiles_to_display %}
{% show_individual_app_profile profile %}
{% show_individual_app_partial profile %}

{% comment %}
Start new column halfway through the list of profiles.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% comment %}
This partial template includes another partial from the context specific to a specific App's profile.
This allows the partial to be rendered with only the context specified by the app, without interference from
other app profile contexts.
This partial template includes another partial from the context specific to a specific App's partial.
This allows the partial to be rendered with only the context specified by the app, without interference from contexts
from other apps.
{% endcomment %}

{% include profile_partial %}
{% include app_partial %}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{% for nav_item in nav_item_list %}
{% include nav_item %}
{% include nav_item.partial %}
{% endfor %}
5 changes: 4 additions & 1 deletion tom_common/templates/tom_common/partials/navbar_login.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{% for nav_item in nav_item_list %}
{% include nav_item.partial %}
{% endfor %}
{% if user.is_authenticated %}
<li class="nav-item">
{% if user.first_name or user.last_name %}
Expand All @@ -13,4 +16,4 @@
<li class="nav-item">
<a class="btn btn-outline-success" title="logout" href="{% url 'login' %}">Login</a>
</li>
{% endif %}
{% endif %}
16 changes: 14 additions & 2 deletions tom_common/templatetags/tom_common_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,18 @@ def navbar_login(context):
Renders the username as a link to the user page, as well as the login button. Can be overridden to render additional
account-related buttons.
"""
return {'user': context['user']}
nav_item_list = []
for app in apps.get_app_configs():
try:
nav_items = app.nav_items()
if nav_items:
for item in nav_items:
if item.get('position', 'left') == 'right':
nav_item_list.append(item)
except AttributeError:
pass
return {'user': context['user'],
'nav_item_list': nav_item_list}


@register.inclusion_tag('tom_common/partials/navbar_app_addons.html', takes_context=True)
Expand All @@ -37,7 +48,8 @@ def navbar_app_addons(context):
nav_items = app.nav_items()
if nav_items:
for item in nav_items:
nav_item_list.append(item)
if item.get('position', 'left') != 'right':
nav_item_list.append(item)
except AttributeError:
pass
return {'nav_item_list': nav_item_list}
Expand Down
47 changes: 41 additions & 6 deletions tom_common/templatetags/user_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,41 @@ def user_list(context):
}


@register.inclusion_tag('auth/partials/app_user_lists.html', takes_context=True)
def include_app_user_lists(context):
"""
Imports the user list content from relevant apps into the template.
Each user_list should be contained in a list of dictionaries in an app's apps.py `user_lists` method.
Each user_list dictionary should contain a 'context' key with the path to the context processor class (typically a
templatetag), and a 'partial' key with the path to the html partial template.
FOR EXAMPLE:
[{'partial': 'path/to/partial.html',
'context': 'path/to/context/data/method'}]
"""
user_lists_to_display = []
for app in apps.get_app_configs():
try:
user_lists = app.user_lists()
except AttributeError:
continue
if user_lists:
for app_users in user_lists:
try:
context_method = import_string(app_users['context'])
except ImportError:
logger.warning(f'WARNING: Could not import context for {app.name} user list from '
f'{app_users["context"]}.\n'
f'Are you sure you have the right path?')
continue
new_context = context_method(context)
user_lists_to_display.append({'partial': app_users['partial'], 'context': new_context})

context['user_lists_to_display'] = user_lists_to_display
return context


@register.inclusion_tag('tom_common/partials/user_data.html')
def user_data(user):
"""
Expand Down Expand Up @@ -84,12 +119,12 @@ def show_app_profiles(context, user):
return context


@register.inclusion_tag('tom_common/partials/include_profile_card.html', takes_context=True)
def show_individual_app_profile(context, profile_data):
@register.inclusion_tag('tom_common/partials/include_app_partial.html', takes_context=True)
def show_individual_app_partial(context, app_partial_data):
"""
An Inclusion tag for setting the unique context for each app's user profile.
An Inclusion tag for setting the unique context for an app's partial.
"""
for item in profile_data['context']:
context[item] = profile_data['context'][item]
context['profile_partial'] = profile_data['partial']
for item in app_partial_data['context']:
context[item] = app_partial_data['context'][item]
context['app_partial'] = app_partial_data['partial']
return context

0 comments on commit cb5dd23

Please sign in to comment.