forked from sshwsfc/xadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added sortablelist plugin. It is used to change order of items in lis…
…tview. Merge from @shymonk PR sshwsfc#236
- Loading branch information
Showing
10 changed files
with
148 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
6 changes: 6 additions & 0 deletions
6
xadmin/templates/xadmin/blocks/model_list.top_toolbar.saveorder.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters