From 12a6a117590589f469ba196af435dfe65e283ddb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Valentin=20Grou=C3=A8s?= <valentin.groues@uni.lu>
Date: Tue, 25 Apr 2017 14:37:34 +0200
Subject: [PATCH] subjects list sorted by nd number and filtered by worker's
 location - fixes #104

---
 smash/web/templates/subjects/index.html | 17 +++++++++++++----
 smash/web/views/subject.py              |  3 ++-
 2 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/smash/web/templates/subjects/index.html b/smash/web/templates/subjects/index.html
index 918ea485..10cd9639 100644
--- a/smash/web/templates/subjects/index.html
+++ b/smash/web/templates/subjects/index.html
@@ -88,6 +88,7 @@
     <script src="{% static 'AdminLTE/plugins/datatables/dataTables.bootstrap.min.js' %}"></script>
 
     <script>
+        var table;
         $('#table tfoot div[name="string_filter"]').each(function () {
             var title = $(this).text();
             $(this).html('<input type="text" style="width:80px" placeholder="' + title + '" />');
@@ -100,17 +101,21 @@
         $('#table tfoot div[name="location_filter"]').each(function () {
             var obj = $(this)
             obj.html('<select style="width:80px"><option value selected="selected">---</option></select>');
+            var select = $('select', obj);
             $.get("{% url 'web.api.locations' %}", function (data) {
                 $.each(data.locations, function (index, location) {
-                    $('select', obj).append('<option value="' + location.id + '">' + location.name + '</option>');
+                    select.append('<option value="' + location.id + '">' + location.name + '</option>');
                 });
+                {% if worker.locations.all.count == 1 %}
+                    select.val({{  worker.locations.first.id}});
+                {% endif %}
             });
+
         });
 
         $(function () {
-            var table = $('#table').DataTable({
+            table = $('#table').DataTable({
                 pageLength: 25,
-                stateSave: true,
                 serverSide: true,
                 processing: true,
                 responsive: true,
@@ -131,8 +136,12 @@
                     "targets": 9,
                     "data": "id",
                     "defaultContent": '<a href="#" type="button" class="btn btn-block btn-default">Edit</a>'
-                }]
+                }],
+                order: [[0, 'desc']]
             });
+            {% if worker.locations.all.count == 1 %}
+                table.columns(4).search({{  worker.locations.first.id}}).draw();
+            {% endif %}
 
             $('#table tbody').on('click', 'a', function () {
                 var data = table.row($(this).parents('tr')).data();
diff --git a/smash/web/views/subject.py b/smash/web/views/subject.py
index 7c7ca07d..494dca01 100644
--- a/smash/web/views/subject.py
+++ b/smash/web/views/subject.py
@@ -4,7 +4,7 @@ from django.shortcuts import redirect, get_object_or_404
 
 from . import wrap_response
 from ..forms import SubjectAddForm, SubjectEditForm, VisitDetailForm
-from ..models import Subject, MailTemplate
+from ..models import Subject, MailTemplate, Worker
 
 SUBJECT_LIST_GENERIC = "GENERIC"
 SUBJECT_LIST_NO_VISIT = "NO_VISIT"
@@ -14,6 +14,7 @@ SUBJECT_LIST_REQUIRE_CONTACT = "REQUIRE_CONTACT"
 def subjects(request):
     context = {
         'list_type': SUBJECT_LIST_GENERIC,
+        'worker': Worker.get_by_user(request.user)
     }
     return wrap_response(request, 'subjects/index.html', context)
 
-- 
GitLab