From e6635c633bf249607a592826342a971b4e2b01bc Mon Sep 17 00:00:00 2001
From: Benjamin Bertrand <benjamin.bertrand@esss.se>
Date: Tue, 3 Jul 2018 22:18:17 +0200
Subject: [PATCH] Add checkbox to display all tasks (admin only)

---
 app/static/js/tasks.js        |  8 +++++++-
 app/task/views.py             |  6 ++++--
 app/templates/task/tasks.html | 11 +++++++++++
 3 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/app/static/js/tasks.js b/app/static/js/tasks.js
index 0aeb32e..5d1730c 100644
--- a/app/static/js/tasks.js
+++ b/app/static/js/tasks.js
@@ -1,9 +1,15 @@
 $(document).ready(function() {
 
+  $("#allTasks").on('change', function() {
+    // reload the data from the Ajax source
+    tasks_table.ajax.reload();
+  });
+
   var tasks_table =  $("#tasks_table").DataTable({
     "ajax": function(data, callback, settings) {
+      var allTasks = $('#allTasks').prop("checked");
       $.getJSON(
-        $SCRIPT_ROOT + "/task/_retrieve_tasks",
+        $SCRIPT_ROOT + "/task/_retrieve_tasks?all=" + allTasks,
         function(json) {
           callback(json);
         });
diff --git a/app/task/views.py b/app/task/views.py
index 399785a..4642213 100644
--- a/app/task/views.py
+++ b/app/task/views.py
@@ -9,7 +9,8 @@ This module implements the task blueprint.
 :license: BSD 2-Clause, see LICENSE for more details.
 
 """
-from flask import Blueprint, render_template, jsonify
+from flask import (Blueprint, render_template, jsonify,
+                   request)
 from flask_login import login_required, current_user
 from .. import models
 
@@ -32,5 +33,6 @@ def view_task(id_):
 @bp.route('/_retrieve_tasks')
 @login_required
 def retrieve_tasks():
-    data = [task.to_dict() for task in current_user.get_tasks()]
+    all = request.args.get('all', 'false') == 'true'
+    data = [task.to_dict() for task in current_user.get_tasks(all=all)]
     return jsonify(data=data)
diff --git a/app/templates/task/tasks.html b/app/templates/task/tasks.html
index 799be14..2c0c6f8 100644
--- a/app/templates/task/tasks.html
+++ b/app/templates/task/tasks.html
@@ -15,6 +15,17 @@
   <br>
 
   {% block tasks_main %}
+  {% if current_user.is_authenticated and current_user.is_admin %}
+    <div class="form-check">
+      <input class="form-check-input" type="checkbox" value="" id="allTasks">
+      <label class="form-check-label" for="allTasks">
+        Show all tasks (all users)
+      </label>
+    </div>
+
+    <hr class="separator">
+  {%- endif %}
+
   <table id="tasks_table" class="table table-bordered table-hover table-sm" cellspacing="0" width="100%">
     <thead>
       <tr>
-- 
GitLab