add support for django admin filters
parent
a90449a8a5
commit
f135ad906d
|
|
@ -109,14 +109,21 @@ class SortableAdmin(SortableAdminBase, ModelAdmin):
|
|||
"""
|
||||
# get sort group index from querystring if present
|
||||
sort_filter_index = request.GET.get('sort_filter')
|
||||
filter_expression = request.GET.get('filter_expression')
|
||||
|
||||
filters = {}
|
||||
|
||||
if sort_filter_index:
|
||||
try:
|
||||
filters = self.model.sorting_filters[int(sort_filter_index)][1]
|
||||
except (IndexError, ValueError):
|
||||
pass
|
||||
|
||||
if filter_expression:
|
||||
filters.update(
|
||||
dict([filter_expression.split('=')])
|
||||
)
|
||||
|
||||
# Apply any sort filters to create a subset of sortable objects
|
||||
return self.get_queryset(request).filter(**filters)
|
||||
|
||||
|
|
|
|||
|
|
@ -29,6 +29,23 @@
|
|||
<script src="{% static 'adminsortable/js/jquery.ui.touch-punch.min.js' %}"></script>
|
||||
{% include 'adminsortable/csrf/jquery.django-csrf.html' with csrf_cookie_name=csrf_cookie_name %}
|
||||
<script src="{% static 'adminsortable/js/admin.sortable.js' %}"></script>
|
||||
|
||||
<script type="text/javascript">
|
||||
(function($) {
|
||||
$(document).ready(function($) {
|
||||
var url = window.location.href;
|
||||
var urlParts = url.split('?');
|
||||
var cnangeListUrl = $('a#return-to-changelist').attr('href')
|
||||
|
||||
if (urlParts.length === 2) {
|
||||
$('a#return-to-changelist').attr(
|
||||
'href',
|
||||
cnangeListUrl + '?' + urlParts[1].replace('filter_expression=', '')
|
||||
);
|
||||
}
|
||||
});
|
||||
})(django.jQuery);
|
||||
</script>
|
||||
{% endblock %}
|
||||
|
||||
{% block bodyclass %}{{ block.super }} app-{{ opts.app_label }} model-{{ opts.model_name }} change-list{% endblock %}
|
||||
|
|
@ -77,7 +94,7 @@
|
|||
{% block object-tools %}
|
||||
<ul class="object-tools">
|
||||
<li>
|
||||
<a href="{% url opts|admin_urlname:'changelist' %}">
|
||||
<a id="return-to-changelist" href="{% url opts|admin_urlname:'changelist' %}">
|
||||
{% blocktrans with opts.verbose_name_plural|capfirst as model %}Return to {{ model }}{% endblocktrans %}
|
||||
</a>
|
||||
</li>
|
||||
|
|
|
|||
|
|
@ -1,6 +1,23 @@
|
|||
{% extends change_list_template_extends %}
|
||||
{% load i18n %}
|
||||
|
||||
{% block extrahead %}
|
||||
{{ block.super }}
|
||||
<script type="text/javascript">
|
||||
(function($) {
|
||||
$(document).ready(function($) {
|
||||
var url = window.location.href;
|
||||
var urlParts = url.split('?');
|
||||
|
||||
if (urlParts.length === 2) {
|
||||
$('a#change-order').attr('href', './sort/?filter_expression=' + urlParts[1]);
|
||||
}
|
||||
});
|
||||
})(django.jQuery);
|
||||
</script>
|
||||
{% endblock %}
|
||||
|
||||
|
||||
{% block object-tools-items %}
|
||||
{% for sorting_filter in sorting_filters %}
|
||||
<li>
|
||||
|
|
@ -8,7 +25,7 @@
|
|||
</li>
|
||||
{% empty %}
|
||||
<li>
|
||||
<a href="./sort/">{% trans 'Change Order' %}</a>
|
||||
<a id="change-order" href="./sort/">{% trans 'Change Order' %}</a>
|
||||
</li>
|
||||
{% endfor %}
|
||||
{{ block.super }}
|
||||
|
|
|
|||
Loading…
Reference in New Issue