From dd0a52004372c5592a6cecee4969ae188106e424 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Thu, 12 Oct 2017 15:16:41 +0200
Subject: [PATCH] error fixed when saving and finishing appointment without
 visit

---
 smash/web/tests/test_view_appointments.py | 19 +++++++++++++++++++
 smash/web/views/appointment.py            |  2 +-
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/smash/web/tests/test_view_appointments.py b/smash/web/tests/test_view_appointments.py
index bf721ebe..7bb1f511 100644
--- a/smash/web/tests/test_view_appointments.py
+++ b/smash/web/tests/test_view_appointments.py
@@ -1,4 +1,5 @@
 import datetime
+import logging
 
 from django.urls import reverse
 
@@ -9,6 +10,8 @@ from web.models import Appointment, Subject
 from web.views.notifications import get_today_midnight_date
 from . import LoggedInTestCase
 
+logger = logging.getLogger(__name__)
+
 
 class AppointmentsViewTests(LoggedInTestCase):
     def setUp(self):
@@ -71,6 +74,22 @@ class AppointmentsViewTests(LoggedInTestCase):
 
         self.assertEqual(response.status_code, 200)
 
+    def test_save_as_finished_appointments_edit_without_visit(self):
+        appointment = create_appointment(None, get_today_midnight_date())
+        appointment.visit = None
+        appointment.save()
+
+        form_appointment = AppointmentEditForm(user=self.user, instance=appointment, prefix="appointment")
+        form_data = {}
+        for key, value in form_appointment.initial.items():
+            if value is not None:
+                form_data['appointment-{}'.format(key)] = format_form_field(value)
+        form_data['appointment-status'.format(key)] = Appointment.APPOINTMENT_STATUS_FINISHED
+        self.client.post(reverse('web.views.appointment_edit', kwargs={'id': appointment.id}), data=form_data)
+
+        appointment_result = Appointment.objects.filter(id=appointment.id)[0]
+        self.assertEqual(Appointment.APPOINTMENT_STATUS_FINISHED, appointment_result.status)
+
     def test_save_appointments_edit(self):
         subject = create_subject()
         visit = create_visit(subject)
diff --git a/smash/web/views/appointment.py b/smash/web/views/appointment.py
index 5f0e6d7d..c0462a99 100644
--- a/smash/web/views/appointment.py
+++ b/smash/web/views/appointment.py
@@ -80,7 +80,7 @@ def appointment_edit(request, id):
                 subject_form.save()
             the_appointment = get_object_or_404(Appointment, id=id)
 
-            if the_appointment.status == Appointment.APPOINTMENT_STATUS_FINISHED:
+            if the_appointment.status == Appointment.APPOINTMENT_STATUS_FINISHED and the_appointment.visit is not None:
                 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:
-- 
GitLab