forked from jsocol/django-adminplus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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
Showing
3 changed files
with
72 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
|