David Jean Louis avatar David Jean Louis committed 4e73e10

Fixed issue #33: empty applist menu items should not be displayed

Comments (0)

Files changed (3)

admin_tools/menu/items.py

         return self.url == current_url or \
             len([c for c in self.children if c.is_selected(request)]) > 0
 
+    def is_empty(self):
+        """
+        Helper method that returns ``True`` if the menu item is empty.
+        This method always returns ``False`` for basic items, but can return 
+        ``True`` if the item is an AppList.
+        """
+        return False
+
 
 class AppList(MenuItem, AppListElementMixin):
     """
                 item.children.append(MenuItem(**model_dict))
             self.children.append(item)
 
+    def is_empty(self):
+        """
+        Helper method that returns ``True`` if the applist menu item has no 
+        children.
+
+        >>> from admin_tools.menu.items import MenuItem, AppList
+        >>> item = AppList(title='My menu item')
+        >>> item.is_empty()
+        True
+        >>> item.children.append(MenuItem(title='foo'))
+        >>> item.is_empty()
+        False
+        >>> item.children = []
+        >>> item.is_empty()
+        True
+        """
+        return len(self.children) == 0
+
 
 class Bookmarks(MenuItem, AppListElementMixin):
     """

admin_tools/menu/templates/admin_tools/menu/item.html

 {% load admin_tools_menu_tags %}
 {% spaceless %}
+{% if not item.is_empty %}
 <li class="menu-item{% ifequal index 1 %} first{% endifequal %}{% if not item.enabled %} disabled{% endif %}{% if selected %} selected{% endif %}{% if item.css_classes %} {{ item.css_classes|join:' ' }}{% endif %}">
 <a href="{% if item.url and item.enabled %}{{ item.url }}{% else %}#{% endif %}"{% if item.description %} title="{{ item.description }}"{% endif %}{% if item.accesskey %} accesskey="{{ item.accesskey }}"{% endif %}>{% if item.children %}<span class="icon"></span>{% endif %}{{ item.title }}</a>
     {% if item.children %}
     </ul>
     {% endif %}
 </li>
+{% endif %}
 {% endspaceless %}

admin_tools/menu/tests.py

 from django.test import TestCase
 from django.core import management
 
+from admin_tools.menu.items import AppList
 
 class ManagementCommandTest(TestCase):
 
             assert False
         except:
             pass
+
+
+__test__ = {
+    'AppList.is_empty': AppList.is_empty,
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.