From 70690a3bae067bbf1e6f20b98790a8cf48722bbc Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Thu, 30 Mar 2017 17:00:19 +0200
Subject: [PATCH] filtering by location added

---
 smash/web/api_views.py                  | 13 ++++++++-----
 smash/web/templates/subjects/index.html | 17 +++++++++++++++--
 2 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/smash/web/api_views.py b/smash/web/api_views.py
index f84dc656..dd85b867 100644
--- a/smash/web/api_views.py
+++ b/smash/web/api_views.py
@@ -17,9 +17,12 @@ def cities(request):
 
 @login_required
 def locations(request):
-    X = Location.objects.values_list('name').distinct()
+    locations = Location.objects.all()
+    data = []
+    for location in locations:
+        data.append({"id": location.id, "name": location.name})
     return JsonResponse({
-        "locations": [x[0] for x in X]
+        "locations": data
     })
 
 
@@ -107,10 +110,10 @@ def get_subjects_filtered(subjects, filters):
             result = result.filter(resigned=(value == "true"))
         elif column == "postponed":
             result = result.filter(postponed=(value == "true"))
+        elif column == "default_location":
+            result = result.filter(default_location=value)
         else:
-            print row
-            # elif order_column == "default_location":
-            #     result = subjects.order_by(order_direction + 'default_location')
+            print "UNKNOWN filter: " + row
 
     return result
 
diff --git a/smash/web/templates/subjects/index.html b/smash/web/templates/subjects/index.html
index f637cd23..581b6cfb 100644
--- a/smash/web/templates/subjects/index.html
+++ b/smash/web/templates/subjects/index.html
@@ -4,6 +4,7 @@
 {% block styles %}
     {{ block.super }}
     <link rel="stylesheet" href="{% static 'AdminLTE/plugins/datatables/dataTables.bootstrap.css' %}">
+
 {% endblock styles %}
 
 {% block ui_active_tab %}'subjects'{% endblock ui_active_tab %}
@@ -55,7 +56,9 @@
                 <th>
                     <div name="string_filter">Surname</div>
                 </th>
-                <th>Default location</th>
+                <th>
+                    <div name="location_filter">---</div>
+                </th>
                 <th>
                     <div name="yes_no_filter">---</div>
                 </th>
@@ -87,10 +90,19 @@
         });
 
         $('#table tfoot div[name="yes_no_filter"]').each(function () {
-            var title = $(this).text();
             $(this).html('<select ><option value selected="selected">----------</option><option value="true">YES</option><option value="false">NO</option></select>');
         });
 
+        $('#table tfoot div[name="location_filter"]').each(function () {
+            var obj = $(this)
+            obj.html('<select ><option value selected="selected">----------</option></select>');
+            $.get("{% url 'web.api.locations' %}", function (data) {
+                $.each(data.locations, function (index, location) {
+                    $('select', obj).append( '<option value="' + location.id + '">' + location.name + '</option>');
+                });
+            });
+        });
+
         $(function () {
             var table = $('#table').DataTable({
                 serverSide: true,
@@ -126,6 +138,7 @@
                     }
                 });
             });
+            $('#table_filter').css("display","none");
         });
     </script>
 
-- 
GitLab