Skip to content

Commit

Permalink
Added sortablelist plugin. It is used to change order of items in lis…
Browse files Browse the repository at this point in the history
…tview.

Merge from @shymonk PR sshwsfc#236
  • Loading branch information
sshwsfc committed Jul 8, 2016
1 parent 3b7e409 commit a12f04c
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 56 deletions.
4 changes: 4 additions & 0 deletions xadmin/locale/en/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,10 @@ msgstr ""
msgid "Every %(t)s seconds"
msgstr ""

#: templates/xadmin/blocks/model_list.top_toolbar.saveorder.html:4
msgid "Save Order"
msgstr ""

#: templates/xadmin/edit_inline/blank.html:5 views/detail.py:22
#: views/edit.py:100 views/list.py:28
msgid "Null"
Expand Down
Binary file modified xadmin/locale/zh_Hans/LC_MESSAGES/django.mo
Binary file not shown.
4 changes: 4 additions & 0 deletions xadmin/locale/zh_Hans/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,10 @@ msgstr "清除自动刷新"
msgid "Every %(t)s seconds"
msgstr "每 %(t)s 秒"

#: templates/xadmin/blocks/model_list.top_toolbar.saveorder.html:4
msgid "Save Order"
msgstr "保存排序"

#: templates/xadmin/edit_inline/blank.html:5 views/detail.py:22
#: views/edit.py:100 views/list.py:28
msgid "Null"
Expand Down
6 changes: 3 additions & 3 deletions xadmin/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
'bookmark',
'export',
'layout',
'refresh',
'sortable',
'refresh',
'details',
'editable',
'relate',
Expand All @@ -27,7 +26,8 @@
'passwords',
'sitemenu',
'language',
'quickfilter'
'quickfilter',
'sortablelist'
)


Expand Down
36 changes: 0 additions & 36 deletions xadmin/plugins/sortable.py

This file was deleted.

81 changes: 81 additions & 0 deletions xadmin/plugins/sortablelist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# coding: utf-8
"""
Make items sortable by drag-drop in list view. Diffierent from
builtin plugin sortable, it touches model field indeed intead
of only for display.
"""

from __future__ import unicode_literals

from django.template.loader import render_to_string
from django.core.urlresolvers import reverse
from django.db import transaction

from xadmin.views import (
BaseAdminPlugin, ModelAdminView, ListAdminView
)
from xadmin.sites import site
from xadmin.views.base import csrf_protect_m


class SortableListPlugin(BaseAdminPlugin):

list_order_field = None

def init_request(self, *args, **kwargs):
return bool(self.list_order_field)

@property
def is_list_sortable(self):
return True

def result_row(self, __, obj):
row = __()
row.update({
"tagattrs": "order-key=order_{}".format(obj.pk)
})
return row

def result_item(self, item, obj, field_name, row):
if self.is_list_sortable and field_name == self.list_order_field:
item.btns.append('<a><i class="fa fa-arrows"></i></a>')
return item

def get_context(self, context):
context['save_order_url'] = self.get_model_url(self.admin_view.model, 'save_order')
return context

def block_top_toolbar(self, context, nodes):
save_node = render_to_string(
'xadmin/blocks/model_list.top_toolbar.saveorder.html', context_instance=context
)
nodes.append(save_node)

def get_media(self, media):
if self.is_list_sortable:
media = media + self.vendor('xadmin.plugin.sortablelist.js')
return media


class SaveOrderView(ModelAdminView):

@csrf_protect_m
@transaction.atomic
def post(self, request):
order_objs = request.POST.getlist("order[]")
for order_value, pk in enumerate(order_objs, start=1):
self.save_order(pk, order_value)
return self.render_response({})

def save_order(self, pk, order_value):
obj = self.model.objects.get(pk=pk)
order_field = self.list_order_field
is_order_changed = lambda x: getattr(x, order_field) != order_value

if is_order_changed(obj):
setattr(obj, order_field, order_value)
obj.save()


site.register_plugin(SortableListPlugin, ListAdminView)
site.register_modelview(r'^save-order/$', SaveOrderView, name='%s_%s_save_order')
16 changes: 0 additions & 16 deletions xadmin/static/xadmin/js/xadmin.plugin.sortable.js

This file was deleted.

49 changes: 49 additions & 0 deletions xadmin/static/xadmin/js/xadmin.plugin.sortablelist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
(function($) {
$(function() {
$.ajaxSetup({
beforeSend: function(xhr, settings) {
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
// Only send the token to relative URLs i.e. locally.
xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
}
}
});

$(".results table tbody").sortable({
axis: 'y',
items: 'tr',
cursor: 'move',
opacity: 0.8,
update: function(event, ui) {
var $rows = $(this);
$("#save-order").on("click", function(e) {
$.ajax({
url: $(this).attr('post-url'),
method: 'POST',
data: $rows.sortable('serialize', {
attribute: 'order-key',
expression: (/(.+)_(.+)/),
})
});
location.reload();
}).show();
}
});
});

})(jQuery);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% load i18n %}
<div class="btn-group">
<a href="#" id="save-order" class="btn btn-primary btn-sm" style="display: None" post-url="{{ save_order_url }}">
{% trans "Save Order" %}
</a>
</div>
2 changes: 1 addition & 1 deletion xadmin/templates/xadmin/views/model_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
{% block results_grid_body %}
<tbody>
{% for row in results %}
<tr class="grid-item{% if row.css_class %} {{row.css_class}}{%endif%}">{% for o in row.cells %}
<tr class="grid-item{% if row.css_class %} {{row.css_class}}{%endif%}" {{ row.tagattrs }}>{% for o in row.cells %}
<td {{o.tagattrs}}>
{% if o.btns %}
<div class="btn-group pull-right">
Expand Down

0 comments on commit a12f04c

Please sign in to comment.