Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added dashboard app #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions wpadmin/dashboard/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from wpadmin.dashboard.dashboards import *
from wpadmin.dashboard.registry import *

default_app_config = "wpadmin.dashboard.apps.DashboardConfig"
10 changes: 10 additions & 0 deletions wpadmin/dashboard/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# coding: utf-8

# DJANGO IMPORTS
from django.apps import AppConfig


class DashboardConfig(AppConfig):
name = "wpadmin.dashboard"
label = "wpadmin.dashboard"
verbose_name = "WP Admin Dashboard"
192 changes: 192 additions & 0 deletions wpadmin/dashboard/dashboards.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
# coding: utf-8

"""
Module where wpadmin dashboard classes are defined.
"""

# DJANGO IMPORTS
from django.utils.translation import ugettext_lazy as _
from django.core.urlresolvers import reverse
from django import forms

# WPADMIN IMPORTS
from wpadmin.dashboard import modules
from wpadmin.dashboard.utils import get_admin_site_name


class Dashboard(object):
"""
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: 'admin_tools/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 (these files should be placed in your
media path), for example::

from admin_tools.dashboard import Dashboard

class MyDashboard(Dashboard):
class Media:
css = {
'all': (
'css/mydashboard.css',
'css/mystyles.css',
),
}
js = (
'js/mydashboard.js',
'js/myscript.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 import modules, Dashboard

class MyDashboard(Dashboard):

# we want a 3 columns layout
columns = 3

def __init__(self, **kwargs):
super(MyDashboard, self).__init__(**kwargs)

# append an app list module for "Applications"
self.children.append(modules.AppList(
title=_('Applications'),
exclude=('django.contrib.*',),
))

# append an app list module for "Administration"
self.children.append(modules.AppList(
title=_('Administration'),
models=('django.contrib.*',),
))

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

"""

# Using Django's Media meta class
__metaclass__ = forms.MediaDefiningClass

def _media(self):
return forms.Media()
media = property(_media)

title = _('Dashboard')
template = 'wpadmin/dashboard/dashboard.html'
columns = 2
children = None

def __init__(self, **kwargs):
for key in kwargs:
if hasattr(self.__class__, key):
setattr(self, key, kwargs[key])
self.children = self.children or []

def init_with_context(self, 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``.
"""
pass

def get_id(self):
"""
Internal method used to distinguish different dashboards in js code.
"""
return 'wpadmin-dashboard'


class DefaultIndexDashboard(Dashboard):
"""
The default dashboard displayed on the admin index page.
To change the default dashboard you'll have to type the following from the
commandline in your project root directory::

python manage.py customdashboard

And then set the `WPADMIN_INDEX_DASHBOARD`` settings variable to
point to your custom index dashboard class.
"""

def init_with_context(self, context):
site_name = get_admin_site_name(context)
# append a link list module for "quick links"
self.children.append(modules.LinkList(
_('Quick links'),
collapsible=False,
children=[
[_('Return to site'), '/'],
[_('Change password'),
reverse('%s:password_change' % site_name)],
[_('Log out'), reverse('%s:logout' % site_name)],
]
))

# append an app list module for "Applications"
self.children.append(modules.AppList(
_('Applications'),
exclude=('django.contrib.*',),
))

# append an app list module for "Administration"
self.children.append(modules.AppList(
_('Administration'),
models=('django.contrib.*',),
))

# append a recent actions module
self.children.append(modules.RecentActions(_('Recent Actions'), 5))

# append a feed module
self.children.append(modules.Feed(
_('Latest Django News'),
feed_url='http://www.djangoproject.com/rss/weblog/',
limit=5
))

# append another link list module for "support".
self.children.append(modules.LinkList(
_('Support'),
children=[
{
'title': _('Django documentation'),
'url': 'http://docs.djangoproject.com/',
'external': True,
},
{
'title': _('Django "django-users" mailing list'),
'url': 'http://groups.google.com/group/django-users',
'external': True,
},
{
'title': _('Django irc channel'),
'url': 'irc://irc.freenode.net/django',
'external': True,
},
]
))
Empty file.
Empty file.
28 changes: 28 additions & 0 deletions wpadmin/dashboard/management/commands/customdashboard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# coding: utf-8

# PYTHON IMPORTS
import os

# DJANGO IMPORTS
from django.core.management.base import BaseCommand, CommandError
from django.template.loader import render_to_string

DEFAULT_FILE = 'dashboard.py'


class Command(BaseCommand):
help = ('Creates a template file containing the base code to get you '
'started with your custom dashboard.')
args = '[file]'
label = 'application name'

def handle(self, file=None, **options):
context = {}
context['project'] = os.path.basename(os.getcwd())
tpl = ['dashboard/dashboard.txt', 'wpadmin/dashboard/dashboard.txt']
dst = file is not None and file or DEFAULT_FILE
if os.path.exists(dst):
raise CommandError('file "%s" already exists' % dst)
context['file'] = os.path.basename(dst).split('.')[0]
open(dst, 'w').write(render_to_string(tpl, context))
print('"%s" written.' % os.path.join(dst))
Loading