Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Allow to name urls patterns for reverses, redirects, etc. #4

Merged
merged 5 commits into from

2 participants

This page is out of date. Refresh to see the latest.
Showing with 24 additions and 9 deletions.
  1. +8 −2 README.rst
  2. +16 −7 adminplus/__init__.py
View
10 README.rst
@@ -81,11 +81,17 @@ arbitrary views to the admin::
Now ``my_view`` will be accessible at ``admin/somepath`` and there will be a
link to it in the *Custom Views* section of the admin index.
-``register_view`` takes a 3rd, optional argument: a friendly name for display
-in the list of custom views. For example::
+``register_view`` takes some optional arguments:
+
+* ``name``: a friendly name for display in the list of custom views. For example::
def my_view(request):
"""Does something fancy!"""
admin.site.register_view('somepath', my_view, 'My Fancy Admin View!')
+* ``urlname``: give a name to the urlpattern so it can be called by
+ ``redirect()``, ``reverse()``, etc.
+* `visible`: a boolean that defines if the custom view is visible in the admin
+ dashboard.
+
All registered views are wrapped in ``admin.site.admin_view``.
View
23 adminplus/__init__.py
@@ -11,7 +11,7 @@ class AdminSitePlus(AdminSite):
index_template = 'adminplus/index.html' # That was easy.
custom_views = []
- def register_view(self, path, view, name=None):
+ 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.
@@ -19,16 +19,20 @@ def register_view(self, path, view, name=None):
* `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))
+ 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 in self.custom_views:
+ for path, view, name, urlname, visible in self.custom_views:
urls += patterns('',
- url(r'^%s$' % path, self.admin_view(view)),
+ url(r'^%s$' % path, self.admin_view(view), name=urlname),
)
return urls
@@ -36,9 +40,14 @@ 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]
+ 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({
Something went wrong with that request. Please try again.