Commits

David Jean Louis committed a3301e5

- disable rendering of empty group modules (fixes #49),
- group empty children modules are not rendered now,
- added a _initialized property to modules to ensure that init_with_context is called only once.

  • Participants
  • Parent commits 7649921

Comments (0)

Files changed (3)

File admin_tools/dashboard/modules.py

                 setattr(self, key, kwargs[key])
         self.children = self.children or []
         self.css_classes = self.css_classes or []
+        # boolean flag to ensure that the module is initialized only once
+        self._initialized = False
 
     def init_with_context(self, context):
         """
     display = 'tabs'
 
     def init_with_context(self, context):
+        if self._initialized:
+            return
         for module in self.children:
             # to simplify the whole stuff, modules have some limitations,
             # they cannot be dragged, collapsed or closed
             module.draggable = False
             module.deletable = False
             module.show_title = (self.display == 'stacked')
+            module.init_with_context(context)
+        self._initialized = True
 
     def is_empty(self):
         """
         A group of modules is considered empty if it has no children or if 
         all its children are empty.
 
+        >>> from admin_tools.dashboard.modules import DashboardModule, LinkList
         >>> mod = Group()
         >>> mod.is_empty()
         True
         >>> mod.children.append(DashboardModule())
         >>> mod.is_empty()
         True
-        >>> mod.children.append(LinkList(title='links', children[
-            {'title': 'example1', 'url': 'http://example.com'},
-            {'title': 'example2', 'url': 'http://example.com'},
-        ]))
+        >>> mod.children.append(LinkList('links', children=[
+        ...    {'title': 'example1', 'url': 'http://example.com'},
+        ...    {'title': 'example2', 'url': 'http://example.com'},
+        ... ]))
         >>> mod.is_empty()
         False
         """
         if super(Group, self).is_empty():
             return True
         for child in self.children:
-            if child.is_empty():
-                return True
-        return False
+            if not child.is_empty():
+                return False
+        return True
 
 
 class LinkList(DashboardModule):
     layout = 'stacked'
 
     def init_with_context(self, context):
+        if self._initialized:
+            return
         new_children = []
         for link in self.children:
             if isinstance(link, (tuple, list,)):
             else:
                 new_children.append(link)
         self.children = new_children
+        self._initialized = True
 
 
 class AppList(DashboardModule, AppListElementMixin):
         super(AppList, self).__init__(title, **kwargs)
 
     def init_with_context(self, context):
+        if self._initialized:
+            return
         items = self._visible_models(context['request'])
         apps = {}
         for model, perms in items:
             # sort model list alphabetically
             apps[app]['models'].sort(lambda x, y: cmp(x['title'], y['title']))
             self.children.append(apps[app])
+        self._initialized = True
 
 
 class ModelList(DashboardModule, AppListElementMixin):
         super(ModelList, self).__init__(title, **kwargs)
 
     def init_with_context(self, context):
+        if self._initialized:
+            return
         items = self._visible_models(context['request'])
         if not items:
             return
             if perms['add']:
                 model_dict['add_url'] = self._get_admin_add_url(model)
             self.children.append(model_dict)
+        self._initialized = True
 
 
 class RecentActions(DashboardModule):
         super(RecentActions, self).__init__(title, **kwargs)
 
     def init_with_context(self, context):
+        if self._initialized:
+            return
         from django.db.models import Q
         from django.contrib.admin.models import LogEntry
 
         self.children = qs.select_related('content_type', 'user')[:self.limit]
         if not len(self.children):
             self.pre_content = _('No recent actions.')
+        self._initialized = True
 
 
 class Feed(DashboardModule):
         super(Feed, self).__init__(title, **kwargs)
 
     def init_with_context(self, context):
+        if self._initialized:
+            return
         import datetime
         if self.feed_url is None:
             raise ValueError('You must provide a valid feed URL')
                 # no date for certain feeds
                 pass
             self.children.append(entry)
+        self._initialized = True

File admin_tools/dashboard/templates/admin_tools/dashboard/modules/group.html

     {% ifequal module.display "tabs" %}
     <ul>
     {% for sub_module in module.children %}
-        <li class="group-tabs-link"><a href="#module_{{ index }}_{{ forloop.counter }}">{{ sub_module.title }}</a></li>
+        {% if not sub_module.is_empty %}<li class="group-tabs-link"><a href="#module_{{ index }}_{{ forloop.counter }}">{{ sub_module.title }}</a></li>{% endif %}
     {% endfor %}
     </ul>
     {% endifequal %}
     {% ifequal module.display "accordion" %}
     {% for sub_module in module.children %}
-        <span class="group-accordion-header"><a href="#">{{ sub_module.title }}</a></span>
+        {% if not sub_module.is_empty %}<span class="group-accordion-header"><a href="#">{{ sub_module.title }}</a></span>{% endif %}
         {% admin_tools_render_dashboard_module sub_module index forloop.counter %}
     {% endfor %}
     {% else %}

File admin_tools/dashboard/tests.py

 from unittest import TestCase
 from django.test import TestCase as DjangoTestCase
 from django.core import management
-from django.contrib.auth.models import User, Group
+from django.contrib.auth import models as auth_models
 
 from admin_tools.dashboard import AppIndexDashboard
-from admin_tools.dashboard.modules import DashboardModule
+from admin_tools.dashboard.modules import DashboardModule, Group
 
 class ManagementCommandTest(DjangoTestCase):
     def test_customdashboard(self):
         models = ['django.contrib.auth.models.User',
                   'django.contrib.auth.models.Group']
         board = AppIndexDashboard('Auth', models)
-        self.assertEqual(board.get_app_model_classes(), [User, Group])
+        self.assertEqual(board.get_app_model_classes(),
+                         [auth_models.User, auth_models.Group])
 
 __test__ = {
     'DashboardModule.is_empty': DashboardModule.is_empty,
-    'DashboardModule.render_css_classes': DashboardModule.render_css_classes
+    'DashboardModule.render_css_classes': DashboardModule.render_css_classes,
+    'Group.is_empty': Group.is_empty,
 }