Skip to content

Commit

Permalink
Reorganize and clean up.
Browse files Browse the repository at this point in the history
- AdminPlus functionality is now a mixin, so it can play nicely (in
  theory, anyway) with other Django admin-enhancing libraries.
- The class is now stored in the adminplus.sites module.
- There is a default instance at adminplus.site.
- Most code has been removed from __init__.py, which just makes me
  happy.
  • Loading branch information
jsocol committed Mar 24, 2013
1 parent 09fa387 commit 11ed970
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 55 deletions.
60 changes: 8 additions & 52 deletions adminplus/__init__.py
Original file line number Diff line number Diff line change
@@ -1,56 +1,12 @@
from django.contrib.admin.sites import AdminSite
from django.utils.text import capfirst
from __future__ import absolute_import

try:
from .sites import AdminSitePlus

VERSION = (0, 1, 7)
__version__ = '.'.join([str(x) for x in VERSION])
site = AdminSitePlus()
except ImportError:
pass


class AdminSitePlus(AdminSite):
"""Extend AdminSite to allow registering custom admin views."""
index_template = 'adminplus/index.html' # That was easy.
custom_views = []

def register_view(self, path, view, name=None, urlname=None, visible=True):
"""Add a custom admin view.
* `path` is the path in the admin where the view will live, e.g.
http://example.com/admin/somepath
* `view` is any view function you can imagine.
* `name` is an optional pretty name for the list of custom views. If
empty, we'll guess based on view.__name__.
* `urlname` is an optional parameter to be able to call the view with a
redirect() or reverse()
* `visible` is a boolean to set if the custom view should be visible in
the admin dashboard or not.
"""
self.custom_views.append((path, view, name, urlname, visible))

def get_urls(self):
"""Add our custom views to the admin urlconf."""
urls = super(AdminSitePlus, self).get_urls()
from django.conf.urls.defaults import patterns, url
for path, view, name, urlname, visible in self.custom_views:
urls += patterns('',
url(r'^%s$' % path, self.admin_view(view), name=urlname),
)
return urls

def index(self, request, extra_context=None):
"""Make sure our list of custom views is on the index page."""
if not extra_context:
extra_context = {}
custom_list = []
for path, view, name, urlname, visible in self.custom_views:
if visible is True:
if name:
custom_list.append((path, name))
else:
custom_list.append((path, capfirst(view.__name__)))

# Sort views alphabetically.
custom_list.sort(key=lambda x: x[1])
extra_context.update({
'custom_list': custom_list
})
return super(AdminSitePlus, self).index(request, extra_context)
VERSION = (0, 2, 'dev')
__version__ = '.'.join(map(str, VERSION))
59 changes: 59 additions & 0 deletions adminplus/sites.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from django.contrib.admin.sites import AdminSite
from django.utils.text import capfirst


class AdminPlusMixin(object):
"""Mixin for AdminSite to allow registering custom admin views."""

index_template = 'adminplus/index.html' # That was easy.
custom_views = []

def register_view(self, path, view, name=None, urlname=None, visible=True):
"""Add a custom admin view.
* `path` is the path in the admin where the view will live, e.g.
http://example.com/admin/somepath
* `view` is any view function you can imagine.
* `name` is an optional pretty name for the list of custom views. If
empty, we'll guess based on view.__name__.
* `urlname` is an optional parameter to be able to call the view with a
redirect() or reverse()
* `visible` is a boolean to set if the custom view should be visible in
the admin dashboard or not.
"""
self.custom_views.append((path, view, name, urlname, visible))

def get_urls(self):
"""Add our custom views to the admin urlconf."""
urls = super(AdminSitePlus, self).get_urls()
from django.conf.urls.defaults import patterns, url
for path, view, name, urlname, visible in self.custom_views:
urls += patterns(
'',
url(r'^%s$' % path, self.admin_view(view), name=urlname),
)
return urls

def index(self, request, extra_context=None):
"""Make sure our list of custom views is on the index page."""
if not extra_context:
extra_context = {}
custom_list = []
for path, view, name, urlname, visible in self.custom_views:
if visible is True:
if name:
custom_list.append((path, name))
else:
custom_list.append((path, capfirst(view.__name__)))

# Sort views alphabetically.
custom_list.sort(key=lambda x: x[1])
extra_context.update({
'custom_list': custom_list
})
return super(AdminSitePlus, self).index(request, extra_context)


class AdminSitePlus(AdminPlusMixin, AdminSite):
"""A Django AdminSite with the AdminPlusMixin to allow registering custom
views not connected to models."""
8 changes: 5 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
from setuptools import setup
from setuptools import setup, find_packages

import adminplus


setup(
name='django-adminplus',
version='0.1.7',
version=adminplus.__version__,
description='Add new pages to the Django admin.',
long_description=open('README.rst').read(),
author='James Socol',
author_email='[email protected]',
url='http://github.com/jsocol/django-adminplus',
license='BSD',
packages=['adminplus'],
packages=find_packages(exclude=['test_settings']),
include_package_data=True,
package_data = {'': ['README.rst', 'templates/adminplus/*.html']},
zip_safe=False,
Expand Down

0 comments on commit 11ed970

Please sign in to comment.