Commits

Matthew Schinckel committed 0948cdd

Initial import

Comments (0)

Files changed (6)

+# django-admin-additions
+
+There are a few things about the django admin that get me down. Here are solutions to
+those, all bundled up together.
+
+## Installation
+
+Add `'admin_additions'` to you `settings.INSTALLED_APPS`.
+
+Then configure the additions you want to use. The default settings are shown:
+
+    ADMIN_ADDITIONS = {
+        'RETURN_TO_FILTERED_CHANGELIST': False,
+        'SAVE_ON_TOP': True,
+        'LIST_SELECT_RELATED': False,
+        'FULLY_DYNAMIC_FORMSETS': True,
+    }
+
+## Modules/patches/settings
+
+### Return to filtered changelist view
+
+When editing an object, you usually want to revert back to the filtered view
+from whence you came.
+
+This is a monkey-patch that supplements the normal `change_view` method on the
+`ModelAdmin` base class, and ensures that it returns after a POST back to the 
+referring view.
+
+### Save on top
+
+Should be the default: display the save toolbar on the top of every `change_view`.
+
+### LIST_SELECT_RELATED
+
+Turn on automatic `.select_related()` for all queries for a change_list. If you have
+altered the display columns, and do any lookups at all, this is a good idea.
+
+### FULLY_DYNAMIC_FORMSETS
+
+Sets the `extra` value on `InlineModelAdmin` to 0, so you just use the addition button
+instead of having any empty formsets.
Add a comment to this file

admin_additions/__init__.py

Empty file added.

admin_additions/admin.py

+from django.conf import settings
+from django.contrib import admin
+
+if hasattr(settings, 'ADMIN_ADDITIONS'):
+    additions = settings.ADMIN_ADDITIONS
+    
+    if additions.get('RETURN_TO_FILTERED_CHANGELIST', False):
+        import change_view_referer
+    
+    admin.ModelAdmin.save_on_top = additions.get('SAVE_ON_TOP', True)
+    
+    admin.options.list_select_related = additions.get('LIST_SELECT_RELATED', False)
+    
+    if additions.get('FULLY_DYNAMIC_FORMSETS', True):
+        admin.options.InlineModelAdmin.extra = 0
+    
+    

admin_additions/change_view_referer.py

+"""
+This method of returning to the filtered view after submitting a form in an admin
+change view is based largely upon http://djangosnippets.org/snippets/2531/
+
+The changes:
+
+    - Don't use a literal {} in a function/method declaration. It really is bad.
+    - Use request.METHOD to determine if we want to set or use the referer.
+    - Monkey patch all admin models.
+    
+"""
+
+from django.contrib.admin import ModelAdmin
+from django import forms
+
+old_change_view = ModelAdmin.change_view
+
+def change_view(self, request, object_id, extra_content=None):
+    result = old_change_view(self, request, object_id, extra_content)
+    
+    if request.method != 'POST':
+        ref = request.META.get('HTTP_REFERER', None)
+        if ref:
+            request.session['filtered'] = ref
+    elif request.POST.has_key('_save'):
+        if request.session.get('filtered', None):
+            result['Location'] = request.session['filtered']
+            request.session['filtered'] = None
+    
+    return result
+
+ModelAdmin.change_view = change_view
Add a comment to this file

admin_additions/models.py

Empty file added.

+from distutils.core import setup
+
+setup(
+    name = "django-admin-additions",
+    version = "0.1",
+    description = "Admin additions.",
+    url = "http://hg.schinckel.net/django-admin-additions",
+    author = "Matthew Schinckel",
+    author_email = "matt@schinckel.net",
+    packages = [
+        "admin_additions",
+    ],
+    classifiers = [
+        'Programming Language :: Python',
+        'Operating System :: OS Independent',
+        'Framework :: Django',
+    ],
+)
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.