diff --git a/adminsortable/admin.py b/adminsortable/admin.py index 4ad25cb..fb27575 100644 --- a/adminsortable/admin.py +++ b/adminsortable/admin.py @@ -28,6 +28,8 @@ 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, @@ -35,12 +37,11 @@ class SortableAdminBase(object): object_tools block to take people to the view to change the sorting. """ - if self.model.ordering_subset() is not None: - objects = self.model.ordering_subset() - else: - objects = self.queryset(request) + # Apply any additional filters to create a subset of sortable objects + self.filtered_objects = self.queryset(request).filter( + **self.model.sorting_filters) - if get_is_sortable(objects): + if get_is_sortable(self.filtered_objects): self.change_list_template = \ self.sortable_change_list_with_sort_link_template self.is_sortable = True @@ -66,8 +67,6 @@ class SortableAdmin(SortableAdminBase, ModelAdmin): 'adminsortable/change_list_with_sort_link.html' sortable_change_form_template = 'adminsortable/change_form.html' sortable_change_list_template = 'adminsortable/change_list.html' - sortable_javascript_includes_template = \ - 'adminsortable/shared/javascript_includes.html' change_form_template_extends = 'admin/change_form.html' change_list_template_extends = 'admin/change_list.html' @@ -107,10 +106,7 @@ class SortableAdmin(SortableAdminBase, ModelAdmin): has_perm = request.user.has_perm('{}.{}'.format(opts.app_label, opts.get_change_permission())) - if self.model.ordering_subset() is not None: - objects = self.model.ordering_subset() - else: - objects = self.queryset(request) + objects = self.filtered_objects # Determine if we need to regroup objects relative to a # foreign key specified on the model class that is extending Sortable. @@ -169,9 +165,7 @@ class SortableAdmin(SortableAdminBase, ModelAdmin): 'group_expression': sortable_by_expression, 'sortable_by_class': sortable_by_class, 'sortable_by_class_is_sortable': sortable_by_class_is_sortable, - 'sortable_by_class_display_name': sortable_by_class_display_name, - 'sortable_javascript_includes_template': - self.sortable_javascript_includes_template + 'sortable_by_class_display_name': sortable_by_class_display_name } return render(request, self.sortable_change_list_template, context) @@ -208,8 +202,6 @@ class SortableAdmin(SortableAdminBase, ModelAdmin): self.change_form_template = self.sortable_change_form_template extra_context.update({ - 'sortable_javascript_includes_template': - self.sortable_javascript_includes_template, 'has_sortable_tabular_inlines': self.has_sortable_tabular_inlines, 'has_sortable_stacked_inlines': diff --git a/adminsortable/models.py b/adminsortable/models.py index 52cad36..d3dfef4 100644 --- a/adminsortable/models.py +++ b/adminsortable/models.py @@ -31,6 +31,7 @@ class Sortable(models.Model): order = models.PositiveIntegerField(editable=False, default=1, db_index=True) is_sortable = False + sorting_filters = {} # legacy support sortable_by = None @@ -43,10 +44,6 @@ class Sortable(models.Model): def model_type_id(cls): return ContentType.objects.get_for_model(cls).id - @classmethod - def ordering_subset(cls): - return None - def __init__(self, *args, **kwargs): super(Sortable, self).__init__(*args, **kwargs) diff --git a/adminsortable/templates/adminsortable/change_list.html b/adminsortable/templates/adminsortable/change_list.html index bdc9522..af06213 100644 --- a/adminsortable/templates/adminsortable/change_list.html +++ b/adminsortable/templates/adminsortable/change_list.html @@ -9,11 +9,11 @@ {% block extrahead %} {{ block.super }} - - - - - + + + + + {% endblock %} {% block title %}{% blocktrans with opts.verbose_name_plural|capfirst as model %}Drag and drop {{ model }} to change display order{% endblocktrans %} | {% trans 'Django site admin' %}{% endblock %} diff --git a/adminsortable/utils.py b/adminsortable/utils.py index b38c42a..e2948cf 100644 --- a/adminsortable/utils.py +++ b/adminsortable/utils.py @@ -1,4 +1,5 @@ def get_is_sortable(objects): - if objects.count() > 1: - return True + if objects: + if objects.count() > 1: + return True return False diff --git a/sample_project/app/admin.py b/sample_project/app/admin.py index b0079e4..a00e3eb 100644 --- a/sample_project/app/admin.py +++ b/sample_project/app/admin.py @@ -60,4 +60,8 @@ class ProjectAdmin(SortableAdmin): admin.site.register(Project, ProjectAdmin) -admin.site.register(Person, SortableAdmin) +class PersonAdmin(SortableAdmin): + sortable_change_list_with_sort_link_template = 'app/person/admin/change_list_with_sort_link.html' + + +admin.site.register(Person, PersonAdmin) diff --git a/sample_project/app/models.py b/sample_project/app/models.py index 26c1b48..41b9417 100644 --- a/sample_project/app/models.py +++ b/sample_project/app/models.py @@ -105,9 +105,7 @@ class Person(Sortable): last_name = models.CharField(max_length=50) is_board_member = models.BooleanField(default=False) + sorting_filters = {'is_board_member': True} + def __unicode__(self): return '{} {}'.format(self.first_name, self.last_name) - - @classmethod - def ordering_subset(cls): - return cls.objects.filter(is_board_member=True) diff --git a/sample_project/database/test_project.sqlite b/sample_project/database/test_project.sqlite index 2cd709f..486daf5 100644 Binary files a/sample_project/database/test_project.sqlite and b/sample_project/database/test_project.sqlite differ diff --git a/sample_project/requirements.txt b/sample_project/requirements.txt index 8e1d2ba..61f1db1 100644 --- a/sample_project/requirements.txt +++ b/sample_project/requirements.txt @@ -1,2 +1,2 @@ -django==1.5.2 +django==1.6.1 south==0.8.1 diff --git a/sample_project/sample_project/settings.py b/sample_project/sample_project/settings.py index 3c2b32e..9280511 100755 --- a/sample_project/sample_project/settings.py +++ b/sample_project/sample_project/settings.py @@ -109,10 +109,7 @@ ROOT_URLCONF = 'sample_project.urls' WSGI_APPLICATION = 'sample_project.wsgi.application' TEMPLATE_DIRS = ( - # Put strings here, like "/home/html/django_templates" or - # "C:/www/django/templates". - # Always use forward slashes, even on Windows. - # Don't forget to use absolute paths, not relative paths. + map_path('templates'), ) INSTALLED_APPS = ( diff --git a/sample_project/templates/app/person/admin/change_list_with_sort_link.html b/sample_project/templates/app/person/admin/change_list_with_sort_link.html new file mode 100644 index 0000000..d406c80 --- /dev/null +++ b/sample_project/templates/app/person/admin/change_list_with_sort_link.html @@ -0,0 +1,9 @@ +{% extends change_list_template_extends %} +{% load i18n %} + +{% block object-tools-items %} +