Skip to content
This repository has been archived by the owner on Mar 4, 2024. It is now read-only.

Allow sidebar customization #128

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bootstrap_admin/templates/admin/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ <h1>
{% endblock %}
<div id="user-tools" class="btn-group">
<div class="btn-group">
{% block user_tools_items %}
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
{% if user.is_active and user.is_staff %}
{% block welcome-msg %}
Expand All @@ -127,6 +128,7 @@ <h1>
<a href="{% url 'admin:logout' %}">{% trans 'Log out' %}</a>
</li>
</ul>
{% endblock user_tools_items %}
</div>
{% if docsroot %}
<a href="{{ docsroot }}" title="{% trans 'Documentation' %}" class="btn btn-default hidden-sm"><span class="glyphicon glyphicon-book"></span></a>
Expand Down
12 changes: 8 additions & 4 deletions bootstrap_admin/templates/bootstrap_admin/sidebar_menu.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
{% load i18n %}
{% load i18n bootstrap_admin_template_tags %}
{% if app_list %}

{% for app in app_list %}
<ul class="nav nav-sidebar app-{{ app.app_label }} module{% if app.app_url in current_url %} has-active-menu show-models{% endif %}">
<ul class="nav nav-sidebar app-{{ app.app_label }} module{% if app.app_url in current_url or app|has_active_menu:current_url %} has-active-menu show-models{% endif %}">
<li {% if app.app_url == current_url %} class="active"{% endif %}>
<a href="{{ app.app_url }}" class="section" title="{% blocktrans with name=app.name %}Models in the {{ name }} application{% endblocktrans %}">
{{ app.name }}
</a>
</li>
{% for model in app.models %}
<li {% if model.admin_url in current_url %}class="active"{% endif %}>
<a href="{% firstof model.admin_url model.add_url '#' %}" title="{% if not model.admin_url and not model.add_url %}{% trans "You don't have permission to edit anything." %}{% endif %}">{{ model.name }}</a>
<li class="model model-{{ model.object_name|lower }}{% if model.admin_url in current_url %} active{% endif %}">
<a href="{% firstof model.admin_url model.add_url '#' %}"
title="{% if not model.admin_url and not model.add_url %}{% trans 'You don\'t have permission to edit anything.' %}{% endif %}">
{{ model.name }}
</a>
</li>
{% endfor %}
</ul>
Expand Down
87 changes: 87 additions & 0 deletions bootstrap_admin/templatetags/bootstrap_admin_template_tags.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import django
from django.contrib.admin import site
from django.apps import apps
from django.utils.text import capfirst
from django.core.exceptions import ImproperlyConfigured
from django.utils import six
from django.utils.module_loading import import_string
from django.conf import settings
from django import template
from django import VERSION as DJANGO_VERSION
Expand Down Expand Up @@ -87,13 +89,97 @@ def display_sidebar_menu(has_filters=False):
return sidebar_menu_setting()


@register.filter()
def has_active_menu(app, current_url):
return current_url in [item['admin_url'] for item in app['models']]


def sidebar_menu_list(sidebar_menu_callback, request):
"""
Adapted from https://github.com/maykinmedia/django-admin-index
"""

show_remaining_apps = getattr(settings, 'BOOTSTRAP_ADMIN_SHOW_REMAINING_APPS', False) or \
(getattr(settings, 'BOOTSTRAP_ADMIN_SHOW_REMAINING_APPS_TO_SUPERUSERS', True) and request.user.is_superuser)

sidebar_menu_callback_func = import_string(sidebar_menu_callback)
sidebar_menu_items = sidebar_menu_callback_func(request)

# Convert to convienent dict
model_dicts = {}

if django.VERSION < (1, 9):
from .compat.django18 import get_app_list
original_app_list = get_app_list(site, request)
else:
original_app_list = site.get_app_list(request)

for app in original_app_list:
for model in app['models']:
key = '{}.{}'.format(app['app_label'], model['object_name'].lower()) # noqa
model_dict = model.copy()
model_dict.update({
'app_label': app['app_label'],
'app_name': app['name'],
'app_url': app['app_url'],
'has_module_perms': app['has_module_perms'],
})
model_dicts[key] = model_dict

# Create new list based on our groups, using the model_dicts constructed above.
added = []
result = []
app_list = sidebar_menu_items

for app in app_list:
models = []
for item in app['items']:
if 'model' in item:
key = item['model']
o = model_dicts.get(key)
if o:
models.append(o)
added.append(key)
elif 'link' in item:
models.append({
'name': item['name'],
'app_label': app['app_label'],
'admin_url': item['link'],
'object_name': item['object_name'],
})

result.append({
'name': app['name'],
'app_label': app['app_label'],
'models': models,
})

if show_remaining_apps:
other = [model_dicts[k] for k in model_dicts if k not in added]
if other:
result.append({
'name': ('Miscellaneous'),
'app_label': 'misc',
'models': sorted(other, key=lambda m: m['name'])
})

return result


@register.inclusion_tag('bootstrap_admin/sidebar_menu.html',
takes_context=True)
def render_menu_app_list(context):
show_global_menu = sidebar_menu_setting()
if not show_global_menu:
return {'app_list': ''}

sidebar_menu_callback = getattr(settings, 'BOOTSTRAP_ADMIN_SIDEBAR_MENU_CALLBACK', None)
if sidebar_menu_callback:
return {
'app_list': sidebar_menu_list(sidebar_menu_callback, context.get('request')),
'current_url': context.get('request').path,
}

if DJANGO_VERSION < (1, 8):
dependencie = 'django.core.context_processors.request'
processors = settings.TEMPLATE_CONTEXT_PROCESSORS
Expand Down Expand Up @@ -180,6 +266,7 @@ def render_menu_app_list(context):
# Sort the models alphabetically within each sapp.
for app in app_list:
app['models'].sort(key=lambda x: x['name'])

return {'app_list': app_list, 'current_url': context.get('request').path}


Expand Down