The django-admin-tools dashboard and dashboard modules API

This section describe the API of the django-admin-tools dashboard and dashboard modules. Make sure you read this before creating your custom dashboard and custom modules.

The Dashboard class

class admin_tools.dashboard.models.Dashboard(**kwargs)

Base class for dashboards. The Dashboard class is a simple python list that has three additional properties:

title
The dashboard title, by default, it is displayed above the dashboard in a h2 tag. Default value: ‘Dashboard’.
template
The template to use to render the dashboard. Default value: ‘dashboard/dashboard.html’
columns
An integer that represents the number of columns for the dashboard. Default value: 2.

If you want to customize the look of your dashboard and it’s modules, you can declare css stylesheets and/or javascript files to include when rendering the dashboard, for example:

from admin_tools.dashboard.models import *

class MyDashboard(Dashboard):
    class Media:
        css = ('/media/css/mydashboard.css',)
        js = ('/media/js/mydashboard.js',)

Here’s an example of a custom dashboard:

from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
from admin_tools.dashboard.models import *

class MyDashboard(Dashboard):
    def __init__(self, **kwargs):
        # we want a 3 columns layout
        self.columns = 3

        # append an app list module for "Applications"
        self.children.append(AppListDashboardModule(
            title=_('Applications'),
            exclude_list=('django.contrib',),
        ))

        # append an app list module for "Administration"
        self.children.append(AppListDashboardModule(
            title=_('Administration'),
            include_list=('django.contrib',),
        ))

        # append a recent actions module
        self.children.append(RecentActionsDashboardModule(
            title=_('Recent Actions'),
            limit=5
        ))

Below is a screenshot of the resulting dashboard:

_images/dashboard_example.png
get_id()
Internal method used to distinguish different dashboards in js code.
init_with_context(context)
Sometimes you may need to access context or request variables to build your dashboard, this is what the init_with_context() method is for. This method is called just before the display with a django.template.RequestContext as unique argument, so you can access to all context variables and to the django.http.HttpRequest.

The AppIndexDashboard class

class admin_tools.dashboard.models.AppIndexDashboard(app_title, models, **kwargs)

Class that represents an app index dashboard, app index dashboards are displayed in the applications index page. AppIndexDashboard is very similar to the Dashboard class except that its constructor receives two extra arguments:

app_title
The title of the application
models

A list of strings representing the available models for the current application, example:

['yourproject.app.Model1', 'yourproject.app.Model2']

It also provides two helper methods:

get_app_model_classes()
Method that returns the list of model classes for the current app.
get_app_content_types()
Method that returns the list of content types for the current app.

If you want to provide custom app index dashboard, be sure to inherit from this class instead of the Dashboard class.

Here’s an example of a custom app index dashboard:

from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
from admin_tools.dashboard.models import *

class MyAppIndexDashboard(AppIndexDashboard):
    def __init__(self, **kwargs):
        AppIndexDashboard.__init__(self, **kwargs)
        # we don't want a title, it's redundant
        self.title = ''

        # append a model list module that lists all models 
        # for the app
        self.children.append(ModelListDashboardModule(
            title=self.app_title,
            include_list=self.models,
        ))

        # append a recent actions module for the current app
        self.children.append(RecentActionsDashboardModule(
            title=_('Recent Actions'),
            include_list=self.models,
            limit=5
        ))

Below is a screenshot of the resulting dashboard:

_images/dashboard_app_index_example.png
get_app_content_types()
Return a list of all content_types for this app.
get_app_model_classes()
Helper method that returns a list of model classes for the current app.
get_id()
Internal method used to distinguish different dashboards in js code.

The DashboardModule class

class admin_tools.dashboard.models.DashboardModule(**kwargs)

Base class for all dashboard modules. Dashboard modules have the following properties:

enabled
Boolean that determines whether the module should be enabled in the dashboard by default or not. Default value: True.
draggable
Boolean that determines whether the module can be draggable or not. Draggable modules can be re-arranged by users. Default value: True.
collapsible
Boolean that determines whether the module is collapsible, this allows users to show/hide module content. Default: True.
deletable
Boolean that determines whether the module can be removed from the dashboard by users or not. Default: True.
title
String that contains the module title, make sure you use the django gettext functions if your application is multilingual. Default value: ‘’.
title_url
String that contains the module title URL. If given the module title will be a link to this URL. Default value: None.
css_classes
A list of css classes to be added to the module div class attribute. Default value: None.
pre_content
Text or HTML content to display above the module content. Default value: None.
content
The module text or HTML content. Default value: None.
post_content
Text or HTML content to display under the module content. Default value: None.
template
The template to use to render the module. Default value: ‘dashboard/module.html’.
init_with_context(context)

Like for the Dashboard class, dashboard modules have a init_with_context method that is called with a django.template.RequestContext instance as unique argument.

This gives you enough flexibility to build complex modules, for example, let’s build a “history” dashboard module, that will list the last ten visited pages:

class HistoryDashboardModule(LinkListDashboardModule):
    def init_with_context(self, context):
        self.title = 'History'
        request = context['request']
        # we use sessions to store the visited pages stack
        history = request.session.get('history', [])
        for item in history:
            self.children.append(item)
        # add the current page to the history
        history.insert(0, {
            'title': context['title'],
            'url': request.META['PATH_INFO']
        })
        if len(history) > 10:
            history = history[:10]
        request.session['history'] = history

Here’s a screenshot of our history item:

_images/history_dashboard_module.png
is_empty()

Return True if the module has no content and False otherwise.

