Skip to content

Commit 527057e

Browse files
committed
Merge pull request sshwsfc#139 from frmdstryr/master
Added MultiSelectFieldListFilter
2 parents 7e3b14d + 7b4ea1f commit 527057e

3 files changed

Lines changed: 48 additions & 1 deletion

File tree

demo_app/app/adminx.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def open_web(self, instance):
7575

7676
search_fields = ['name', 'ip', 'description']
7777
list_filter = ['idc', 'guarantee_date', 'status', 'brand', 'model',
78-
'cpu', 'core_num', 'hard_disk', 'memory', 'service_type']
78+
'cpu', 'core_num', 'hard_disk', 'memory', ('service_type',xadmin.filters.MultiSelectFieldListFilter)]
7979

8080
list_bookmarks = [{'title': "Need Guarantee", 'query': {'status__exact': 2}, 'order': ('-guarantee_date',), 'cols': ('brand', 'guarantee_date', 'service_type')}]
8181

xadmin/filters.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,39 @@ def choices(self):
419419
'display': EMPTY_CHANGELIST_VALUE,
420420
}
421421

422+
@manager.register
423+
class MultiSelectFieldListFilter(ListFieldFilter):
424+
""" Delegates the filter to the default filter and ors the results of each
425+
426+
Lists the distinct values of each field as a checkbox
427+
Uses the default spec for each
428+
429+
"""
430+
template = 'xadmin/filters/checklist.html'
431+
lookup_formats = {'in': '%s__in'}
432+
433+
@classmethod
434+
def test(cls, field, request, params, model, admin_view, field_path):
435+
return True
436+
437+
def __init__(self, field, request, params, model, model_admin, field_path):
438+
super(MultiSelectFieldListFilter,self).__init__(field, request, params, model, model_admin, field_path)
439+
self.lookup_choices = [x[0] for x in self.admin_view.queryset().order_by(field_path).values_list(field_path).distinct().exclude(**{"%s__isnull"%field_path:True}) if str(x[0]).strip()!=""]#field.get_choices(include_blank=False)
440+
441+
def choices(self):
442+
self.lookup_in_val = (type(self.lookup_in_val) in (tuple,list)) and self.lookup_in_val or list(self.lookup_in_val)
443+
yield {
444+
'selected': len(self.lookup_in_val) == 0,
445+
'query_string': self.query_string({},[self.lookup_in_name]),
446+
'display': _('All'),
447+
}
448+
for val in self.lookup_choices:
449+
yield {
450+
'selected': smart_unicode(val) in self.lookup_in_val,
451+
'query_string': self.query_string({self.lookup_in_name: ",".join([val]+self.lookup_in_val),}),
452+
'remove_query_string': self.query_string({self.lookup_in_name: ",".join([v for v in self.lookup_in_val if v != val]),}),
453+
'display': val,
454+
}
422455

423456
@manager.register
424457
class AllValuesFieldListFilter(ListFieldFilter):
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{% load i18n %}
2+
<li class="dropdown-submenu">
3+
<a><i class="icon-filter {% if spec.is_used %}text-success{%else%}text-muted{% endif %}"></i> {{ title }}</a>
4+
<ul class="dropdown-menu">
5+
{% for choice in choices %}
6+
<li{% if choice.selected %} class="active"{% endif %}>
7+
<a href="{% if choice.selected %}{{ choice.remove_query_string|iriencode }}{% else %}{{ choice.query_string|iriencode }}{% endif %}">
8+
<input type="checkbox" {% if choice.selected %} checked="checked"{% endif %}>
9+
{{ choice.display }}
10+
</a>
11+
</li>
12+
{% endfor %}
13+
</ul>
14+
</li>

0 commit comments

Comments
 (0)