Refactored sorting_filters into a tuple and moved logic for retrieving sorting filters into the sort_view versus changelist_view.

Updated readme.
Updated sample project to leverage new sorting_filters on Person model and admin.
Removed custom change list template for Person admin.
This commit is contained in:
Brandon Taylor
2014-03-05 09:10:47 -05:00
parent af6c8f2e15
commit 80bdaafdc3
9 changed files with 70 additions and 51 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
VERSION = (1, 6, 5) # following PEP 386
VERSION = (1, 6, 6) # following PEP 386
DEV_N = None
+21 -10
View File
@@ -28,8 +28,6 @@ STATIC_URL = settings.STATIC_URL
class SortableAdminBase(object):
filtered_objects = []
def changelist_view(self, request, extra_context=None):
"""
If the model that inherits Sortable has more than one object,
@@ -37,11 +35,10 @@ class SortableAdminBase(object):
object_tools block to take people to the view to change the sorting.
"""
# Apply any additional filters to create a subset of sortable objects
self.filtered_objects = self.queryset(request).filter(
**self.model.sorting_filters)
# get sort group index from querystring
sort_filter_index = request.GET.get('sort_filter')
if get_is_sortable(self.filtered_objects):
if get_is_sortable(self.queryset(request)):
self.change_list_template = \
self.sortable_change_list_with_sort_link_template
self.is_sortable = True
@@ -50,7 +47,8 @@ class SortableAdminBase(object):
extra_context = {}
extra_context.update({
'change_list_template_extends': self.change_list_template_extends
'change_list_template_extends': self.change_list_template_extends,
'sorting_filters': [sort_filter[0] for sort_filter in self.model.sorting_filters]
})
return super(SortableAdminBase, self).changelist_view(request,
extra_context=extra_context)
@@ -106,7 +104,18 @@ class SortableAdmin(SortableAdminBase, ModelAdmin):
has_perm = request.user.has_perm('{0}.{1}'.format(opts.app_label,
opts.get_change_permission()))
objects = self.filtered_objects
# get sort group index from querystring if present
sort_filter_index = request.GET.get('sort_filter')
filters = {}
if sort_filter_index:
try:
filters = self.model.sorting_filters[int(sort_filter_index)][1]
except IndexError, ValueError:
pass
# Apply any sort filters to create a subset of sortable objects
objects = self.queryset(request).filter(**filters)
# Determine if we need to regroup objects relative to a
# foreign key specified on the model class that is extending Sortable.
@@ -191,9 +200,11 @@ class SortableAdmin(SortableAdminBase, ModelAdmin):
})
for klass in self.inlines:
if issubclass(klass, SortableTabularInline) or issubclass(klass, SortableGenericTabularInline):
if issubclass(klass, SortableTabularInline) or issubclass(klass,
SortableGenericTabularInline):
self.has_sortable_tabular_inlines = True
if issubclass(klass, SortableStackedInline) or issubclass(klass, SortableGenericStackedInline):
if issubclass(klass, SortableStackedInline) or issubclass(klass,
SortableGenericStackedInline):
self.has_sortable_stacked_inlines = True
if self.has_sortable_tabular_inlines or \
+1 -4
View File
@@ -14,9 +14,6 @@ class MultipleSortableForeignKeyException(Exception):
class Sortable(models.Model):
"""
Unfortunately, Django doesn't support using more than one AutoField
in a model or this class could be simplified.
`is_sortable` determines whether or not the Model is sortable by
determining if the last value of `order` is greater than the default
of 1, which should be present if there is only one object.
@@ -31,7 +28,7 @@ class Sortable(models.Model):
order = models.PositiveIntegerField(editable=False, default=1,
db_index=True)
is_sortable = False
sorting_filters = {}
sorting_filters = ()
# legacy support
sortable_by = None
@@ -2,8 +2,14 @@
{% load i18n %}
{% block object-tools-items %}
<li>
<a href="./sort/">{% trans 'Change Order' %}</a>
</li>
{{ block.super }}
{% for sorting_filter in sorting_filters %}
<li>
<a href="./sort/?sort_filter={{ forloop.counter0 }}">{% trans 'Change Order of' %} {{ sorting_filter }}</a>
</li>
{% empty %}
<li>
<a href="./sort/">{% trans 'Change Order' %}</a>
</li>
{% endfor %}
{{ block.super }}
{% endblock %}