def render_topbar_nav(context):
    admin.autodiscover()
    admin_type_list = build_app_list(admin.site._registry).keys()
    admin_type_list.sort()

    all_admin_types = []
    for type in admin_type_list:
        all_admin_types.append(
            {
                "admin_type": type,
                "admin_type_priority": get_admin_type_priority(type),
                "admin_type_verbose": get_verbose_admin_type(type),
            }
        )

    try:
        admin_type = context["admin_type"]
    except:
        admin_type = None

    try:
        is_front_page = context["admin_front_page"]
    except:
        is_front_page = False

    more_menu_active = False
    app_counter = 0

    all_admin_types = sorted(all_admin_types, key=itemgetter("admin_type_priority", "admin_type_verbose"))

    if len(all_admin_types) > 4:
        if admin_type:
            for type in all_admin_types:
                app_counter += 1
                if type["admin_type"] == admin_type:
                    if app_counter > 4:
                        more_menu_active = True

    return {
        "admin_type": admin_type,
        "all_admin_types": all_admin_types,
        "is_front_page": is_front_page,
        "more_active": more_menu_active,
    }
Exemplo n.º 2
0
    def front_page(self, request, extra_context=None, *args, **kwargs):
        """
        Displays the admin's front page, which lists all models in all
        installed apps, split up by which 'admin_type' (if any) they have
        been assigned in their respective ModelAdmin classes.
        """
        user = request.user

        apps = {}
        app_list = build_app_list(site._registry)

        for admin_type in app_list.keys():
            apps[admin_type] = {
                'apps': {},
                'description': get_admin_type_description(admin_type),
                'index_url': admin_type + '_index',
                'priority': get_admin_type_priority(admin_type),
                'raw_name': admin_type,
                'verbose_name': get_verbose_admin_type(admin_type),
            }

            for model, model_admin in app_list[admin_type].iteritems():
                app_label = model._meta.app_label
                has_module_perms = user.has_module_perms(app_label)

                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():
                        model_dict = {
                            'name': capfirst(model._meta.verbose_name_plural),
                            'admin_url': mark_safe('%s/%s/%s/' % (admin_type, app_label, model.__name__.lower())),
                            'perms': perms,
                        }
                        if app_label in apps[admin_type]['apps']:
                            apps[admin_type]['apps'][app_label]['models'].append(model_dict)
                        else:
                            apps[admin_type]['apps'][app_label] = {
                                'name': app_label.title(),
                                'app_url': admin_type + '/' + app_label + '/',
                                'has_module_perms': has_module_perms,
                                'models': [model_dict],
                            }
            list = []
            for key in apps[admin_type]['apps'].keys():
                list.append(apps[admin_type]['apps'][key])
            apps[admin_type]['apps'] = list

        context = {
            'title': None,
            'app_list': sorted(apps.values(), key=itemgetter('priority', 'verbose_name')),
            'admin_front_page': True,
            'is_front_page': True,
            'sidebar_template': self.front_page_sidebar_template or self.default_sidebar_template or "admin/sidebar.html",
        }
        context.update(extra_context or {})
        context_instance = template.RequestContext(request, current_app=site.name)
        return render_to_response('admin/front_page.html', context,
            context_instance=context_instance
        )