Commits

David Jean Louis committed 694c441

changed the way dashboards are selected, don't rely on request variables but pass an extra argument to the template tag instead

  • Participants
  • Parent commits c2f19bd

Comments (0)

Files changed (4)

File admin_tools/dashboard/templates/admin/app_index.html

 
 {% if not is_popup %}
 
+{% block dashboard_css %}{% render_dashboard_css 'app_index' %}{% endblock %}
 {% block breadcrumbs %}
 <div class="breadcrumbs"><a href="../">
 {% trans "Home" %}</a> &rsaquo; 
 {% endif %} 
 
 {% block content %}
-{% render_dashboard %}
+{% render_dashboard 'app_index' %}
 {% endblock %}
 {% block sidebar %}{% endblock %}

File admin_tools/dashboard/templates/admin/index.html

 
 {% block extrastyle %}
 {{ block.super }}
-{% render_dashboard_css %}
+{% block dashboard_css %}{% render_dashboard_css %}{% endblock %}
 {% endblock %}
 
 {% block bodyclass %}dashboard{% endblock %}

File admin_tools/dashboard/templatetags/dashboard_tags.py

 import math
 from django import template
 from django.conf import settings
-from admin_tools.dashboard.utils import get_dashboard_from_context
+from admin_tools.dashboard.utils import get_dashboard
 
 register = template.Library()
 tag_func = register.inclusion_tag('dashboard/dummy.html', takes_context=True)
 
 
-def render_dashboard(context, dashboard=None):
+def render_dashboard(context, location='index', dashboard=None):
     """
-    Template tag that renders the dashboard, it takes an optional ``Dashboard``
-    instance as unique argument, if not given, the dashboard is retrieved with
-    the ``get_dashboard_from_context`` function.
+    Template tag that renders the dashboard, it takes two optional arguments:
+    
+    ``location``
+        The location of the dashboard, it can be 'index' (for the admin index
+        dashboard) or 'app_index' (for the app index dashboard), the default
+        value is 'index'.
+
+    ``dashboard``
+        An instance of ``Dashboard``, if not given, the dashboard is retrieved
+        with the ``get_index_dashboard`` or ``get_app_index_dashboard``
+        functions, depending on the ``location`` argument.
     """
-    if not dashboard:
-        dashboard = get_dashboard_from_context(context)
+    if dashboard is None:
+        dashboard = get_dashboard(context, location)
+
     dashboard.init_with_context(context)
     context.update({
         'template': dashboard.template,
 render_dashboard_module = tag_func(render_dashboard_module)
 
 
-def render_dashboard_css(context, dashboard=None):
+def render_dashboard_css(context, location='index', dashboard=None):
     """
-    Template tag that renders the dashboard css files.
+    Template tag that renders the dashboard css files, it takes two optional
+    arguments:
+    
+    ``location``
+        The location of the dashboard, it can be 'index' (for the admin index
+        dashboard) or 'app_index' (for the app index dashboard), the default
+        value is 'index'.
+
+    ``dashboard``
+        An instance of ``Dashboard``, if not given, the dashboard is retrieved
+        with the ``get_index_dashboard`` or ``get_app_index_dashboard``
+        functions, depending on the ``location`` argument.
     """
     if dashboard is None:
-        dashboard = get_dashboard_from_context(context)
+        dashboard = get_dashboard(context, location)
 
     context.update({
         'template' : 'dashboard/css.html',

File admin_tools/dashboard/utils.py

 from admin_tools.dashboard.models import *
 
 
+def get_dashboard(context, location):
+    """
+    Returns the dashboard that match the given ``location``
+    (index or app_index).
+    """
+    if location == 'index':
+        return get_index_dashboard(context)
+    elif location == 'app_index':
+        return get_app_index_dashboard(context)
+    raise ValueError('Invalid dashboard location: "%s"' % location)
 
-def get_dashboard_from_context(context):
-    """
-    Return the dashboard instance given the context.
-    """
-    request = context['request']
-    if request.META.get('REQUEST_URI') == reverse('admin:index'):
-        return get_index_dashboard()
-    # this is a mess, needs cleanup !
-    app = context['app_list'][0]
-    models = []
-    app_label = None
-    app_title = app['name']
-    for model, model_admin in admin.site._registry.items():
-        if app['name'] == model._meta.app_label.title():
-            split = model.__module__.find(model._meta.app_label)
-            app_label = model.__module__[0:split] + model._meta.app_label
-            app_title = model._meta.app_label.title
-            for m in app['models']:
-                if m['name'] == capfirst(model._meta.verbose_name_plural):
-                    mod = '%s.%s' % (model.__module__, model.__name__)
-                    models.append(mod)
-    return get_app_index_dashboard(app_label, app_title, models)
-
-
-def get_index_dashboard():
+def get_index_dashboard(context):
     """
     Returns the admin dashboard defined by the user or the default one.
     """
     return getattr(mod, inst)()
 
 
-def get_app_index_dashboard(app_label=None, app_title='', model_list=[]):
+def get_app_index_dashboard(context):
     """
     Returns the admin dashboard defined by the user or the default one.
     """
+    # this is a mess, needs cleanup !
+    app = context['app_list'][0]
+    model_list = []
+    app_label = None
+    app_title = app['name']
+    for model, model_admin in admin.site._registry.items():
+        if app['name'] == model._meta.app_label.title():
+            split = model.__module__.find(model._meta.app_label)
+            app_label = model.__module__[0:split] + model._meta.app_label
+            app_title = model._meta.app_label.title()
+            for m in app['models']:
+                if m['name'] == capfirst(model._meta.verbose_name_plural):
+                    mod = '%s.%s' % (model.__module__, model.__name__)
+                    model_list.append(mod)
 
     # if an app has registered its own dashboard, use it
     if app_label is not None and app_label in Registry.registry: