def jet_sibling_object(context, next): original = context.get("original") if not original: return model = type(original) preserved_filters_plain = context.get("preserved_filters", "") preserved_filters = dict(parse_qsl(preserved_filters_plain)) admin_site = get_admin_site(context) if admin_site is None: return request = context.get("request") queryset = get_model_queryset(admin_site, model, request, preserved_filters=preserved_filters) if queryset is None: return sibling_object = None object_pks = list(queryset.values_list("pk", flat=True)) try: index = object_pks.index(original.pk) sibling_index = index + 1 if next else index - 1 exists = sibling_index < len( object_pks) if next else sibling_index >= 0 sibling_object = queryset.get( pk=object_pks[sibling_index]) if exists else None except ValueError: pass if sibling_object is None: return url = reverse( "%s:%s_%s_change" % (admin_site.name, model._meta.app_label, model._meta.model_name), args=(sibling_object.pk, ), ) if preserved_filters_plain != "": url += "?" + preserved_filters_plain return {"label": str(sibling_object), "url": url}
def jet_sibling_object_url(context, next): original = context.get('original') if not original: return model = type(original) preserved_filters_plain = context.get('preserved_filters', '') preserved_filters = dict(parse_qsl(preserved_filters_plain)) admin_site = get_admin_site(context) if admin_site is None: return request = context.get('request') queryset = get_model_queryset(admin_site, model, request, preserved_filters=preserved_filters) if queryset is None: return sibling_object = None object_pks = list(queryset.values_list('pk', flat=True)) try: index = object_pks.index(original.pk) sibling_index = index + 1 if next else index - 1 exists = sibling_index < len( object_pks) if next else sibling_index >= 0 sibling_object = queryset.get( pk=object_pks[sibling_index]) if exists else None except ValueError: pass if sibling_object is None: return url = reverse( '%s:%s_%s_change' % (admin_site.name, model._meta.app_label, model._meta.model_name), args=(sibling_object.pk, )) if preserved_filters_plain != '': url += '?' + preserved_filters_plain return url
def jet_sibling_object(context, next): original = context.get('original') if not original: return model = type(original) preserved_filters_plain = context.get('preserved_filters', '') preserved_filters = dict(parse_qsl(preserved_filters_plain)) admin_site = get_admin_site(context) if admin_site is None: return request = context.get('request') queryset = get_model_queryset(admin_site, model, request, preserved_filters=preserved_filters) if queryset is None: return sibling_object = None object_pks = list(queryset.values_list('pk', flat=True)) try: index = object_pks.index(original.pk) sibling_index = index + 1 if next else index - 1 exists = sibling_index < len(object_pks) if next else sibling_index >= 0 sibling_object = queryset.get(pk=object_pks[sibling_index]) if exists else None except ValueError: pass if sibling_object is None: return url = reverse('%s:%s_%s_change' % ( admin_site.name, model._meta.app_label, model._meta.model_name ), args=(sibling_object.pk,)) if preserved_filters_plain != '': url += '?' + preserved_filters_plain return { 'label': str(sibling_object), 'url': url }
def test_get_admin_site(self): admin_site = get_admin_site({}) self.assertIsInstance(admin_site, AdminSite)
def jet_get_menu(context): if settings.JET_SIDE_MENU_CUSTOM_APPS not in (None, False): app_list = get_app_list(context, False) app_dict = {} models_dict = {} for app in app_list: app_label = app.get('app_label', app.get('name')) 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 = [] settings_app_list = 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: 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 = get_app_list(context) current_found = False pinned = PinnedApplication.objects.filter( user=context.get('user').pk).values_list('app_label', flat=True) all_aps = [] apps = [] pinned_apps = [] for app in app_list: if not current_found: for model in app['models']: if context['request'].path.startswith(model['admin_url']): model['current'] = True current_found = True break if not current_found and context['request'].path.startswith( app['app_url']): app['current'] = True current_found = True if app.get('app_label', app.get('name')) in pinned: pinned_apps.append(app) else: apps.append(app) all_aps.append(app) return {'apps': apps, 'pinned_apps': pinned_apps, 'all_apps': all_aps}
def jet_get_menu(context): if settings.JET_SIDE_MENU_CUSTOM_APPS not in (None, False): app_list = get_app_list(context, False) app_dict = {} models_dict = {} for app in app_list: app_label = app.get('app_label', app.get('name')) 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 = [] settings_app_list = 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: 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 = get_app_list(context) current_found = False pinned = PinnedApplication.objects.filter(user=context.get('user').pk).values_list('app_label', flat=True) all_aps = [] apps = [] pinned_apps = [] for app in app_list: if not current_found: for model in app['models']: if 'admin_url' in model and context['request'].path.startswith(model['admin_url']): model['current'] = True current_found = True break if not current_found and context['request'].path.startswith(app['app_url']): app['current'] = True current_found = True if app.get('app_label', app.get('name')) in pinned: pinned_apps.append(app) else: apps.append(app) all_aps.append(app) return {'apps': apps, 'pinned_apps': pinned_apps, 'all_apps': all_aps}
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: parts = data['name'].split('.', 2) if len(parts) > 1: app_label, name = parts else: name = data['name'] if app_label in original_app_list: models = dict(map( lambda x: (x['name'], x), original_app_list[app_label]['models'] )) if name in models: item = models[name].copy() if 'label' in data: item['label'] = data['label'] if 'materialicon' in data: item['materialicon'] = data['materialicon'] 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'], allow_unicode=True) if app_label in original_app_list: item = original_app_list[app_label].copy() else: item = {'app_label': app_label, 'has_perms': True} if 'label' in data: item['label'] = data['label'] if 'items' in data: item['items'] = list(map(lambda x: get_menu_item_app_model(app_label, x), data['items'])) 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['items'] = [] 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['items'] = models_dict[app_label].values() break elif model_label in models_dict[app_label]: model = models_dict[app_label][model_label] app['items'].append(model) app_list.append(app) else: def map_item(item): item['items'] = item['models'] return item app_list = list(map(map_item, original_app_list.values())) current_found = False for app in app_list: if not current_found: for model in app['items']: 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
def my_get_app_list(context, order=True): admin_site = get_admin_site(context) request = context['request'] app_dict = {} for model, model_admin in admin_site._registry.items(): app_label = model._meta.app_label try: has_module_perms = model_admin.has_module_permission(request) except AttributeError: has_module_perms = request.user.has_module_perms( app_label) # Fix Django < 1.8 issue if has_module_perms: perms = model_admin.get_model_perms(request) # Check whether user has any perm for this module. # If so, add the module to the model_list. if True in perms.values(): info = (app_label, model._meta.model_name) model_dict = { 'name': capfirst(model._meta.verbose_name_plural), 'object_name': model._meta.object_name, 'perms': perms, 'model_name': model._meta.model_name } if perms.get('change', False): try: model_dict['admin_url'] = reverse( 'admin:%s_%s_changelist' % info, current_app=admin_site.name) except NoReverseMatch: pass if perms.get('add', False): try: model_dict['add_url'] = reverse( 'admin:%s_%s_add' % info, current_app=admin_site.name) except NoReverseMatch: pass if perms.get('view', False): try: model_dict['view_url'] = reverse( 'admin:%s_%s_changelist' % info, current_app=admin_site.name) except NoReverseMatch: pass if app_label in app_dict: app_dict[app_label]['models'].append(model_dict) else: try: name = apps.get_app_config(app_label).verbose_name except NameError: name = app_label.title() app_dict[app_label] = { 'name': name, 'app_label': app_label, 'app_url': reverse( 'admin:app_list', kwargs={'app_label': app_label}, current_app=admin_site.name, ), 'has_module_perms': has_module_perms, 'models': [model_dict], } # Sort the apps alphabetically. app_list = list(app_dict.values()) if order: app_list.sort(key=lambda x: x['name'].lower()) # Sort the models alphabetically within each app. for app in app_list: app['models'].sort(key=lambda x: x['name']) return app_list