Skip to content

Commit 42a7d37

Browse files
committed
Merge branch 'release/1.1.1'
2 parents 2f71f1d + 4b774e0 commit 42a7d37

File tree

5 files changed

+70
-49
lines changed

5 files changed

+70
-49
lines changed

adminlteui/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
version = '1.1.0'
1+
version = '1.1.1'
22
# default_app_config = 'adminlteui.apps.AdminlteUIConfig'

adminlteui/templates/admin/change_list.html

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,10 @@ <h4 class="box-title">
106106
<script src="{% static "admin/plugins/datatables/dataTables.bootstrap.min.js" %}"></script>
107107

108108
<script>
109-
var selectors = document.querySelector('#changelist-search').querySelectorAll('select');
110-
for (var i in selectors) {
111-
selectors[i].onchange = function() {
109+
// var selectors = document.querySelector('#changelist-search').querySelectorAll('select');
110+
111+
$.fn.search_filters = function () {
112+
$(this).change(function () {
112113
var $field = $(this);
113114
var $option = $field.find('option:selected');
114115
var select_name = $option.data('name');
@@ -129,12 +130,11 @@ <h4 class="box-title">
129130
// additional = additional.split('=');
130131
// $hidden.attr('name', additional[0]).val(additional[1])
131132
// }
133+
});
134+
$(this).trigger('change');
135+
};
132136

133-
// remove the hidden input of select_name
134-
var input = document.querySelector('#search_group').querySelector('input[name="' + $field.data('name') + '"]');
135-
input.parentNode.removeChild(input)
136-
}
137-
}
137+
$('.search-filter').search_filters();
138138

139139
</script>
140140

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
{% load i18n %}
22

3-
43
<div class="form-group">
5-
<select class="form-control select2 select2-hidden-accessible" style="width: 100%;" tabindex="-1" aria-hidden="true" data-name="{{spec.lookup_kwarg}}">
4+
<select class="form-control select2 select2-hidden-accessible search-filter" style="width: 100%;" tabindex="-1" aria-hidden="true" data-name="{{ field_name }}">
65
<option value="">{{ title }}</option>
7-
<option value="">------</option>
8-
{% for choice in new_choices %}
9-
<option data-name="{{ choice.name }}" value="{{ choice.value }}" {% if choice.selected %} selected {% endif %}>{{ choice.display }}</option>
6+
<option value="">---------</option>
7+
{% for choice in choices %}
8+
{% if choice.name %}
9+
<option data-name="{{ choice.name }}" value="{{ choice.value }}" {% if choice.selected %} selected {% endif %}>
10+
{{ choice.display }}
11+
</option>
12+
{% endif %}
1013
{% endfor %}
1114
</select>
1215
</div>

adminlteui/templates/admin/search_form.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
{% if show_result_count %}
2222
<span class="small quiet">{% blocktrans count counter=cl.result_count %}{{ counter }} result{% plural %}{{ counter }} results{% endblocktrans %} (<a href="?{% if cl.is_popup %}_popup=1{% endif %}">{% if cl.show_full_result_count %}{% blocktrans with full_result_count=cl.full_result_count %}{{ full_result_count }} total{% endblocktrans %}{% else %}{% trans "Show all" %}{% endif %}</a>)</span>
2323
{% endif %}
24-
{% for pair in cl.params.items %}
24+
{% admin_extra_filters cl as extra_filters %}
25+
{% for pair in extra_filters.items %}
2526
{% if pair.0 != search_var %}<input type="hidden" name="{{ pair.0 }}" value="{{ pair.1 }}">{% endif %}
2627
{% endfor %}
2728
</div>

adminlteui/templatetags/adminlte_list.py

Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import itertools
12
from django.contrib.admin.views.main import (
23
ALL_VAR, ORDER_VAR, PAGE_VAR, SEARCH_VAR,
34
)
@@ -35,48 +36,64 @@ def adminlte_paginator_number(cl, i):
3536
)
3637

3738

39+
def get_filter_id(spec):
40+
try:
41+
return getattr(spec, 'field_path')
42+
except AttributeError:
43+
try:
44+
return getattr(spec, 'parameter_name')
45+
except AttributeError:
46+
pass
47+
return spec.title
48+
49+
50+
@register.simple_tag
51+
def admin_extra_filters(cl):
52+
""" Return the dict of used filters which is not included
53+
in list_filters form """
54+
used_parameters = list(itertools.chain(*(s.used_parameters.keys()
55+
for s in cl.filter_specs)))
56+
return dict(
57+
(k, v) for k, v in cl.params.items() if k not in used_parameters)
58+
59+
3860
@register.simple_tag
3961
def adminlte_admin_list_filter(cl, spec):
4062
tpl = get_template(spec.template)
41-
new_choice = []
63+
choices = list(spec.choices(cl))
64+
field_key = get_filter_id(spec)
65+
matched_key = field_key
66+
for choice in choices:
67+
query_string = choice['query_string'][1:]
68+
query_parts = urllib.parse.parse_qs(query_string)
4269

43-
# print(spec.lookup_kwarg)
44-
# print(list(spec.choices(cl)))
70+
value = ''
71+
matches = {}
72+
for key in query_parts.keys():
73+
if key == field_key:
74+
value = query_parts[key][0]
75+
matched_key = key
76+
elif key.startswith(
77+
field_key + '__') or '__' + field_key + '__' in key:
78+
value = query_parts[key][0]
79+
matched_key = key
4580

46-
for choice in list(spec.choices(cl)):
47-
qs = (urllib.parse.parse_qs(choice.get('query_string')))
48-
# print(qs)
49-
for k, v in qs.items():
50-
if k.strip('?') in cl.params.keys() and k.strip(
51-
'?') != spec.lookup_kwarg:
52-
continue
53-
new_choice.append({'name': k.strip('?'), 'value': v[0],
54-
'display': choice.get('display'),
55-
'selected': choice.get('selected')})
81+
if value:
82+
matches[matched_key] = value
5683

57-
# print(new_choice)
84+
# Iterate matches, use first as actual values, additional for hidden
85+
i = 0
86+
for key, value in matches.items():
87+
if i == 0:
88+
choice['name'] = key
89+
choice['value'] = value
90+
# else:
91+
# choice['additional'] = '%s=%s' % (key, value)
92+
i += 1
5893

5994
return tpl.render({
95+
'field_name': field_key,
6096
'title': spec.title,
61-
'choices': list(spec.choices(cl)),
62-
'new_choices': new_choice,
97+
'choices': choices,
6398
'spec': spec,
6499
})
65-
66-
# def adminlte_search_form(cl):
67-
# """
68-
# Display a search form for searching the list.
69-
# """
70-
# print(cl)
71-
# return {
72-
# 'cl': cl,
73-
# 'show_result_count': cl.result_count != cl.full_result_count,
74-
# 'search_var': SEARCH_VAR
75-
# }
76-
#
77-
#
78-
# @register.tag(name='adminlte_search_form')
79-
# def adminlte_search_form_tag(parser, token):
80-
# return InclusionAdminNode(parser, token, func=adminlte_search_form,
81-
# template_name='search_form.html',
82-
# takes_context=False)

0 commit comments

Comments
 (0)