From 38d78e4bc74714e03525d8b3dd43b02447c8da3b Mon Sep 17 00:00:00 2001
From: Todd Dembrey <todd.dembrey@torchbox.com>
Date: Thu, 15 Feb 2018 10:09:38 +0000
Subject: [PATCH] Add Filters to the dashboard and add round filter

---
 opentech/apply/dashboard/tables.py                |  8 ++++++++
 .../dashboard/templates/dashboard/dashboard.html  |  8 +++++++-
 opentech/apply/dashboard/views.py                 | 15 ++++++---------
 opentech/settings/base.py                         |  1 +
 requirements.txt                                  |  1 +
 5 files changed, 23 insertions(+), 10 deletions(-)

diff --git a/opentech/apply/dashboard/tables.py b/opentech/apply/dashboard/tables.py
index 3ae000d46..d98eed033 100644
--- a/opentech/apply/dashboard/tables.py
+++ b/opentech/apply/dashboard/tables.py
@@ -1,4 +1,6 @@
+import django_filters as filters
 import django_tables2 as tables
+
 from opentech.apply.funds.models import ApplicationSubmission
 
 
@@ -15,3 +17,9 @@ class DashboardTable(tables.Table):
 
     def render_user(self, value):
         return value.get_full_name()
+
+
+class SubmissionFilter(filters.FilterSet):
+    class Meta:
+        model = ApplicationSubmission
+        fields = ('round',)
diff --git a/opentech/apply/dashboard/templates/dashboard/dashboard.html b/opentech/apply/dashboard/templates/dashboard/dashboard.html
index ee2f7fc26..08ae36336 100644
--- a/opentech/apply/dashboard/templates/dashboard/dashboard.html
+++ b/opentech/apply/dashboard/templates/dashboard/dashboard.html
@@ -15,6 +15,12 @@
 </div>
 
 <div class="wrapper wrapper--medium wrapper--top-bottom-inner-space">
-    {% render_table object_list %}
+    {% if filter %}
+        <form action="" method="get" class="form form-inline">
+            {{ filter.form.as_p }}
+            <button>Filter</button>
+        </form>
+    {% endif %}
+    {% render_table table %}
 </div>
 {% endblock %}
diff --git a/opentech/apply/dashboard/views.py b/opentech/apply/dashboard/views.py
index b86e281c4..0fa61f17c 100644
--- a/opentech/apply/dashboard/views.py
+++ b/opentech/apply/dashboard/views.py
@@ -1,17 +1,14 @@
-from django.views.generic import ListView
-from django_tables2 import RequestConfig
+from django_filters.views import FilterView
+from django_tables2.views import SingleTableMixin
 
 from opentech.apply.funds.models import ApplicationSubmission
 
-from .tables import DashboardTable
+from .tables import DashboardTable, SubmissionFilter
 
 
-class DashboardView(ListView):
+class DashboardView(SingleTableMixin, FilterView):
     model = ApplicationSubmission
     template_name = 'dashboard/dashboard.html'
+    table_class = DashboardTable
 
-    def get_context_data(self, **kwargs):
-        context = super().get_context_data(**kwargs)
-        context['object_list'] = DashboardTable(context['object_list'])
-        RequestConfig(self.request).configure(context['object_list'])
-        return context
+    filterset_class = SubmissionFilter
diff --git a/opentech/settings/base.py b/opentech/settings/base.py
index 063ce2e71..8fca9323f 100644
--- a/opentech/settings/base.py
+++ b/opentech/settings/base.py
@@ -57,6 +57,7 @@ INSTALLED_APPS = [
     'tinymce',
     'wagtailcaptcha',
     'django_tables2',
+    'django_filters',
     'addressfield',
 
     'django.contrib.admin',
diff --git a/requirements.txt b/requirements.txt
index 3289a9504..b8b5004d5 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -19,6 +19,7 @@ flake8
 
 social_auth_app_django==2.1.0
 django-tables2==1.17.1
+django-filter==1.1.0
 
 # Production dependencies
 dj-database-url==0.4.1
-- 
GitLab