From 840a96cb2221716b9d7a8cca9f01becb35a7063f Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Fri, 13 Oct 2017 12:28:24 +0200
Subject: [PATCH] when appointment is finished, nd number for subject is
 required

---
 .../test_appointments.py}                     | 29 +++++++++++++++----
 smash/web/views/appointment.py                | 12 +++++++-
 2 files changed, 34 insertions(+), 7 deletions(-)
 rename smash/web/tests/{test_view_appointments.py => view/test_appointments.py} (84%)

diff --git a/smash/web/tests/test_view_appointments.py b/smash/web/tests/view/test_appointments.py
similarity index 84%
rename from smash/web/tests/test_view_appointments.py
rename to smash/web/tests/view/test_appointments.py
index 7bb1f511..f3e269fe 100644
--- a/smash/web/tests/test_view_appointments.py
+++ b/smash/web/tests/view/test_appointments.py
@@ -3,12 +3,12 @@ import logging
 
 from django.urls import reverse
 
-from functions import create_subject, create_visit, create_appointment, create_worker, create_flying_team, \
+from web.tests.functions import create_subject, create_visit, create_appointment, create_worker, create_flying_team, \
     format_form_field
 from web.forms import AppointmentEditForm, SubjectEditForm
 from web.models import Appointment, Subject
 from web.views.notifications import get_today_midnight_date
-from . import LoggedInTestCase
+from web.tests import LoggedInTestCase
 
 logger = logging.getLogger(__name__)
 
@@ -84,19 +84,36 @@ class AppointmentsViewTests(LoggedInTestCase):
         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
+        form_data['appointment-status'] = 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):
+    def test_save_appointments_edit_with_invalid_nd_number(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
+
+        response = self.client.post(
+            reverse('web.views.appointment_edit', kwargs={'id': appointment.id}), data=form_data)
+
+        self.assertEqual(response.status_code, 200)
+
+        updated_subject = Subject.objects.get(id=subject.id)
+        self.assertFalse(updated_subject.information_sent)
+
+    def test_save_appointments_edit_with_valid_nd_number(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["subject-nd_number"] = "ND9999"
 
         response = self.client.post(
             reverse('web.views.appointment_edit', kwargs={'id': appointment.id}), data=form_data)
@@ -127,9 +144,9 @@ class AppointmentsViewTests(LoggedInTestCase):
         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
+        form_data["subject-nd_number"] = "ND9999"
 
-        response = self.client.post(
-            reverse('web.views.appointment_edit', kwargs={'id': appointment.id}), data=form_data)
+        self.client.post(reverse('web.views.appointment_edit', kwargs={'id': appointment.id}), data=form_data)
 
         updated_subject = Subject.objects.get(id=subject.id)
         self.assertIsNotNone(updated_subject.flying_team)
diff --git a/smash/web/views/appointment.py b/smash/web/views/appointment.py
index c0462a99..9f4c8a12 100644
--- a/smash/web/views/appointment.py
+++ b/smash/web/views/appointment.py
@@ -1,6 +1,8 @@
 # coding=utf-8
 import logging
+import re
 from django.contrib import messages
+from django.core.exceptions import ValidationError
 from django.shortcuts import get_object_or_404, redirect
 
 from . import wrap_response
@@ -64,7 +66,6 @@ def appointment_edit(request, id):
                                                instance=the_appointment,
                                                user=request.user,
                                                prefix="appointment")
-
         is_valid_form = True
         if the_appointment.visit is not None:
             subject_form = SubjectEditForm(request.POST, instance=the_appointment.visit.subject, prefix="subject")
@@ -74,6 +75,12 @@ def appointment_edit(request, id):
         if not appointment_form.is_valid():
             is_valid_form = False
 
+        if the_appointment.visit is not None:
+            if appointment_form.cleaned_data["status"] == Appointment.APPOINTMENT_STATUS_FINISHED:
+                if re.match('ND[0-9][0-9][0-9][0-9]', subject_form.cleaned_data["nd_number"]) is None:
+                    subject_form.add_error('nd_number', ValidationError("invalid ND number"))
+                    is_valid_form = False
+
         if is_valid_form:
             appointment_form.save()
             if subject_form is not None:
@@ -94,6 +101,9 @@ def appointment_edit(request, id):
                 return redirect('web.views.visit_details', id=the_appointment.visit.id)
             else:
                 return redirect('web.views.appointments')
+        else:
+            messages.add_message(request, messages.ERROR, 'Invalid data. Please fix data and try again.')
+
     else:
         appointment_form = AppointmentEditForm(instance=the_appointment, user=request.user, prefix="appointment")
 
-- 
GitLab