Skip to content

Commit

Permalink
Added MultiSelectFieldListFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
Jairus Martin committed Mar 11, 2014
1 parent f067e92 commit 7b4ea1f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion demo_app/app/adminx.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def open_web(self, instance):

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

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

Expand Down
33 changes: 33 additions & 0 deletions xadmin/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,39 @@ def choices(self):
'display': EMPTY_CHANGELIST_VALUE,
}

@manager.register
class MultiSelectFieldListFilter(ListFieldFilter):
""" Delegates the filter to the default filter and ors the results of each
Lists the distinct values of each field as a checkbox
Uses the default spec for each
"""
template = 'xadmin/filters/checklist.html'
lookup_formats = {'in': '%s__in'}

@classmethod
def test(cls, field, request, params, model, admin_view, field_path):
return True

def __init__(self, field, request, params, model, model_admin, field_path):
super(MultiSelectFieldListFilter,self).__init__(field, request, params, model, model_admin, field_path)
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)

def choices(self):
self.lookup_in_val = (type(self.lookup_in_val) in (tuple,list)) and self.lookup_in_val or list(self.lookup_in_val)
yield {
'selected': len(self.lookup_in_val) == 0,
'query_string': self.query_string({},[self.lookup_in_name]),
'display': _('All'),
}
for val in self.lookup_choices:
yield {
'selected': smart_unicode(val) in self.lookup_in_val,
'query_string': self.query_string({self.lookup_in_name: ",".join([val]+self.lookup_in_val),}),
'remove_query_string': self.query_string({self.lookup_in_name: ",".join([v for v in self.lookup_in_val if v != val]),}),
'display': val,
}

@manager.register
class AllValuesFieldListFilter(ListFieldFilter):
Expand Down
14 changes: 14 additions & 0 deletions xadmin/templates/xadmin/filters/checklist.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% load i18n %}
<li class="dropdown-submenu">
<a><i class="icon-filter {% if spec.is_used %}text-success{%else%}text-muted{% endif %}"></i> {{ title }}</a>
<ul class="dropdown-menu">
{% for choice in choices %}
<li{% if choice.selected %} class="active"{% endif %}>
<a href="{% if choice.selected %}{{ choice.remove_query_string|iriencode }}{% else %}{{ choice.query_string|iriencode }}{% endif %}">
<input type="checkbox" {% if choice.selected %} checked="checked"{% endif %}>
{{ choice.display }}
</a>
</li>
{% endfor %}
</ul>
</li>

0 comments on commit 7b4ea1f

Please sign in to comment.