Commits

Anonymous committed ddcdd99

- fixed templates (finally it works the way it should)
- multilple AdminSite() support

  • Participants
  • Parent commits 71f5daf

Comments (0)

Files changed (8)

File admin_tools/dashboard/dashboards.py

 from django.utils.translation import ugettext_lazy as _
 from django.core.urlresolvers import reverse
 from django.contrib.contenttypes.models import ContentType
+from django.contrib import admin
+
 from admin_tools.dashboard import modules
 
 
         self.template = kwargs.get('template', 'admin_tools/dashboard/dashboard.html')
         self.columns = kwargs.get('columns', 2)
         self.children = kwargs.get('children', [])
+        self.admin_site = kwargs.get('admin_site', admin.site)
 
     def init_with_context(self, context):
         """
     """
     def __init__(self, **kwargs):
         Dashboard.__init__(self, **kwargs)
-
+        
         # append a link list module for "quick links"
         self.children.append(modules.LinkList(
             title=_('Quick links'),

File admin_tools/dashboard/modules.py

 from django.contrib.contenttypes.models import ContentType
 from django.utils.translation import ugettext_lazy as _
 
-from admin_tools.utils import AppListElementMixin
+from admin_tools.utils import AppListElementMixin, get_admin_site_name
 
 
 class DashboardModule(object):
             if app_label not in apps:
                 apps[app_label] = {
                     'title': capfirst(app_label.title()),
-                    'url': reverse('admin:app_list', args=(app_label,)),
+                    'url': reverse('%s:app_list' % get_admin_site_name(context), args=(app_label,)),
                     'models': []
                 }
             model_dict = {}
             model_dict['title'] = capfirst(model._meta.verbose_name_plural)
             if perms['change']:
-                model_dict['change_url'] = self._get_admin_change_url(model)
+                model_dict['change_url'] = self._get_admin_change_url(model, context)
             if perms['add']:
-                model_dict['add_url'] = self._get_admin_add_url(model)
+                model_dict['add_url'] = self._get_admin_add_url(model, context)
             apps[app_label]['models'].append(model_dict)
 
         apps_sorted = apps.keys()
             model_dict = {}
             model_dict['title'] = capfirst(model._meta.verbose_name_plural)
             if perms['change']:
-                model_dict['change_url'] = self._get_admin_change_url(model)
+                model_dict['change_url'] = self._get_admin_change_url(model, context)
             if perms['add']:
-                model_dict['add_url'] = self._get_admin_add_url(model)
+                model_dict['add_url'] = self._get_admin_add_url(model, context)
             self.children.append(model_dict)
 
 

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

 {% extends "admin_tools/dashboard/module.html" %}
 {% load i18n %}
 {% block module_content %}
-<div class="module {% for item in module.css_classes %}{{ item }} {% endfor %}">
-    <h3>{{ module.title }}</h2>
+{% if subindex %}
+    <div class="module {% for item in module.css_classes %}{{ item }} {% endfor %}">
+        <h3>{{ module.title }}</h3>
+{% else %}
+    <div class="group {% for item in module.css_classes %}{{ item }} {% endfor %}">
+        <h2>{{ module.title }}</h2>
+{% endif %}
+    
 {% spaceless %}
         {% if module.pre_content %}
             <p>{{ module.pre_content }}</p>
     {% for child in module.children %}
     
     <div class="module">
-        <h4><a href="{{ child.url }}">{{ child.title }}</a></h3>
+        {% if subindex %}
+            <h4><a href="{{ child.url }}">{{ child.title }}</a></h4>
+        {% else %}
+            <h3><a href="{{ child.url }}">{{ child.title }}</a></h3>
+        {% endif %}
+        
         {% for model in child.models %}
             <div class="row">
                 {% if model.change_url %}

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

 {% extends "admin_tools/dashboard/module.html" %}
 {% load admin_tools_dashboard_tags %}
 {% block module_content %}
-<div class="group">
+<div class="group {% for item in module.css_classes %}{{ item }} {% endfor %}">
     {% spaceless %}
     <h2>{{ module.title }}</h2>
     

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

 {% extends "admin_tools/dashboard/module.html" %}
 {% load i18n %}
 {% block module_content %}
-        <div class="module">
-            <h2>{{ module.title }}</h2>
+
+{% if subindex %}
+    <div class="module {% for item in module.css_classes %}{{ item }} {% endfor %}">
+        <h3>{{ module.title }}</h3>
+{% else %}
+    <div class="module {% for item in module.css_classes %}{{ item }} {% endfor %}">
+        <h2>{{ module.title }}</h2>
+{% endif %}
+            
             {% for child in module.children %}
                 <div class="row">
                     {% if child.change_url %}<a href="{{ child.change_url }}">{{ child.title }}</a>{% else %}{{ child.title }}{% endif %}

File admin_tools/dashboard/utils.py

 from django.core.exceptions import ImproperlyConfigured
 from django.utils.importlib import import_module
 from django.utils.text import capfirst
+from django.core.urlresolvers import reverse
+
 from admin_tools.dashboard import Registry
+from admin_tools.utils import get_admin_site
+import types
 
 
 def get_dashboard(context, location):
         'ADMIN_TOOLS_INDEX_DASHBOARD',
         'admin_tools.dashboard.DefaultIndexDashboard'
     )
-    mod, inst = dashboard_cls.rsplit('.', 1)
-    mod = import_module(mod)
-    return getattr(mod, inst)()
+    
+    if type(dashboard_cls) is types.DictType:
+        curr_url = context.get('request').META['PATH_INFO']
+        
+        for key in dashboard_cls:
+            admin_site_mod, admin_site_inst = key.rsplit('.', 1)
+            admin_url = reverse('%s:index' % admin_site_inst)
+            if curr_url.startswith(admin_url):
+                mod, inst = dashboard_cls[key].rsplit('.', 1)
+                mod = import_module(mod)
+                return getattr(mod, inst)()
+    else:
+        mod, inst = dashboard_cls.rsplit('.', 1)
+        mod = import_module(mod)
+        return getattr(mod, inst)()
+    
 
 
 def get_app_index_dashboard(context):
     model_list = []
     app_label = None
     app_title = app['name']
-    for model, model_admin in admin.site._registry.items():
+    admin_site = get_admin_site(context=context)
+    
+    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
         'ADMIN_TOOLS_APP_INDEX_DASHBOARD',
         'admin_tools.dashboard.DefaultAppIndexDashboard'
     )
