From 1485cb219e59f895529fc00bcf1ed323902d9fec Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Wed, 14 Feb 2018 13:47:34 +0100
Subject: [PATCH] appointment list includes type and nd number

---
 smash/web/api_views/appointment.py              | 17 ++++++++++++++++-
 .../0105_unfinished_appointments_list_update.py | 17 +++++++++++++++++
 2 files changed, 33 insertions(+), 1 deletion(-)
 create mode 100644 smash/web/migrations/0105_unfinished_appointments_list_update.py

diff --git a/smash/web/api_views/appointment.py b/smash/web/api_views/appointment.py
index 13dd3259..cdc4e8e4 100644
--- a/smash/web/api_views/appointment.py
+++ b/smash/web/api_views/appointment.py
@@ -7,7 +7,7 @@ from django.utils import timezone
 
 from web.api_views.serialization_utils import serialize_datetime, location_to_str, flying_team_to_str, add_column, \
     bool_to_yes_no, get_filters_for_data_table_request
-from web.models import Appointment, Study, SubjectColumns, AppointmentColumns, AppointmentList
+from web.models import Appointment, Study, SubjectColumns, AppointmentColumns, AppointmentList, StudyColumns
 from web.models.appointment_list import APPOINTMENT_LIST_GENERIC, APPOINTMENT_LIST_UNFINISHED, \
     APPOINTMENT_LIST_APPROACHING
 from web.models.constants import GLOBAL_STUDY_ID
@@ -22,14 +22,18 @@ def get_appointment_columns(request, appointment_list_type):
     if len(appointment_lists) > 0:
         appointment_list = appointment_lists[0]
         subject_columns = appointment_list.visible_subject_columns
+        subject_study_columns = appointment_list.visible_study_subject_columns
         appointment_columns = appointment_list.visible_appointment_columns
     else:
         subject_columns = SubjectColumns()
+        subject_study_columns = StudyColumns()
         appointment_columns = AppointmentColumns()
 
     result = []
     add_column(result, "First name", "first_name", subject_columns, "string_filter")
     add_column(result, "Last name", "last_name", subject_columns, "string_filter")
+    add_column(result, "ND number", "nd_number", subject_study_columns, "string_filter")
+    add_column(result, "Type", "type", subject_study_columns, "type_filter")
     add_column(result, "Info sent", "post_mail_sent", appointment_columns, "yes_no_filter")
     add_column(result, "Date", "datetime_when", appointment_columns, None)
     add_column(result, "Appointment types", "appointment_types", appointment_columns, "appointment_type_filter",
@@ -73,6 +77,10 @@ def get_appointments_order(appointments_to_be_ordered, order_column, order_direc
         result = appointments_to_be_ordered.order_by(order_direction + 'visit__subject__subject__first_name')
     elif order_column == "last_name":
         result = appointments_to_be_ordered.order_by(order_direction + 'visit__subject__subject__last_name')
+    elif order_column == "nd_number":
+        result = appointments_to_be_ordered.order_by(order_direction + 'visit__subject__nd_number')
+    elif order_column == "type":
+        result = appointments_to_be_ordered.order_by(order_direction + 'visit__subject__type')
     elif order_column == "location":
         result = appointments_to_be_ordered.order_by(order_direction + 'location')
     elif order_column == "flying_team":
@@ -95,6 +103,10 @@ def get_appointments_filtered(appointments_to_be_filtered, filters):
             result = result.filter(visit__subject__subject__first_name__icontains=value)
         elif column == "last_name":
             result = result.filter(visit__subject__subject__last_name__icontains=value)
+        elif column == "nd_number":
+            result = result.filter(visit__subject__nd_number__icontains=value)
+        elif column == "type":
+            result = result.filter(visit__subject__type=value)
         elif column == "location":
             result = result.filter(location=value)
         elif column == "flying_team":
@@ -159,6 +171,7 @@ def serialize_appointment(appointment):
     subject_string = ""
     first_name = ""
     last_name = ""
+    subject_type = ""
     nd_number = screening_number = phone_numbers = appointment_type_names = None
     if appointment.visit is not None:
         title = "Visit " + str(appointment.visit.visit_number)
@@ -168,6 +181,7 @@ def serialize_appointment(appointment):
         last_name = study_subject.subject.last_name
         nd_number = study_subject.nd_number
         screening_number = study_subject.screening_number
+        subject_type = study_subject.get_type_display()
         phone_numbers = ", ".join(filter(None,
                                          [study_subject.subject.phone_number, study_subject.subject.phone_number_2,
                                           study_subject.subject.phone_number_3]))
@@ -188,6 +202,7 @@ def serialize_appointment(appointment):
         "title": title,
         "nd_number": nd_number,
         "screening_number": screening_number,
+        "type": subject_type,
         "phone_number": phone_numbers,
         "appointment_type_names": appointment_type_names,
         "datetime_until": until,
diff --git a/smash/web/migrations/0105_unfinished_appointments_list_update.py b/smash/web/migrations/0105_unfinished_appointments_list_update.py
new file mode 100644
index 00000000..7bd87a66
--- /dev/null
+++ b/smash/web/migrations/0105_unfinished_appointments_list_update.py
@@ -0,0 +1,17 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.10.7 on 2018-02-14 10:26
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+    dependencies = [
+        ('web', '0104_contact_required_list_update'),
+    ]
+
+    operations = [
+        migrations.RunSQL(
+            'UPDATE web_studycolumns SET nd_number=TRUE, type=TRUE WHERE id IN ' +
+            '(SELECT visible_study_subject_columns_id FROM web_appointmentlist WHERE type = \'UNFINISHED\');'),
+    ]
-- 
GitLab