Working custom admin view.

This commit is contained in:
Chris Jones
2012-01-16 13:27:52 -08:00
parent 58d59af2d9
commit b7f1ad5fd0
7 changed files with 95 additions and 2 deletions

View File

@@ -0,0 +1,45 @@
from django.contrib.admin.sites import AdminSite
from django.utils.text import capfirst
VERSION = (0, 1, 0)
__version__ = '.'.join([str(x) for x in VERSION])
class DjrillAdminSite(AdminSite):
index_template = "djrill/index.html"
custom_views = []
def register_view(self, path, view, name=None):
"""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__.
"""
self.custom_views.append((path, view, name))
def get_urls(self):
"""Add our custom views to the admin urlconf."""
urls = super(DjrillAdminSite, self).get_urls()
from django.conf.urls.defaults import patterns, url
for path, view, name in self.custom_views:
urls += patterns('',
url(r'^%s$' % path, self.admin_view(view)),
)
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 = [(path, name if name else
capfirst(view.__name__)) for path, view, name in
self.custom_views]
# Sort views alphabetically.
custom_list.sort(key=lambda x: x[1])
extra_context.update({
'custom_list': custom_list
})
return super(DjrillAdminSite, self).index(request, extra_context)

View File

@@ -0,0 +1,6 @@
from django.contrib import admin
from djrill.views import AdminListView
admin.site.register_view("djrill", AdminListView.as_view(), "Djrill")

View File

@@ -0,0 +1,7 @@
{% extends "admin/base_site.html" %}
{% block breadcrumbs %}
<div class="breadcrumbs">
<a href="{% url admin:index %}">Home</a> &rsaquo; {{ title }}
</div>
{% endblock %}

View File

@@ -0,0 +1,18 @@
{% extends "admin/index.html" %}
{% block sidebar %}
{{ block.super }}
{% if custom_list %}
<div class="module" style="float: left; width: 498px">
<table style="width: 100%">
<caption>Djrill</caption>
<tbody>
{% for path, name in custom_list %}
<tr><td><a href="{{ path }}">{{ name }}</a></td></tr>
{% endfor %}
</tbody>
</table>
</div>
{% endif %}
{% endblock %}

View File

@@ -0,0 +1,12 @@
from django.core.urlresolvers import reverse
from django.http import HttpResponse
from django.views.generic import View
class AdminListView(View):
def get(self, request):
return HttpResponse("HOLA")
def admin_list_view(request):
return HttpResponse("Djrill Index")

View File

@@ -104,7 +104,7 @@ MIDDLEWARE_CLASSES = (
'django.contrib.messages.middleware.MessageMiddleware', 'django.contrib.messages.middleware.MessageMiddleware',
) )
ROOT_URLCONF = 'djrill.urls' ROOT_URLCONF = 'urls'
TEMPLATE_DIRS = ( TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
@@ -120,6 +120,8 @@ INSTALLED_APPS = (
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'django.contrib.admin', 'django.contrib.admin',
"djrill",
) )
# A sample logging configuration. The only tangible logging # A sample logging configuration. The only tangible logging

View File

@@ -1,6 +1,9 @@
from django.conf.urls.defaults import patterns, include, url from django.conf.urls.defaults import patterns, include, url
from django.contrib import admin from django.contrib import admin
from djrill import DjrillAdminSite
admin.site = DjrillAdminSite()
admin.autodiscover() admin.autodiscover()
urlpatterns = patterns('', urlpatterns = patterns('',