From f8eb0d16fadd25fb921e4639d7636658b21fdcc5 Mon Sep 17 00:00:00 2001
From: Nicolas Joyard <joyard.nicolas@gmail.com>
Date: Sun, 12 Jun 2016 10:25:18 +0200
Subject: [PATCH] Add filters for name, country, chamber using django-filters

---
 memopol/filters.py                            | 36 +++++++++++++++++++
 memopol/views/representative_list.py          | 15 ++++----
 .../representatives/representative_list.haml  |  4 +--
 3 files changed, 45 insertions(+), 10 deletions(-)
 create mode 100644 memopol/filters.py

diff --git a/memopol/filters.py b/memopol/filters.py
new file mode 100644
index 00000000..f8c08d21
--- /dev/null
+++ b/memopol/filters.py
@@ -0,0 +1,36 @@
+# 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))
diff --git a/memopol/views/representative_list.py b/memopol/views/representative_list.py
index be0fc0be..82eda403 100644
--- a/memopol/views/representative_list.py
+++ b/memopol/views/representative_list.py
@@ -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
 
diff --git a/templates/representatives/representative_list.haml b/templates/representatives/representative_list.haml
index 9f75d0e5..39f1789a 100644
--- a/templates/representatives/representative_list.haml
+++ b/templates/representatives/representative_list.haml
@@ -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"}
-- 
GitLab