|
@@ -1,6 +1,8 @@
|
|
import datetime
|
|
import datetime
|
|
import json
|
|
import json
|
|
from django.utils import translation
|
|
from django.utils import translation
|
|
|
|
+from jet import settings
|
|
|
|
+from jet.models import PinnedApplication
|
|
|
|
|
|
try:
|
|
try:
|
|
from django.apps.registry import apps
|
|
from django.apps.registry import apps
|
|
@@ -21,6 +23,9 @@ from django.utils.functional import Promise
|
|
from django.contrib.admin.options import IncorrectLookupParameters
|
|
from django.contrib.admin.options import IncorrectLookupParameters
|
|
from django.core import urlresolvers
|
|
from django.core import urlresolvers
|
|
from django.contrib import admin
|
|
from django.contrib import admin
|
|
|
|
+from django.utils.translation import ugettext_lazy as _
|
|
|
|
+from django.utils.text import slugify
|
|
|
|
+from collections import OrderedDict
|
|
|
|
|
|
|
|
|
|
class JsonResponse(HttpResponse):
|
|
class JsonResponse(HttpResponse):
|
|
@@ -67,6 +72,7 @@ def get_app_list(context, order=True):
|
|
'name': capfirst(model._meta.verbose_name_plural),
|
|
'name': capfirst(model._meta.verbose_name_plural),
|
|
'object_name': model._meta.object_name,
|
|
'object_name': model._meta.object_name,
|
|
'perms': perms,
|
|
'perms': perms,
|
|
|
|
+ 'model_name': model._meta.model_name
|
|
}
|
|
}
|
|
if perms.get('change', False):
|
|
if perms.get('change', False):
|
|
try:
|
|
try:
|
|
@@ -233,3 +239,182 @@ def get_possible_language_codes():
|
|
language_codes.append(split[0].lower())
|
|
language_codes.append(split[0].lower())
|
|
|
|
|
|
return language_codes
|
|
return language_codes
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def get_original_menu_items(context):
|
|
|
|
+ if context.get('user') and context['user'].is_authenticated():
|
|
|
|
+ pinned_apps = PinnedApplication.objects.filter(user=context['user'].pk).values_list('app_label', flat=True)
|
|
|
|
+ else:
|
|
|
|
+ pinned_apps = []
|
|
|
|
+
|
|
|
|
+ original_app_list = get_app_list(context)
|
|
|
|
+
|
|
|
|
+ return map(lambda app: {
|
|
|
|
+ 'app_label': app['app_label'],
|
|
|
|
+ 'url': app['app_url'],
|
|
|
|
+ 'url_blank': False,
|
|
|
|
+ 'label': app.get('name', capfirst(_(app['app_label']))),
|
|
|
|
+ 'has_perms': app.get('has_module_perms', False),
|
|
|
|
+ 'models': list(map(lambda model: {
|
|
|
|
+ 'url': model.get('admin_url'),
|
|
|
|
+ 'url_blank': False,
|
|
|
|
+ 'name': model['model_name'],
|
|
|
|
+ 'object_name': model['object_name'],
|
|
|
|
+ 'label': model.get('name', model['object_name']),
|
|
|
|
+ 'has_perms': any(model.get('perms', {}).values()),
|
|
|
|
+ }, app['models'])),
|
|
|
|
+ 'pinned': app['app_label'] in pinned_apps,
|
|
|
|
+ 'custom': False
|
|
|
|
+ }, original_app_list)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def get_menu_item_url(url, original_app_list):
|
|
|
|
+ if isinstance(url, dict):
|
|
|
|
+ url_type = url.get('type')
|
|
|
|
+
|
|
|
|
+ if url_type == 'app':
|
|
|
|
+ return original_app_list[url['app_label']]['url']
|
|
|
|
+ elif url_type == 'model':
|
|
|
|
+ models = dict(map(
|
|
|
|
+ lambda x: (x['name'], x['url']),
|
|
|
|
+ original_app_list[url['app_label']]['models']
|
|
|
|
+ ))
|
|
|
|
+ return models[url['model']]
|
|
|
|
+ elif url_type == 'reverse':
|
|
|
|
+ return reverse(url['name'], args=url.get('args'), kwargs=url.get('kwargs'))
|
|
|
|
+ elif isinstance(url, str):
|
|
|
|
+ return url
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def get_menu_items(context):
|
|
|
|
+ pinned_apps = PinnedApplication.objects.filter(user=context['user'].pk).values_list('app_label', flat=True)
|
|
|
|
+ original_app_list = OrderedDict(map(lambda app: (app['app_label'], app), get_original_menu_items(context)))
|
|
|
|
+ custom_app_list = settings.JET_SIDE_MENU_ITEMS
|
|
|
|
+ custom_app_list_deprecated = settings.JET_SIDE_MENU_CUSTOM_APPS
|
|
|
|
+
|
|
|
|
+ if custom_app_list not in (None, False):
|
|
|
|
+ if isinstance(custom_app_list, dict):
|
|
|
|
+ admin_site = get_admin_site(context)
|
|
|
|
+ custom_app_list = custom_app_list.get(admin_site.name, [])
|
|
|
|
+
|
|
|
|
+ app_list = []
|
|
|
|
+
|
|
|
|
+ def get_menu_item_app_model(app_label, data):
|
|
|
|
+ item = {'has_perms': True}
|
|
|
|
+
|
|
|
|
+ if 'name' in data and app_label in original_app_list:
|
|
|
|
+ models = dict(map(
|
|
|
|
+ lambda x: (x['name'], x),
|
|
|
|
+ original_app_list[app_label]['models']
|
|
|
|
+ ))
|
|
|
|
+
|
|
|
|
+ if data['name'] in models:
|
|
|
|
+ item = models[data['name']]
|
|
|
|
+
|
|
|
|
+ if 'label' in data:
|
|
|
|
+ item['label'] = data['label']
|
|
|
|
+
|
|
|
|
+ if 'url' in data:
|
|
|
|
+ item['url'] = get_menu_item_url(data['url'], original_app_list)
|
|
|
|
+
|
|
|
|
+ if 'url_blank' in data:
|
|
|
|
+ item['url_blank'] = data['url_blank']
|
|
|
|
+
|
|
|
|
+ if 'permissions' in data:
|
|
|
|
+ item['has_perms'] = item.get('has_perms', True) and context['user'].has_perms(data['permissions'])
|
|
|
|
+
|
|
|
|
+ return item
|
|
|
|
+
|
|
|
|
+ def get_menu_item_app(data):
|
|
|
|
+ app_label = data.get('app_label')
|
|
|
|
+
|
|
|
|
+ if not app_label:
|
|
|
|
+ if 'label' not in data:
|
|
|
|
+ raise Exception('Custom menu items should at least have \'label\' or \'app_label\' key')
|
|
|
|
+ app_label = 'custom_%s' % slugify(data['label'])
|
|
|
|
+
|
|
|
|
+ if app_label in original_app_list:
|
|
|
|
+ item = original_app_list[app_label]
|
|
|
|
+ else:
|
|
|
|
+ item = {'app_label': app_label, 'has_perms': True}
|
|
|
|
+
|
|
|
|
+ if 'label' in data:
|
|
|
|
+ item['label'] = data['label']
|
|
|
|
+
|
|
|
|
+ if 'models' in data:
|
|
|
|
+ item['models'] = list(map(lambda x: get_menu_item_app_model(app_label, x), data['models']))
|
|
|
|
+
|
|
|
|
+ if 'url' in data:
|
|
|
|
+ item['url'] = get_menu_item_url(data['url'], original_app_list)
|
|
|
|
+
|
|
|
|
+ if 'url_blank' in data:
|
|
|
|
+ item['url_blank'] = data['url_blank']
|
|
|
|
+
|
|
|
|
+ if 'permissions' in data:
|
|
|
|
+ item['has_perms'] = item.get('has_perms', True) and context['user'].has_perms(data['permissions'])
|
|
|
|
+
|
|
|
|
+ item['pinned'] = item['app_label'] in pinned_apps
|
|
|
|
+
|
|
|
|
+ return item
|
|
|
|
+
|
|
|
|
+ for data in custom_app_list:
|
|
|
|
+ item = get_menu_item_app(data)
|
|
|
|
+ app_list.append(item)
|
|
|
|
+ elif custom_app_list_deprecated not in (None, False):
|
|
|
|
+ app_dict = {}
|
|
|
|
+ models_dict = {}
|
|
|
|
+
|
|
|
|
+ for app in original_app_list.values():
|
|
|
|
+ app_label = app['app_label']
|
|
|
|
+ app_dict[app_label] = app
|
|
|
|
+
|
|
|
|
+ for model in app['models']:
|
|
|
|
+ if app_label not in models_dict:
|
|
|
|
+ models_dict[app_label] = {}
|
|
|
|
+
|
|
|
|
+ models_dict[app_label][model['object_name']] = model
|
|
|
|
+
|
|
|
|
+ app['models'] = []
|
|
|
|
+
|
|
|
|
+ app_list = []
|
|
|
|
+
|
|
|
|
+ if isinstance(custom_app_list_deprecated, dict):
|
|
|
|
+ admin_site = get_admin_site(context)
|
|
|
|
+ custom_app_list_deprecated = custom_app_list_deprecated.get(admin_site.name, [])
|
|
|
|
+
|
|
|
|
+ for item in custom_app_list_deprecated:
|
|
|
|
+ app_label, models = item
|
|
|
|
+
|
|
|
|
+ if app_label in app_dict:
|
|
|
|
+ app = app_dict[app_label]
|
|
|
|
+
|
|
|
|
+ for model_label in models:
|
|
|
|
+ if model_label == '__all__':
|
|
|
|
+ app['models'] = models_dict[app_label].values()
|
|
|
|
+ break
|
|
|
|
+ elif model_label in models_dict[app_label]:
|
|
|
|
+ model = models_dict[app_label][model_label]
|
|
|
|
+ app['models'].append(model)
|
|
|
|
+
|
|
|
|
+ app_list.append(app)
|
|
|
|
+ else:
|
|
|
|
+ app_list = original_app_list.values()
|
|
|
|
+
|
|
|
|
+ current_found = False
|
|
|
|
+
|
|
|
|
+ for app in app_list:
|
|
|
|
+ if not current_found:
|
|
|
|
+ for model in app['models']:
|
|
|
|
+ if not current_found and model.get('url') and context['request'].path.startswith(model['url']):
|
|
|
|
+ model['current'] = True
|
|
|
|
+ current_found = True
|
|
|
|
+ else:
|
|
|
|
+ model['current'] = False
|
|
|
|
+
|
|
|
|
+ if not current_found and app.get('url') and context['request'].path.startswith(app['url']):
|
|
|
|
+ app['current'] = True
|
|
|
|
+ current_found = True
|
|
|
|
+ else:
|
|
|
|
+ app['current'] = False
|
|
|
|
+
|
|
|
|
+ return app_list
|