Merge branch 'order-subset'

This commit is contained in:
Brandon Taylor
2014-02-05 11:13:58 -05:00
12 changed files with 184 additions and 23 deletions
+9 -9
View File
@@ -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,7 +37,11 @@ class SortableAdminBase(object):
object_tools block to take people to the view to change the sorting.
"""
if get_is_sortable(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(self.filtered_objects):
self.change_list_template = \
self.sortable_change_list_with_sort_link_template
self.is_sortable = True
@@ -61,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'
@@ -102,7 +106,7 @@ class SortableAdmin(SortableAdminBase, ModelAdmin):
has_perm = request.user.has_perm('{0}.{1}'.format(opts.app_label,
opts.get_change_permission()))
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.
@@ -161,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)
@@ -200,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':
+2 -1
View File
@@ -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
@@ -53,7 +54,7 @@ class Sortable(models.Model):
sortable_foreign_keys.append(field)
if len(sortable_foreign_keys) > 1:
raise MultipleSortableForeignKeyException(
u'%s may only have one SortableForeignKey' % self)
u'{} may only have one SortableForeignKey'.format(self))
def save(self, *args, **kwargs):
if not self.id:
@@ -9,11 +9,11 @@
{% block extrahead %}
{{ block.super }}
<script type="text/javascript" src="{% static 'admin/js/jquery.js' %}"></script>
<script type="text/javascript" src="{% static 'admin/js/jquery.init.js' %}"></script>
<script type="text/javascript" src="{% static 'adminsortable/js/jquery-ui-django-admin.min.js' %}"></script>
<script type="text/javascript" src="{% static 'adminsortable/js/jquery.django-csrf.js' %}"></script>
<script type="text/javascript" src="{% static 'adminsortable/js/admin.sortable.js' %}"></script>
<script src="{% static 'admin/js/jquery.js' %}"></script>
<script src="{% static 'admin/js/jquery.init.js' %}"></script>
<script src="{% static 'adminsortable/js/jquery-ui-django-admin.min.js' %}"></script>
<script src="{% static 'adminsortable/js/jquery.django-csrf.js' %}"></script>
<script src="{% static 'adminsortable/js/admin.sortable.js' %}"></script>
{% 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 %}
+3 -2
View File
@@ -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