-    mod, inst = dashboard_cls.rsplit('.', 1)
-    mod = import_module(mod)
-    return getattr(mod, inst)(app_title, model_list)
+    
+    if type(dashboard_cls) is types.DictType:
+        curr_url = context.get('request').META['PATH_INFO']
+        
+        for key in dashboard_cls:
+            admin_site_mod, admin_site_inst = key.rsplit('.', 1)
+            admin_url = reverse('%s:index' % admin_site_inst)
+            if curr_url.startswith(admin_url):
+                mod, inst = dashboard_cls[key].rsplit('.', 1)
+                mod = import_module(mod)
+                return getattr(mod, inst)(app_title, model_list)
+    else:
+        mod, inst = dashboard_cls.rsplit('.', 1)
+        mod = import_module(mod)
+        return getattr(mod, inst)(app_title, model_list)
+    

File admin_tools/menu/items.py

                 }
             apps[app_label]['models'].append({
                 'title': capfirst(model._meta.verbose_name_plural),
-                'url': self._get_admin_change_url(model)
+                'url': self._get_admin_change_url(model, context)
             })
 
         apps_sorted = apps.keys()

File admin_tools/utils.py

 """
 Admin ui common utilities.
 """
+
+import types
+from fnmatch import fnmatch
+
 from django.conf import settings
 from django.contrib import admin
 from django.core.urlresolvers import reverse
-from fnmatch import fnmatch
+from django.utils.importlib import import_module
+
+
+def get_admin_site(context=None, request=None):
+    dashboard_cls = getattr(
+        settings,
+        'ADMIN_TOOLS_INDEX_DASHBOARD',
+        'admin_tools.dashboard.DefaultIndexDashboard'
+    )
+    
+    if type(dashboard_cls) is types.DictType:
+        if context:
+            curr_url = context.get('request').META['PATH_INFO']
+        else:
+            curr_url = request.META['PATH_INFO']
+        
+        for key in dashboard_cls:
+            mod, inst = key.rsplit('.', 1)
+            admin_url = reverse('%s:index' % inst)
+            if curr_url.startswith(admin_url):
+                mod = import_module(mod)
+                return getattr(mod, inst)
+    else:
+        return admin.site
+
+
+def get_admin_site_name(context):
+    return get_admin_site(context).name
+
 
 def get_avail_models(request):
     """ Returns (model, perm,) for all models user can possibly see """
     items = []
-    for model, model_admin in admin.site._registry.items():
+    
+    admin_site = get_admin_site(request=request)
+    
+    for model, model_admin in admin_site._registry.items():
         perms = model_admin.get_model_perms(request)
         if True not in perms.values():
             continue
         items.append((model, perms,))
     return items
 
+
 def filter_models(request, models, exclude):
     """ Returns (model, perm,) for all models that match
         models/exclude patterns and are visible by current user.
         return filter_models(request, included, excluded)
 
 
-    def _get_admin_change_url(self, model):
+    def _get_admin_change_url(self, model, context):
         """
         Returns the admin change url.
         """
         app_label = model._meta.app_label
-        return reverse('admin:%s_%s_changelist' % (app_label,
-                                                   model.__name__.lower()))
+        return reverse('%s:%s_%s_changelist' % (get_admin_site_name(context),
+                                                app_label,
+                                                model.__name__.lower()))
 
-    def _get_admin_add_url(self, model):
+
+    def _get_admin_add_url(self, model, context):
         """
         Returns the admin add url.
         """
         app_label = model._meta.app_label
-        return reverse('admin:%s_%s_add' % (app_label, model.__name__.lower()))
+        return reverse('%s:%s_%s_add' % (get_admin_site_name(context),
+                                            app_label,
+                                            model.__name__.lower()))
 
 
 def get_media_url():