From 4abd837b631fd0225a9169495739e1460cce115b Mon Sep 17 00:00:00 2001
From: "piotr.atyjaszyk" <piotrmk1@gmail.com>
Date: Fri, 3 Feb 2017 14:03:58 +0100
Subject: [PATCH] After finishing last assignment, visits will now be marked as
 finished

---
 smash/web/models.py                   | 12 ++++++++++++
 smash/web/templates/visits/index.html |  4 ++--
 smash/web/views.py                    |  6 ++++++
 3 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/smash/web/models.py b/smash/web/models.py
index d877da37..c073baa1 100644
--- a/smash/web/models.py
+++ b/smash/web/models.py
@@ -158,6 +158,18 @@ class Visit(models.Model):
     def __str__(self):
         return "%s %s" % (self.subject.first_name, self.subject.last_name)
 
+    def checkIfFinished(self):
+        appvis = self.appointment_set.all()
+        finished = True
+
+        for app in appvis:
+            if(app.is_finished == False):
+                finished = False
+
+        if (finished):
+            self.is_finished = True
+            self.save()
+
 
 class Item (models.Model):
     is_fixed = models.BooleanField(
diff --git a/smash/web/templates/visits/index.html b/smash/web/templates/visits/index.html
index e60d1d8c..a851274e 100644
--- a/smash/web/templates/visits/index.html
+++ b/smash/web/templates/visits/index.html
@@ -62,8 +62,8 @@
 		        		{{ visit.get_visit_type_display }}
 		        	</td>
 		        	<td>
-                {% if visit.is_finished %}<button type="button" class="btn btn-block btn-danger">YES</button>
-                {% else %}<button type="button" class="btn btn-block btn-success">NO</button>
+                {% if visit.is_finished %}<button type="button" class="btn btn-block btn-success">YES</button>
+                {% else %}<button type="button" class="btn btn-block btn-danger">NO</button>
                 {% endif %}
 		        	</td>
 
diff --git a/smash/web/views.py b/smash/web/views.py
index 454d0f61..75661c9e 100644
--- a/smash/web/views.py
+++ b/smash/web/views.py
@@ -304,6 +304,12 @@ def assignment_edit(request, id):
 		form = AppointmentEditForm(request.POST, request.FILES, instance=Appointment.objects.get(id=id))
 		if form.is_valid():
 			form.save()
+
+			data = form.cleaned_data
+			vis = data['visit']
+			visit = Visit.objects.get(id=vis.id)
+			visit.checkIfFinished()
+
 			return redirect(assignments)
 	else:
 		form = AppointmentEditForm(instance=Appointment.objects.get(id=id))
-- 
GitLab