>>> mod = DashboardModule()
>>> mod.is_empty()
True
>>> mod.pre_content = 'foo'
>>> mod.is_empty()
False
>>> mod.pre_content = None
>>> mod.is_empty()
True
>>> mod.children.append('foo')
>>> mod.is_empty()
False
>>> mod.children = []
>>> mod.is_empty()
True
render_css_classes()

Return a string containing the css classes for the module.

>>> mod = DashboardModule(enabled=False, draggable=True, 
...                       collapsible=True, deletable=True)
>>> mod.render_css_classes()
'dashboard-module disabled draggable collapsible deletable'
>>> mod.css_classes.append('foo')
>>> mod.render_css_classes()
'dashboard-module disabled draggable collapsible deletable foo'
>>> mod.enabled = True
>>> mod.render_css_classes()
'dashboard-module draggable collapsible deletable foo'

The LinkListDashboardModule class

class admin_tools.dashboard.models.LinkListDashboardModule(**kwargs)

A module that displays a list of links. As well as the DashboardModule properties, the LinkListDashboardModule takes an extra keyword argument:

layout
The layout of the list, possible values are stacked and inline. The default value is stacked.

Link list modules children are simple python dictionaries that can have the following keys:

title
The link title.
url
The link URL.
external
Boolean that indicates whether the link is an external one or not.
description
A string describing the link, it will be the title attribute of the html a tag.

Here’s a small example of building a link list module:

from admin_tools.dashboard.models import *

class MyDashboard(Dashboard):
    def __init__(self, **kwargs): 
        Dashboard.__init__(self, **kwargs)

        self.children.append(LinkListDashboardModule(
            layout='inline',
            children=(
                {
                    'title': 'Python website',
                    'url': 'http://www.python.org',
                    'external': True,
                    'description': 'Python programming language rocks !',
                },
                {
                    'title': 'Django website',
                    'url': 'http://www.djangoproject.com',
                    'external': True
                },
                {
                    'title': 'Some internal link',
                    'url': '/some/internal/link/',
                    'external': False
                },
            )
        ))

The screenshot of what this code produces:

_images/linklist_dashboard_module.png

The AppListDashboardModule class

class admin_tools.dashboard.models.AppListDashboardModule(**kwargs)

Module that lists installed apps and their models. As well as the DashboardModule properties, the AppListDashboardModule has two extra properties:

exclude_list
A list of apps to exclude, if an app name (e.g. “django.contrib.auth” starts with an element of this list (e.g. “django.contrib”) it won’t appear in the dashboard module.
include_list
A list of apps to include, only apps whose name (e.g. “django.contrib.auth”) starts with one of the strings (e.g. “django.contrib”) in the list will appear in the dashboard module.

If no include/exclude list is provided, all apps are shown.

Here’s a small example of building an app list module:

from admin_tools.dashboard.models import *

class MyDashboard(Dashboard):
    def __init__(self, **kwargs): 
        Dashboard.__init__(self, **kwargs)

        # will only list the django.contrib apps
        self.children.append(AppListDashboardModule(
            title='Administration',
            include_list=('django.contrib',)
        ))
        # will list all apps except the django.contrib ones
        self.children.append(AppListDashboardModule(
            title='Applications',
            exclude_list=('django.contrib',)
        ))

The screenshot of what this code produces:

_images/applist_dashboard_module.png

Note

Note that this module takes into account user permissions, for example, if a user has no rights to change or add a Group, then the django.contrib.auth.Group model line will not be displayed.

The RecentActionsDashboardModule class

class admin_tools.dashboard.models.RecentActionsDashboardModule(**kwargs)

Module that lists the recent actions for the current user. As well as the DashboardModule properties, the RecentActionsDashboardModule takes three extra keyword arguments:

include_list
A list of contenttypes (e.g. “auth.group” or “sites.site”) to include, only recent actions that match the given contenttypes will be displayed.
exclude_list
A list of contenttypes (e.g. “auth.group” or “sites.site”) to exclude, recent actions that match the given contenttypes will not be displayed.
limit
The maximum number of children to display. Default value: 10.

Here’s a small example of building a recent actions module:

from admin_tools.dashboard.models import *

class MyDashboard(Dashboard):
    def __init__(self, **kwargs): 
        Dashboard.__init__(self, **kwargs)

        # will only list the django.contrib apps
        self.children.append(RecentActionsDashboardModule(
            title='Django CMS recent actions',
            include_list=('cms.page', 'cms.cmsplugin',)
        ))

The screenshot of what this code produces:

_images/recentactions_dashboard_module.png

The FeedDashboardModule class

class admin_tools.dashboard.models.FeedDashboardModule(**kwargs)

Class that represents a feed dashboard module.

Important

This class uses the Universal Feed Parser module to parse the feeds, so you’ll need to install it, all feeds supported by FeedParser are thus supported by the FeedDashboardModule.

As well as the DashboardModule properties, the FeedDashboardModule takes two extra keyword arguments:

feed_url
The URL of the feed.
limit
The maximum number of feed children to display. Default value: None, which means that all children are displayed.

Here’s a small example of building a recent actions module:

from admin_tools.dashboard.models import *

class MyDashboard(Dashboard):
    def __init__(self, **kwargs): 
        Dashboard.__init__(self, **kwargs)

        # will only list the django.contrib apps
        self.children.append(FeedDashboardModule(
            title=_('Latest Django News'),
            feed_url='http://www.djangoproject.com/rss/weblog/',
            limit=5
        ))

The screenshot of what this code produces:

_images/feed_dashboard_module.png