Merge pull request #8 from return1/master
made django-admin-sortable work on models with descending ordermaster
commit
e8e20f0f48
|
|
@ -108,7 +108,6 @@ class SortableAdmin(ModelAdmin):
|
||||||
context = {
|
context = {
|
||||||
'title' : 'Drag and drop %s to change display order' % capfirst(verbose_name_plural),
|
'title' : 'Drag and drop %s to change display order' % capfirst(verbose_name_plural),
|
||||||
'opts' : opts,
|
'opts' : opts,
|
||||||
'root_path' : '/%s' % admin_site.root_path,
|
|
||||||
'app_label' : opts.app_label,
|
'app_label' : opts.app_label,
|
||||||
'has_perm' : has_perm,
|
'has_perm' : has_perm,
|
||||||
'objects' : objects,
|
'objects' : objects,
|
||||||
|
|
@ -149,14 +148,20 @@ class SortableAdmin(ModelAdmin):
|
||||||
indexes = map(str, request.POST.get('indexes', []).split(','))
|
indexes = map(str, request.POST.get('indexes', []).split(','))
|
||||||
klass = ContentType.objects.get(id=model_type_id).model_class()
|
klass = ContentType.objects.get(id=model_type_id).model_class()
|
||||||
objects_dict = dict([(str(obj.pk), obj) for obj in klass.objects.filter(pk__in=indexes)])
|
objects_dict = dict([(str(obj.pk), obj) for obj in klass.objects.filter(pk__in=indexes)])
|
||||||
lowest_ordered_object = min(objects_dict.values(), key=lambda x: getattr(x, 'order'))
|
if '-order' in klass._meta.ordering: #desc order
|
||||||
min_index = getattr(lowest_ordered_object, 'order') or 0
|
start_object = max(objects_dict.values(), key=lambda x: getattr(x, 'order'))
|
||||||
|
start_index = getattr(start_object, 'order') or len(indexes)
|
||||||
|
step = -1
|
||||||
|
else: #'order' is default, asc order
|
||||||
|
start_object = min(objects_dict.values(), key=lambda x: getattr(x, 'order'))
|
||||||
|
start_index = getattr(start_object, 'order') or 0
|
||||||
|
step = 1
|
||||||
|
|
||||||
for index in indexes:
|
for index in indexes:
|
||||||
obj = objects_dict.get(index)
|
obj = objects_dict.get(index)
|
||||||
setattr(obj, 'order', min_index)
|
setattr(obj, 'order', start_index)
|
||||||
obj.save()
|
obj.save()
|
||||||
min_index += 1
|
start_index += step
|
||||||
|
|
||||||
response = {'objects_sorted' : True}
|
response = {'objects_sorted' : True}
|
||||||
except (Key, IndexError, klass.DoesNotExist, AttributeError):
|
except (Key, IndexError, klass.DoesNotExist, AttributeError):
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ msgstr ""
|
||||||
#, python-format
|
#, python-format
|
||||||
msgid ""
|
msgid ""
|
||||||
"You may also drag and drop %(sortable_by_class_display_name)s to change "
|
"You may also drag and drop %(sortable_by_class_display_name)s to change "
|
||||||
"thier order."
|
"their order."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: templates/adminsortable/change_list.html:50
|
#: templates/adminsortable/change_list.html:50
|
||||||
|
|
|
||||||
Binary file not shown.
|
|
@ -25,7 +25,7 @@ msgstr "Sleep %(model)s om de volgorde te veranderen"
|
||||||
|
|
||||||
#: templates/adminsortable/change_list.html:39
|
#: templates/adminsortable/change_list.html:39
|
||||||
#, python-format
|
#, python-format
|
||||||
msgid "You may also drag and drop %(sortable_by_class_display_name)s to change thier order."
|
msgid "You may also drag and drop %(sortable_by_class_display_name)s to change their order."
|
||||||
msgstr "U kunt ook %(sortable_by_class_display_name)s slepen om de volgorde te veranderen."
|
msgstr "U kunt ook %(sortable_by_class_display_name)s slepen om de volgorde te veranderen."
|
||||||
|
|
||||||
#: templates/adminsortable/change_list.html:50
|
#: templates/adminsortable/change_list.html:50
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,6 @@
|
||||||
|
|
||||||
{% block extrastyle %}
|
{% block extrastyle %}
|
||||||
{{ block.super }}
|
{{ block.super }}
|
||||||
<link rel="stylesheet" type="text/css" href="{% admin_media_prefix %}css/forms.css" />
|
|
||||||
{% if has_sortable_tabular_inlines %}
|
{% if has_sortable_tabular_inlines %}
|
||||||
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}adminsortable/css/admin.sortable.tabular.inline.css" />
|
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}adminsortable/css/admin.sortable.tabular.inline.css" />
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@
|
||||||
|
|
||||||
{% block extrastyle %}
|
{% block extrastyle %}
|
||||||
{{ block.super }}
|
{{ block.super }}
|
||||||
<link rel="stylesheet" type="text/css" href="{% admin_media_prefix %}css/changelists.css" />
|
|
||||||
<link rel="stylesheet" href="{{ STATIC_URL }}adminsortable/css/admin.sortable.css" />
|
<link rel="stylesheet" href="{{ STATIC_URL }}adminsortable/css/admin.sortable.css" />
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
|
|
@ -36,7 +35,7 @@
|
||||||
</h1>
|
</h1>
|
||||||
{% if sortable_by_class.is_sortable %}
|
{% if sortable_by_class.is_sortable %}
|
||||||
<p>
|
<p>
|
||||||
{% blocktrans %}You may also drag and drop {{ sortable_by_class_display_name }} to change thier order.{% endblocktrans %}
|
{% blocktrans %}You may also drag and drop {{ sortable_by_class_display_name }} to change their order.{% endblocktrans %}
|
||||||
</p>
|
</p>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue