Commit f8eb0d16 authored by Nicolas Joyard's avatar Nicolas Joyard
Browse files

Add filters for name, country, chamber using django-filters

parent 0cea299b
# coding: utf-8
from django.utils.text import slugify
from django_filters import FilterSet, MethodFilter, ModelChoiceFilter
from representatives.models import Chamber, Group, Representative
def chamber_filter(qs, value):
return qs.filter(mandates__group__chamber=value)
def group_filter(qs, value):
return qs.filter(mandates__group=value)
class RepresentativeFilter(FilterSet):
search = MethodFilter(action='search_filter')
chamber = ModelChoiceFilter(queryset=Chamber.objects.all(),
action=chamber_filter)
country = ModelChoiceFilter(queryset=Group.objects.filter(kind='country'),
action=group_filter)
class Meta:
model = Representative
fields = ['search', 'chamber', 'country']
def search_filter(self, qs, value):
if len(value) == 0:
return qs
return qs.filter(slug__icontains=slugify(value))
......@@ -11,6 +11,7 @@ from django.views import generic
from representatives.models import Group, Representative
from ..filters import RepresentativeFilter
from .representative_mixin import RepresentativeViewMixin
......@@ -20,10 +21,12 @@ class RepresentativeList(CSVDownloadMixin, GridListMixin, PaginationMixin,
csv_name = 'representatives'
queryset = Representative.objects.select_related('score')
current_filter = None
def get_context_data(self, **kwargs):
c = super(RepresentativeList, self).get_context_data(**kwargs)
c['filter'] = self.current_filter
c['object_list'] = [
self.add_representative_country_and_main_mandate(r)
for r in c['object_list']
......@@ -31,11 +34,10 @@ class RepresentativeList(CSVDownloadMixin, GridListMixin, PaginationMixin,
return c
def search_filter(self, qs):
search = self.request.GET.get('search', None)
if search:
qs = qs.filter(slug__icontains=slugify(search))
return qs
def rep_filter(self, qs):
f = RepresentativeFilter(self.request.GET, queryset=qs)
self.current_filter = f
return f.qs
def group_filter(self, qs):
group_kind = self.kwargs.get('group_kind', None)
......@@ -69,8 +71,7 @@ class RepresentativeList(CSVDownloadMixin, GridListMixin, PaginationMixin,
qs = super(RepresentativeList, self).get_queryset()
if self.get_active_only():
qs = qs.filter(active=True)
qs = self.group_filter(qs)
qs = self.search_filter(qs)
qs = self.rep_filter(qs)
qs = self.prefetch_for_representative_country_and_main_mandate(qs)
return qs
......
......@@ -9,9 +9,7 @@
- block search
%form{action:'', method:'get'}
%label{for:'search'}
- trans 'Search'
%input{id:"search", type:"text", name:"search"}
{{ filter.form.as_p }}
%input{type:"submit", value:"Go"}
%a{href:"?{% if request.GET.search %}search={{ request.GET.search }}&{% endif %}csv"}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment