From 06b0bdf0c8aef7058eb1922cf10b9dde34d28628 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Mon, 11 Sep 2017 12:57:41 +0200
Subject: [PATCH] subject contains info about default flying team location

---
 smash/web/models/subject.py               |  6 ++++
 smash/web/tests/functions.py              |  8 ++++-
 smash/web/tests/test_view_appointments.py | 38 +++++++++++++++++------
 smash/web/views/appointment.py            |  2 ++
 4 files changed, 43 insertions(+), 11 deletions(-)

diff --git a/smash/web/models/subject.py b/smash/web/models/subject.py
index b28ef59c..38c74700 100644
--- a/smash/web/models/subject.py
+++ b/smash/web/models/subject.py
@@ -52,6 +52,12 @@ class Subject(models.Model):
     default_location = models.ForeignKey(Location,
                                          verbose_name='Default appointment location',
                                          )
+
+    flying_team = models.ForeignKey("web.FlyingTeam",
+                                    verbose_name='Default flying team location (if applicable)',
+                                    null=True, blank=True
+                                    )
+
     first_name = models.CharField(max_length=50,
                                   verbose_name='First name'
                                   )
diff --git a/smash/web/tests/functions.py b/smash/web/tests/functions.py
index a492a1e6..3f616e35 100644
--- a/smash/web/tests/functions.py
+++ b/smash/web/tests/functions.py
@@ -3,7 +3,8 @@ import datetime
 import os
 from django.contrib.auth.models import User
 
-from web.models import Location, AppointmentType, Subject, Worker, Visit, Appointment, ConfigurationItem, Language
+from web.models import Location, AppointmentType, Subject, Worker, Visit, Appointment, ConfigurationItem, Language, \
+    FlyingTeam
 from web.models.constants import SEX_CHOICES_MALE, SUBJECT_TYPE_CHOICES_CONTROL
 from web.views.notifications import get_today_midnight_date
 
@@ -104,6 +105,11 @@ def create_configuration_item():
     return item
 
 
+def create_flying_team():
+    result = FlyingTeam.objects.create()
+    return result
+
+
 def create_language(name="French", locale="fr_FR"):
     language = Language(name=name, locale=locale)
     language.save()
diff --git a/smash/web/tests/test_view_appointments.py b/smash/web/tests/test_view_appointments.py
index 8caa43d8..5ea5c23a 100644
--- a/smash/web/tests/test_view_appointments.py
+++ b/smash/web/tests/test_view_appointments.py
@@ -2,7 +2,7 @@ import datetime
 
 from django.urls import reverse
 
-from functions import create_subject, create_visit, create_appointment, create_worker
+from functions import create_subject, create_visit, create_appointment, create_worker, create_flying_team
 from web.forms import AppointmentEditForm, SubjectEditForm
 from web.models import Appointment, Subject
 from web.views.notifications import get_today_midnight_date
@@ -10,6 +10,10 @@ from . import LoggedInTestCase
 
 
 class AppointmentsViewTests(LoggedInTestCase):
+    def setUp(self):
+        super(AppointmentsViewTests, self).setUp()
+        create_worker(self.user, True)
+
     def test_appointments_list_request(self):
         response = self.client.get(reverse('web.views.appointments'))
         self.assertEqual(response.status_code, 200)
@@ -18,7 +22,6 @@ class AppointmentsViewTests(LoggedInTestCase):
         subject = create_subject()
         visit = create_visit(subject)
         appointment = create_appointment(visit, when=datetime.datetime.now())
-        create_worker(self.user, True)
         new_comment = 'new comment'
         new_status = appointment.APPOINTMENT_STATUS_NO_SHOW
         new_last_name = "new last name"
@@ -44,8 +47,6 @@ class AppointmentsViewTests(LoggedInTestCase):
         self.assertEqual(new_last_name, updated_subject.last_name)
 
     def test_appointments_edit_without_visit(self):
-        create_worker(self.user)
-
         appointment = create_appointment()
         appointment.visit = None
         appointment.save()
@@ -55,8 +56,6 @@ class AppointmentsViewTests(LoggedInTestCase):
         self.assertEqual(response.status_code, 200)
 
     def test_save_appointments_edit_without_visit(self):
-        create_worker(self.user)
-
         appointment = create_appointment()
         appointment.visit = None
         appointment.save()
@@ -73,10 +72,21 @@ class AppointmentsViewTests(LoggedInTestCase):
 
     def test_save_appointments_edit(self):
         subject = create_subject()
-        create_worker(self.user, True)
         visit = create_visit(subject)
         appointment = create_appointment(visit, get_today_midnight_date())
 
+        form_data = self.prepare_form(appointment, subject)
+        form_data["appointment-status"] = Appointment.APPOINTMENT_STATUS_FINISHED
+
+        response = self.client.post(
+            reverse('web.views.appointment_edit', kwargs={'id': appointment.id}), data=form_data)
+
+        self.assertEqual(response.status_code, 302)
+
+        updated_subject = Subject.objects.get(id=subject.id)
+        self.assertTrue(updated_subject.information_sent)
+
+    def prepare_form(self, appointment, subject):
         form_appointment = AppointmentEditForm(user=self.user, instance=appointment, prefix="appointment")
         form_subject = SubjectEditForm(instance=subject, prefix="subject")
         form_data = {}
@@ -90,12 +100,20 @@ class AppointmentsViewTests(LoggedInTestCase):
                 form_data['subject-{}'.format(key)] = value.strftime('%Y-%m-%d %H:%M')
             elif value is not None:
                 form_data['subject-{}'.format(key)] = value
+        return form_data
+
+    def test_subject_flying_team_location(self):
+        subject = create_subject()
+        visit = create_visit(subject)
+        appointment = create_appointment(visit, get_today_midnight_date())
+
+        form_data = self.prepare_form(appointment, subject)
         form_data["appointment-status"] = Appointment.APPOINTMENT_STATUS_FINISHED
+        form_data["appointment-flying_team"] = create_flying_team().id
+        form_data['appointment-status'] = Appointment.APPOINTMENT_STATUS_FINISHED
 
         response = self.client.post(
             reverse('web.views.appointment_edit', kwargs={'id': appointment.id}), data=form_data)
 
-        self.assertEqual(response.status_code, 302)
-
         updated_subject = Subject.objects.get(id=subject.id)
-        self.assertTrue(updated_subject.information_sent)
+        self.assertIsNotNone(updated_subject.flying_team)
diff --git a/smash/web/views/appointment.py b/smash/web/views/appointment.py
index b4712eb7..a13e149e 100644
--- a/smash/web/views/appointment.py
+++ b/smash/web/views/appointment.py
@@ -81,6 +81,8 @@ def appointment_edit(request, id):
             if the_appointment.status == Appointment.APPOINTMENT_STATUS_FINISHED:
                 subject = Subject.objects.get(id=the_appointment.visit.subject.id)
                 subject.information_sent = True
+                if the_appointment.flying_team is not None and subject.flying_team is None:
+                    subject.flying_team = the_appointment.flying_team
                 subject.save()
 
             messages.success(request, "Modifications saved")
-- 
GitLab