diff --git a/smash/web/models.py b/smash/web/models.py index ba8f9e58062daaa42986546af9ed65a9a9e83dad..f6ee5abf41fd11e50f55e1e2d2af981bf33334ba 100644 --- a/smash/web/models.py +++ b/smash/web/models.py @@ -50,14 +50,33 @@ class Subject(models.Model): ('P','PATIENT'), ) + + def finish_all_visits(self): + visits = Visit.objects.filter(subject = self, is_finished = False) + for visit in visits: + visit.is_finished = True + visit.save() + + def finish_all_appointments(self): + appointments = Appointment.objects.filter(visit__subject = self, status = Appointment.APPOINTMENT_STATUS_SCHEDULED) + for appointment in appointments: + appointment.status = Appointment.APPOINTMENT_STATUS_CANCELLED + appointment.save() + def mark_as_dead(self): self.dead = True self.save() + self.finish_all_visits() + self.finish_all_appointments() + def mark_as_rejected(self): self.resigned = True self.save() + self.finish_all_visits() + self.finish_all_appointments() + sex = models.CharField(max_length=1, choices=SEX_CHOICES, verbose_name='Sex' diff --git a/smash/web/tests/test_model_subject.py b/smash/web/tests/test_model_subject.py new file mode 100644 index 0000000000000000000000000000000000000000..4d9f479794896160bc3596ab91180facace87337 --- /dev/null +++ b/smash/web/tests/test_model_subject.py @@ -0,0 +1,37 @@ +from django.contrib.auth.models import User +from django.test import TestCase, RequestFactory +from django.urls import reverse + +from web.views import * + +from web.models import * + +from web.tests.functions import * + +class SubjectModelTests(TestCase): + + def test_mark_as_dead(self): + subject = create_subject() + visit = create_visit(subject) + appointment = create_appointment(visit) + + subject.mark_as_dead() + appointment_status = Appointment.objects.filter(id=appointment.id)[0].status + visit_finsihed = Visit.objects.filter(id=visit.id)[0].is_finished + + self.assertTrue(subject.dead) + self.assertTrue(visit_finsihed) + self.assertEquals(Appointment.APPOINTMENT_STATUS_CANCELLED, appointment_status) + + def test_mark_as_rejected(self): + subject = create_subject() + visit = create_visit(subject) + appointment = create_appointment(visit) + + subject.mark_as_rejected() + appointment_status = Appointment.objects.filter(id=appointment.id)[0].status + visit_finsihed = Visit.objects.filter(id=visit.id)[0].is_finished + + self.assertTrue(subject.resigned) + self.assertTrue(visit_finsihed) + self.assertEquals(Appointment.APPOINTMENT_STATUS_CANCELLED, appointment_status)