1. grh
  2. django-admin-tools

Commits

kl...@icodr.local  committed 980daf0

* add multi AdminSite() support
* add DashboardModule.grappelli_column property

  • Participants
  • Parent commits fd92246
  • Branches default

Comments (0)

Files changed (4)

File admin_tools/dashboard/modules.py

View file
  • Ignore whitespace
     pre_content = None
     post_content = None
     children = None
+    # custom property for grappelli
+    grappelli_column = 1
 
     def __init__(self, title=None, **kwargs):
         if title is not None:
             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)
         self._initialized = True
 

File admin_tools/dashboard/utils.py

View file
  • Ignore whitespace
 from django.contrib import admin
 from django.utils.importlib import import_module
 from django.utils.text import capfirst
+from django.core.urlresolvers import reverse
 from admin_tools.dashboard.registry import Registry
+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.dashboards.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_site_mod = import_module(admin_site_mod)
+            admin_site = getattr(admin_site_mod, admin_site_inst)
+            admin_url = reverse('%s:index' % admin_site.name)
+            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.dashboards.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_site_mod = import_module(admin_site_mod)
+            admin_site = getattr(admin_site_mod, admin_site_inst)
+            admin_url = reverse('%s:index' % admin_site.name)
+            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

View file
  • Ignore whitespace
                 }
             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()
             if not perms['change']:
                 continue
             title = capfirst(model._meta.verbose_name_plural)
-            url = self._get_admin_change_url(model)
+            url = self._get_admin_change_url(model, context)
             item = MenuItem(title=title, url=url)
             self.children.append(item)
 

File admin_tools/utils.py

View file
  • Ignore whitespace
 from django.contrib import admin
 from django.core.urlresolvers import reverse
 from fnmatch import fnmatch
+from django.utils.importlib import import_module
+import types
+
+def get_admin_site(context=None, request=None):
+    dashboard_cls = getattr(
+        settings,
+        'ADMIN_TOOLS_INDEX_DASHBOARD',
+        'admin_tools.dashboard.dashboards.DefaultIndexDashboard'
+    )
+    
+    if type(dashboard_cls) is types.DictType:
+        if context:
+            request = context.get('request')
+        
+        curr_url = request.META['PATH_INFO']
+        
+        for key in dashboard_cls:
+            mod, inst = key.rsplit('.', 1)
+            mod = import_module(mod)
+            admin_site = getattr(mod, inst)
+            admin_url = reverse('%s:index' % admin_site.name)
+            if curr_url.startswith(admin_url):
+                return admin_site
+    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
         if self.exclude_list and not included:
             included = ["*"]
         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()))
-
-    def _get_admin_add_url(self, model):
+        return reverse('%s:%s_%s_changelist' % (get_admin_site_name(context),
+                                                app_label,
+                                                model.__name__.lower()))
+    
+    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():