Explorar el Código

Fix multiple admin sites support

Denis K hace 8 años
padre
commit
19b8400e26
Se han modificado 3 ficheros con 18 adiciones y 7 borrados
  1. 8 2
      jet/templatetags/jet_tags.py
  2. 1 1
      jet/tests/test_utils.py
  3. 9 4
      jet/utils.py

+ 8 - 2
jet/templatetags/jet_tags.py

@@ -9,7 +9,8 @@ from django.utils.formats import get_format
 from django.utils.safestring import mark_safe
 from jet import settings, VERSION
 from jet.models import Bookmark, PinnedApplication
-from jet.utils import get_app_list, get_model_instance_label, get_model_queryset, get_possible_language_codes
+from jet.utils import get_app_list, get_model_instance_label, get_model_queryset, get_possible_language_codes, \
+    get_admin_site
 
 try:
     from urllib.parse import parse_qsl
@@ -55,8 +56,13 @@ def jet_get_menu(context):
             app['models'] = []
 
         app_list = []
+        settings_app_list = settings.JET_SIDE_MENU_CUSTOM_APPS
 
-        for item in settings.JET_SIDE_MENU_CUSTOM_APPS:
+        if isinstance(settings_app_list, dict):
+            admin_site = get_admin_site(context)
+            settings_app_list = settings_app_list.get(admin_site.name, [])
+
+        for item in settings_app_list:
             app_label, models = item
 
             if app_label in app_dict:

+ 1 - 1
jet/tests/test_utils.py

@@ -52,7 +52,7 @@ class UtilsTestCase(TestCase):
                 self.assertIsNotNone(app, model.get('name'))
 
     def test_get_admin_site(self):
-        admin_site = get_admin_site('')
+        admin_site = get_admin_site({})
         self.assertIsInstance(admin_site, AdminSite)
 
     def test_lazy_date_time_encoder_dates(self):

+ 9 - 4
jet/utils.py

@@ -46,7 +46,7 @@ class JsonResponse(HttpResponse):
 
 
 def get_app_list(context, order=True):
-    admin_site = get_admin_site(context.get('current_app', ''))
+    admin_site = get_admin_site(context)
     request = context['request']
 
     app_dict = {}
@@ -111,10 +111,15 @@ def get_app_list(context, order=True):
     return app_list
 
 
-def get_admin_site(current_app):
+def get_admin_site(context):
     try:
-        resolver_match = resolve(reverse('%s:index' % current_app))
-        for func_closure in resolver_match.func.func_closure:
+        current_resolver = resolve(context.get('request').path)
+        index_resolver = resolve(reverse('%s:index' % current_resolver.namespaces[0]))
+
+        if hasattr(index_resolver.func, 'admin_site'):
+            return index_resolver.func.admin_site
+
+        for func_closure in index_resolver.func.__closure__:
             if isinstance(func_closure.cell_contents, AdminSite):
                 return func_closure.cell_contents
     except: