Refactored do_sorting_view to get start_index default value from len(indexes) rather than duplicate the line and set to 0.

Added NonSortableParentAdmin class to enable sorting URLs and JavaScript on models that do not inherit from Sortable.
Removed unused 'ordering' property on SortableAdmin.
Changed assignment of sorting_filters to use getattr, enabling NonSortableParentAdmin to inherit from SortableAdminBase.
Moved template properties to SortableAdminBase.
master
Brandon Taylor 2014-09-21 15:12:30 -04:00
parent b880018f71
commit f33f5e90ed
1 changed files with 23 additions and 17 deletions

View File

@ -27,6 +27,14 @@ STATIC_URL = settings.STATIC_URL
class SortableAdminBase(object): class SortableAdminBase(object):
sortable_change_list_with_sort_link_template = \
'adminsortable/change_list_with_sort_link.html'
sortable_change_form_template = 'adminsortable/change_form.html'
sortable_change_list_template = 'adminsortable/change_list.html'
change_form_template_extends = 'admin/change_form.html'
change_list_template_extends = 'admin/change_list.html'
def changelist_view(self, request, extra_context=None): def changelist_view(self, request, extra_context=None):
""" """
If the model that inherits Sortable has more than one object, If the model that inherits Sortable has more than one object,
@ -45,7 +53,7 @@ class SortableAdminBase(object):
extra_context.update({ 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 'sorting_filters': [sort_filter[0] for sort_filter
in self.model.sorting_filters] in getattr(self.model, 'sorting_filters', [])]
}) })
return super(SortableAdminBase, self).changelist_view(request, return super(SortableAdminBase, self).changelist_view(request,
extra_context=extra_context) extra_context=extra_context)
@ -56,15 +64,6 @@ class SortableAdmin(SortableAdminBase, ModelAdmin):
Admin class to add template overrides and context objects to enable Admin class to add template overrides and context objects to enable
drag-and-drop ordering. drag-and-drop ordering.
""" """
ordering = ('order', 'id')
sortable_change_list_with_sort_link_template = \
'adminsortable/change_list_with_sort_link.html'
sortable_change_form_template = 'adminsortable/change_form.html'
sortable_change_list_template = 'adminsortable/change_list.html'
change_form_template_extends = 'admin/change_form.html'
change_list_template_extends = 'admin/change_list.html'
class Meta: class Meta:
abstract = True abstract = True
@ -89,6 +88,7 @@ class SortableAdmin(SortableAdminBase, ModelAdmin):
Custom admin view that displays the objects as a list whose sort Custom admin view that displays the objects as a list whose sort
order can be changed via drag-and-drop. order can be changed via drag-and-drop.
""" """
opts = self.model._meta opts = self.model._meta
has_perm = request.user.has_perm('{0}.{1}'.format(opts.app_label, has_perm = request.user.has_perm('{0}.{1}'.format(opts.app_label,
opts.get_change_permission())) opts.get_change_permission()))
@ -226,17 +226,17 @@ class SortableAdmin(SortableAdminBase, ModelAdmin):
id=model_type_id).model_class() id=model_type_id).model_class()
objects_dict = dict([(str(obj.pk), obj) for obj in objects_dict = dict([(str(obj.pk), obj) for obj in
klass.objects.filter(pk__in=indexes)]) klass.objects.filter(pk__in=indexes)])
if '-order' in klass._meta.ordering: # desc order
if '-order' in klass._meta.ordering:
step = -1
start_object = max(objects_dict.values(), start_object = max(objects_dict.values(),
key=lambda x: getattr(x, 'order')) key=lambda x: getattr(x, 'order'))
start_index = getattr(start_object, 'order') \ else:
or len(indexes) step = 1
step = -1
else: # 'order' is default, asc order
start_object = min(objects_dict.values(), start_object = min(objects_dict.values(),
key=lambda x: getattr(x, 'order')) key=lambda x: getattr(x, 'order'))
start_index = getattr(start_object, 'order') or 0
step = 1 start_index = getattr(start_object, 'order', len(indexes))
for index in indexes: for index in indexes:
obj = objects_dict.get(index) obj = objects_dict.get(index)
@ -252,6 +252,12 @@ class SortableAdmin(SortableAdminBase, ModelAdmin):
content_type='application/json') content_type='application/json')
class NonSortableParentAdmin(SortableAdmin):
def changelist_view(self, request, extra_context=None):
return super(SortableAdminBase, self).changelist_view(request,
extra_context=extra_context)
class SortableInlineBase(SortableAdminBase, InlineModelAdmin): class SortableInlineBase(SortableAdminBase, InlineModelAdmin):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(SortableInlineBase, self).__init__(*args, **kwargs) super(SortableInlineBase, self).__init__(*args, **kwargs)