Commit 6f0867e6 authored by Nicolas Joyard's avatar Nicolas Joyard

Add group filtering with select2 ac

parent f8eb0d16
# coding: utf-8
from dal.autocomplete import ModelSelect2
import datetime
from django.db.models import Q
from django.utils.text import slugify
from django_filters import FilterSet, MethodFilter, ModelChoiceFilter
......@@ -8,11 +13,19 @@ from representatives.models import Chamber, Group, Representative
def chamber_filter(qs, value):
return qs.filter(mandates__group__chamber=value)
today = datetime.date.today()
return qs.filter(
Q(mandates__end_date__gte=today) | Q(mandates__end_date__isnull=True),
mandates__group__chamber=value
)
def group_filter(qs, value):
return qs.filter(mandates__group=value)
today = datetime.date.today()
return qs.filter(
Q(mandates__end_date__gte=today) | Q(mandates__end_date__isnull=True),
mandates__group=value
)
class RepresentativeFilter(FilterSet):
......@@ -25,6 +38,12 @@ class RepresentativeFilter(FilterSet):
country = ModelChoiceFilter(queryset=Group.objects.filter(kind='country'),
action=group_filter)
group = ModelChoiceFilter(queryset=Group.objects.exclude(
kind__in=['chamber', 'country']),
action=group_filter,
widget=ModelSelect2(url='group-autocomplete'),
label='Party, committee or delegation')
class Meta:
model = Representative
fields = ['search', 'chamber', 'country']
......
......@@ -6,6 +6,7 @@ from django.views import generic
from views.dossier_ac import DossierAutocomplete, ProposalAutocomplete
from views.dossier_detail import DossierDetail
from views.dossier_list import DossierList
from views.group_ac import GroupAutocomplete
from views.group_list import GroupList
from views.representative_detail import RepresentativeDetail
from views.representative_list import RepresentativeList
......@@ -58,6 +59,11 @@ urlpatterns = [
RedirectGroupList.as_view(),
name='group-list-redirect'
),
url(
r'^legislature/autocomplete/group/$',
GroupAutocomplete.as_view(),
name='group-autocomplete',
),
url(
r'^votes/dossier/$',
DossierList.as_view(),
......
# coding: utf-8
from dal.autocomplete import Select2QuerySetView
from django.db.models import Q
from representatives.models import Group
class GroupAutocomplete(Select2QuerySetView):
def get_queryset(self):
qs = Group.objects.exclude(kind__in=['chamber', 'country'])
if self.q:
qs = qs.filter(
Q(name__icontains=self.q) |
Q(abbreviation__icontains=self.q)
)
return qs
......@@ -39,34 +39,6 @@ class RepresentativeList(CSVDownloadMixin, GridListMixin, PaginationMixin,
self.current_filter = f
return f.qs
def group_filter(self, qs):
group_kind = self.kwargs.get('group_kind', None)
chamber = self.kwargs.get('chamber', None)
group = self.kwargs.get('group', None)
today = datetime.date.today()
if group_kind and group:
if group.isnumeric():
group_qs = Group.objects.filter(
id=int(group)
)
else:
group_qs = Group.objects.filter(
name=group,
kind=group_kind
)
if chamber:
group_qs = group_qs.filter(chamber__name=chamber)
qs = qs.filter(
models.Q(mandates__end_date__gte=today) |
models.Q(mandates__end_date__isnull=True),
mandates__group__in=group_qs
)
return qs
def get_queryset(self):
qs = super(RepresentativeList, self).get_queryset()
if self.get_active_only():
......
......@@ -8,7 +8,9 @@
- block search
%form{action:'', method:'get'}
{{ filter.form.media }}
%form.filter-form{action:'', method:'get'}
{{ filter.form.as_p }}
%input{type:"submit", value:"Go"}
......
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