From be5f3537c9e071082843e9f884a1868b87a6a843 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Thu, 14 Sep 2017 12:52:58 +0200
Subject: [PATCH] possibility of ignoring missing subjects

---
 smash/web/api_urls.py                         | 10 +++++--
 smash/web/api_views/redcap.py                 | 26 +++++++++++++++++
 smash/web/static/css/redcap.css               |  3 ++
 smash/web/static/js/redcap.js                 | 22 ++++++++++++++
 .../redcap/inconsistent_subjects.html         |  4 +--
 .../templates/redcap/missing_subjects.html    | 29 +++++++++++++------
 smash/web/views/notifications.py              | 11 ++++---
 7 files changed, 88 insertions(+), 17 deletions(-)
 create mode 100644 smash/web/api_views/redcap.py
 create mode 100644 smash/web/static/css/redcap.css
 create mode 100644 smash/web/static/js/redcap.js

diff --git a/smash/web/api_urls.py b/smash/web/api_urls.py
index aafb4491..99f0b927 100644
--- a/smash/web/api_urls.py
+++ b/smash/web/api_urls.py
@@ -15,7 +15,8 @@ Including another URLconf
 """
 from django.conf.urls import url
 
-from web.api_views import worker, location, subject, appointment_type, appointment, configuration, daily_planning
+from web.api_views import worker, location, subject, appointment_type, appointment, configuration, daily_planning, \
+    redcap
 
 urlpatterns = [
     # appointments
@@ -39,7 +40,6 @@ urlpatterns = [
     # locations
     url(r'^locations$', location.locations, name='web.api.locations'),
 
-
     # worker data
     url(r'^specializations$', worker.specializations, name='web.api.specializations'),
     url(r'^units$', worker.units, name='web.api.units'),
@@ -50,4 +50,10 @@ urlpatterns = [
     url(r'^availabilities/(?P<date>\d{4}-\d{2}-\d{2})/$', daily_planning.availabilities, name='web.api.availabilities'),
     url(r'^events_persist$', daily_planning.events_persist, name='web.api.events_persist'),
 
+    # worker data
+    url(r'^redcap/missing_subjects/(?P<missing_subject_id>\d+):ignore$', redcap.ignore_missing_subject,
+        name='web.api.redcap.ignore_missing_subject'),
+    url(r'^redcap/missing_subjects/(?P<missing_subject_id>\d+):unignore$', redcap.unignore_missing_subject,
+        name='web.api.redcap.unignore_missing_subject'),
+
 ]
diff --git a/smash/web/api_views/redcap.py b/smash/web/api_views/redcap.py
new file mode 100644
index 00000000..677e4334
--- /dev/null
+++ b/smash/web/api_views/redcap.py
@@ -0,0 +1,26 @@
+from django.contrib.auth.decorators import login_required
+from django.http import JsonResponse
+
+from web.models import MissingSubject
+
+
+@login_required
+def ignore_missing_subject(request, missing_subject_id):
+    missing_subjects = MissingSubject.objects.filter(id=missing_subject_id)
+    for missing_subject in missing_subjects:
+        missing_subject.ignore = True
+        missing_subject.save()
+    return JsonResponse({
+        "status": "ok"
+    })
+
+
+@login_required
+def unignore_missing_subject(request, missing_subject_id):
+    missing_subjects = MissingSubject.objects.filter(id=missing_subject_id)
+    for missing_subject in missing_subjects:
+        missing_subject.ignore = False
+        missing_subject.save()
+    return JsonResponse({
+        "status": "ok"
+    })
diff --git a/smash/web/static/css/redcap.css b/smash/web/static/css/redcap.css
new file mode 100644
index 00000000..a5fe9a1f
--- /dev/null
+++ b/smash/web/static/css/redcap.css
@@ -0,0 +1,3 @@
+.ignore-row {
+    background-color: orange;
+}
diff --git a/smash/web/static/js/redcap.js b/smash/web/static/js/redcap.js
new file mode 100644
index 00000000..4cdb49ae
--- /dev/null
+++ b/smash/web/static/js/redcap.js
@@ -0,0 +1,22 @@
+function ignore_missing_subject(id, url) {
+    $.ajax({
+        url: url,
+        success: function () {
+            $("#ignore_" + id).hide()
+            $("#unignore_" + id).show()
+            $("#row_" + id).find('td').addClass("ignore-row");
+        }
+    });
+}
+
+function unignore_missing_subject(id, url) {
+
+    $.ajax({
+        url: url,
+        success: function () {
+            $("#unignore_" + id).hide()
+            $("#ignore_" + id).show()
+            $("#row_" + id).find('td').removeClass("ignore-row");
+        }
+    });
+}
diff --git a/smash/web/templates/redcap/inconsistent_subjects.html b/smash/web/templates/redcap/inconsistent_subjects.html
index e436c11e..459dd31e 100644
--- a/smash/web/templates/redcap/inconsistent_subjects.html
+++ b/smash/web/templates/redcap/inconsistent_subjects.html
@@ -30,10 +30,10 @@
             {% for inconsistent_subject in inconsistent_subjects %}
                 <tr>
                     <td>
-                        <a href="{% url 'web.views.subject_edit' inconsistent_subject.subject.id %}">{{ inconsistent_subject.subject }}</a>
+                        <a class="btn btn-block btn-default" href="{% url 'web.views.subject_edit' inconsistent_subject.subject.id %}">{{ inconsistent_subject.subject }}</a>
                     </td>
                     <td>
-                        <a href="{{ inconsistent_subject.redcap_url }}"
+                        <a class="btn btn-block btn-default" href="{{ inconsistent_subject.redcap_url }}"
                            target="_redcap">{{ inconsistent_subject.subject.nd_number }}</a>
                     </td>
                     <td>
diff --git a/smash/web/templates/redcap/missing_subjects.html b/smash/web/templates/redcap/missing_subjects.html
index 855df575..a3f9a6c8 100644
--- a/smash/web/templates/redcap/missing_subjects.html
+++ b/smash/web/templates/redcap/missing_subjects.html
@@ -5,6 +5,7 @@
     {{ block.super }}
     <!-- DataTables -->
     <link rel="stylesheet" href="{% static 'AdminLTE/plugins/datatables/dataTables.bootstrap.css' %}">
+    <link rel="stylesheet" href="{% static 'css/redcap.css' %}">
 {% endblock styles %}
 
 {% block ui_active_tab %}'subjects'{% endblock ui_active_tab %}
@@ -29,19 +30,28 @@
             </thead>
             <tbody>
             {% for missing_subject in missing_subjects %}
-                <tr>
-                    <td>{% if missing_subject.subject %}
+                <tr id="row_{{ missing_subject.id }}">
+                    <td {% if missing_subject.ignore %}class="ignore-row"{% endif %}>{% if missing_subject.subject %}
                         {{ missing_subject.subject.nd_number }} {% elif missing_subject.redcap_id %}
                         {{ missing_subject.redcap_id }} {% endif %}</td>
-                    <td>{% if missing_subject.subject %}
-                        <a href="{% url 'web.views.subject_edit' missing_subject.subject.id %}">{{ missing_subject.subject }}</a>
+                    <td {% if missing_subject.ignore %}class="ignore-row"{% endif %}>{% if missing_subject.subject %}
+                        <a class="btn btn-block btn-default"
+                           href="{% url 'web.views.subject_edit' missing_subject.subject.id %}">{{ missing_subject.subject }}</a>
                     {% endif %}
                     </td>
-                    <td>{% if missing_subject.redcap_id %}
-                        <a href="{{ missing_subject.redcap_url }}" target="_redcap">{{ missing_subject.redcap_id }}</a> {% endif %}
+                    <td {% if missing_subject.ignore %}class="ignore-row"{% endif %}>{% if missing_subject.redcap_id %}
+                        <a class="btn btn-block btn-default" href="{{ missing_subject.redcap_url }}"
+                           target="_redcap">{{ missing_subject.redcap_id }}</a> {% endif %}
                     </td>
-                    <td>
-                        {% if missing_subject.ignore %}YES{% else %}NO{% endif %}
+                    <td {% if missing_subject.ignore %}class="ignore-row"{% endif %}>
+                        <a class="btn btn-block btn-warning" id="ignore_{{ missing_subject.id }}"
+                           onclick="ignore_missing_subject({{ missing_subject.id }}, '{% url 'web.api.redcap.ignore_missing_subject' missing_subject.id %}')"
+                           {% if missing_subject.ignore %}style="display:none"{% endif %}>IGNORE
+                        </a>
+                        <a class="btn btn-block btn-success" id="unignore_{{ missing_subject.id }}"
+                           onclick="unignore_missing_subject({{ missing_subject.id }}, '{% url 'web.api.redcap.unignore_missing_subject' missing_subject.id %}')"
+                           {% if not missing_subject.ignore %}style="display:none"{% endif %}>UN-IGNORE
+                        </a>
                     </td>
                 </tr>
             {% endfor %}
@@ -56,13 +66,14 @@
 
     <script src="{% static 'AdminLTE/plugins/datatables/jquery.dataTables.min.js' %}"></script>
     <script src="{% static 'AdminLTE/plugins/datatables/dataTables.bootstrap.min.js' %}"></script>
+    <script src="{% static 'js/redcap.js' %}"></script>
     <script>
         $(function () {
             $('#table').DataTable({
                 "paging": true,
                 "lengthChange": false,
                 "searching": true,
-                "ordering": true,
+                "ordering": false,
                 "info": true,
                 "autoWidth": false
             });
diff --git a/smash/web/views/notifications.py b/smash/web/views/notifications.py
index 8dd49b02..42cbc3e4 100644
--- a/smash/web/views/notifications.py
+++ b/smash/web/views/notifications.py
@@ -33,8 +33,8 @@ def get_exceeded_visit_notifications_count(user):
 def get_missing_redcap_subject_notification_count(user):
     notification = NotificationCount(
         title="missing RED Cap subject",
-        count=get_missing_redcap_subjects(user).count(),
-        style="fa fa-user text-yellow",
+        count=get_missing_redcap_subjects(user, False).count(),
+        style="fa fa-user text-red",
         type='web.views.missing_redcap_subject')
     return notification
 
@@ -204,8 +204,11 @@ def get_exceeded_visits(user):
                                 ).order_by('datetime_begin')
 
 
-def get_missing_redcap_subjects(user):
-    return MissingSubject.objects.all().order_by("ignore")
+def get_missing_redcap_subjects(user, include_ignored=True):
+    if include_ignored:
+        return MissingSubject.objects.order_by("ignore")
+    else:
+        return MissingSubject.objects.filter(ignore=False)
 
 
 def get_inconsistent_redcap_subjects(user):
-- 
GitLab