diff --git a/admin_toolbox/settings.py b/admin_toolbox/settings.py index 7fe1d20..0ac5964 100644 --- a/admin_toolbox/settings.py +++ b/admin_toolbox/settings.py @@ -5,3 +5,5 @@ sidebar = ADMIN_TOOLBOX.get('sidebar', { 'default': 'admin_toolbox.builders.AppsListBuilder', }) + +breadcrumbs = ADMIN_TOOLBOX.get('breadcrumbs', 'smart') diff --git a/admin_toolbox/templates/admin/base.html b/admin_toolbox/templates/admin/base.html index ada65b1..9f2b528 100644 --- a/admin_toolbox/templates/admin/base.html +++ b/admin_toolbox/templates/admin/base.html @@ -1,4 +1,4 @@ -{% load i18n admin_static admin_toolbox_sidebar %} +{% load i18n admin_static admin_toolbox_sidebar admin_toolbox_breadcrumbs %} {% get_current_language as LANGUAGE_CODE %}{% get_current_language_bidi as LANGUAGE_BIDI %} @@ -56,14 +56,16 @@ {% block nav-global %}{% endblock %} - {% if True|check_show_breadcrumbs %} - {% block breadcrumbs %} - - {% endblock %} - {% endif %} +{# {% if True|check_show_breadcrumbs %}#} + {% rebreadcrumbs %} + {% block breadcrumbs %} + + {% endblock %} + {% endrebreadcrumbs %} +{# {% endif %}#} {% if request.user.is_authenticated %}
{% admin_sidebar_content %} diff --git a/admin_toolbox/templates/admin_toolbox/breadcrumbs.html b/admin_toolbox/templates/admin_toolbox/breadcrumbs.html new file mode 100644 index 0000000..92ef3bb --- /dev/null +++ b/admin_toolbox/templates/admin_toolbox/breadcrumbs.html @@ -0,0 +1,10 @@ + diff --git a/admin_toolbox/templatetags/admin_toolbox_breadcrumbs.py b/admin_toolbox/templatetags/admin_toolbox_breadcrumbs.py new file mode 100644 index 0000000..0f38a47 --- /dev/null +++ b/admin_toolbox/templatetags/admin_toolbox_breadcrumbs.py @@ -0,0 +1,82 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from operator import itemgetter + +from django.core.exceptions import ImproperlyConfigured +from django.template.loader import render_to_string +from django.utils.translation import ugettext as _ +from six import text_type +from django import template +try: + from bs4 import BeautifulSoup +except ImportError: + BeautifulSoup = None +from admin_toolbox import settings + +from .admin_toolbox_sidebar import admin_sidebar_content + +register = template.Library() + + +@register.tag() +def rebreadcrumbs(parser, token): + nodelist = parser.parse(('endrebreadcrumbs',)) + parser.delete_first_token() + + return RerenderBreadcrumbs(nodelist) + + +class RerenderBreadcrumbs(template.Node): + def __init__(self, nodelist): + self.nodelist = nodelist + pass + + def parse_node(self, node): + soup = BeautifulSoup(node) + if soup.find_all('a'): + node = soup.a + return node['href'], text_type(node.text).strip() + else: + return None, node.strip() + + def render(self, context): + tx = self.nodelist.render(context) + print('----------', tx, '-------------') + + if not settings.breadcrumbs: + return '' + + if settings.breadcrumbs == 'smart': + if BeautifulSoup is None: + raise ImproperlyConfigured('beautifulsoup4 package is required for smart breadcrumbs to operate.') + soup = BeautifulSoup(tx) + tx = "".join(map(text_type, soup.div.contents if soup.div else [])) + + nodes = [self.parse_node(node) for node in tx.split('›') if node] + + if not nodes: + nodes = [ + (None, _('Home')) + ] + + active_path = admin_sidebar_content(context)['active_path'] + + index_node = nodes[0] + if active_path and active_path[-1]['url'] in set(map(itemgetter(0), nodes)): + nodes = nodes[list(map(itemgetter(0), nodes)).index(active_path[-1]['url']) + 1:] + nodes = [index_node] + [ + (node.get('url'), node['name']) for node in active_path + ] + nodes + elif active_path: + nodes = [index_node] + [ + (node.get('url'), node['name']) for node in active_path + ] + if len(nodes) > 1: + nodes[-1] = (None, nodes[-1][1]) + elif len(nodes) > 1: + nodes = [index_node, nodes[-1]] + + return render_to_string('admin_toolbox/breadcrumbs.html', context={'nodes': nodes}) + + return tx diff --git a/admin_toolbox/templatetags/admin_toolbox_sidebar.py b/admin_toolbox/templatetags/admin_toolbox_sidebar.py index e69db89..4997d33 100644 --- a/admin_toolbox/templatetags/admin_toolbox_sidebar.py +++ b/admin_toolbox/templatetags/admin_toolbox_sidebar.py @@ -96,14 +96,16 @@ def admin_sidebar_content(context, menu_name='default'): key=lambda x: (-len(x[1]), x[0]) ) current_items = items + active_path = [] if track_active: for index in track_active[0][0]: current_items[index]['active'] = True + active_path.append(current_items[index]) current_items = current_items[index]['items'] if 'items' in current_items[index] else [] return { 'items': items, - + 'active_path': active_path } diff --git a/setup.py b/setup.py index 1534175..de0e731 100644 --- a/setup.py +++ b/setup.py @@ -70,10 +70,9 @@ 'six', ], - # extras_require={ - # 'dev': ['check-manifest'], - # 'test': ['coverage'], - # }, + extras_require={ + 'smart-breadcrumbs': ['beautifulsoup4'], + }, setup_requires=[ 'setuptools_scm',