diff --git a/smash/web/api_views/daily_planning.py b/smash/web/api_views/daily_planning.py index 116b021e4fae2189287eaa7964b34d289dbdac7e..bd739c5c5569aabffc3ed9b05c9164e3cd9d5c07 100644 --- a/smash/web/api_views/daily_planning.py +++ b/smash/web/api_views/daily_planning.py @@ -7,8 +7,8 @@ from django.contrib.auth.decorators import login_required from django.http import JsonResponse from django.shortcuts import get_object_or_404 -from web.views import e500_error from web.models import Appointment, AppointmentTypeLink, Worker, Availability, Holiday +from web.views import e500_error from web.views.notifications import get_filter_locations logger = logging.getLogger(__name__) @@ -202,7 +202,7 @@ def get_generic_appointment_events(request, date): appointment_data = { 'name': "GENERAL", 'id': appointment.id, - 'color': RANDOM_COLORS[len(RANDOM_COLORS)-1], + 'color': RANDOM_COLORS[len(RANDOM_COLORS) - 1], 'start': "", 'location': str(appointment.location), 'events': [] diff --git a/smash/web/forms.py b/smash/web/forms.py index 657306be52cc1b95579e8fc59c288ba75bad35ab..571fd786a5506d785f7aa1107a727ca23cbc88fd 100644 --- a/smash/web/forms.py +++ b/smash/web/forms.py @@ -6,7 +6,7 @@ from django.forms import ModelForm, Form from django.utils.dates import MONTHS from web.models import Subject, Worker, Appointment, Visit, AppointmentType, ContactAttempt, AppointmentTypeLink, \ - Availability, Holiday, Country + Availability, Holiday from web.models.constants import SUBJECT_TYPE_CHOICES, SCREENING_NUMBER_PREFIXES_FOR_TYPE, COUNTRY_OTHER_ID from web.views.notifications import get_filter_locations @@ -254,7 +254,12 @@ class AppointmentEditForm(ModelForm): def save(self, commit=True): appointment = super(AppointmentEditForm, self).save(commit) - appointment_type_links = AppointmentTypeLink.objects.filter(appointment=appointment).all() + # if appointment date change, remove appointment_type links + if 'datetime_when' in self.changed_data: + AppointmentTypeLink.objects.filter(appointment=appointment).delete() + appointment_type_links = [] + else: + appointment_type_links = AppointmentTypeLink.objects.filter(appointment=appointment).all() appointment_types_from_links = [] appointment_types = self.cleaned_data['appointment_types'] for appointment_type_link in appointment_type_links: @@ -266,7 +271,8 @@ class AppointmentEditForm(ModelForm): for appointment_type in appointment_types: if appointment_type not in appointment_types_from_links: # we create new appointment links for appointments types that have been added - appointment_type_link = AppointmentTypeLink(appointment=appointment, appointment_type=appointment_type) + appointment_type_link = AppointmentTypeLink(appointment=appointment, + appointment_type=appointment_type) appointment_type_link.save() return appointment diff --git a/smash/web/tests/forms/test_AppointmentEditForm.py b/smash/web/tests/forms/test_AppointmentEditForm.py index 0a60144efbc9916be9c6eee7f3719a2f1fa937f7..f62fe60937dcb10e313d273f93e3a908619d1b88 100644 --- a/smash/web/tests/forms/test_AppointmentEditForm.py +++ b/smash/web/tests/forms/test_AppointmentEditForm.py @@ -2,8 +2,8 @@ from django.test import TestCase from web.forms import AppointmentAddForm from web.forms import AppointmentEditForm -from web.models import Appointment, Worker -from web.tests.functions import get_test_location, create_user, create_visit, create_location +from web.models import Appointment, Worker, AppointmentTypeLink +from web.tests.functions import get_test_location, create_user, create_visit, create_location, create_appointment_type class AppointmentEditFormTests(TestCase): @@ -11,9 +11,9 @@ class AppointmentEditFormTests(TestCase): location = get_test_location() self.user = create_user() - worker = Worker.get_by_user(self.user) - worker.locations = [get_test_location()] - worker.save() + self.worker = Worker.get_by_user(self.user) + self.worker.locations = [get_test_location()] + self.worker.save() self.visit = create_visit() @@ -27,6 +27,66 @@ class AppointmentEditFormTests(TestCase): add_form = AppointmentAddForm(user=self.user, data=self.sample_data) add_form.instance.visit_id = self.visit.id self.appointment = add_form.save() + self.appointment_type = create_appointment_type() + + def test_appointment_types_links_add_type(self): + self.assertFalse(AppointmentTypeLink.objects.filter(appointment=self.appointment).all()) + sample_data = self.sample_data.copy() + sample_data['appointment_types'] = [self.appointment_type.id] + form = AppointmentEditForm(user=self.user, instance=self.appointment, data=sample_data) + self.assertTrue(form.is_valid()) + form.save() + self.assertEqual(1, AppointmentTypeLink.objects.filter(appointment=self.appointment).count(), + "one appointment link should have been created") + + def test_appointment_types_links_removed_after_date_changed(self): + # create first link + self.test_appointment_types_links_add_type() + # modify link + link = AppointmentTypeLink.objects.filter(appointment=self.appointment).first() + self.assertIsNone(link.worker) + link.worker = self.worker + link.save() + self.assertEqual(self.worker, link.worker) + # change date + sample_data = self.sample_data.copy() + sample_data['appointment_types'] = [self.appointment_type.id] + sample_data['datetime_when'] = '2021-01-01' + form = AppointmentEditForm(user=self.user, instance=self.appointment, data=sample_data) + self.assertTrue(form.is_valid()) + form.save() + # check that the appointment links have been deleted and recreated + links_count = AppointmentTypeLink.objects.filter(appointment=self.appointment).count() + self.assertEqual(1, links_count, + "only one appointment link should exist, {} found".format(links_count)) + new_link = AppointmentTypeLink.objects.filter(appointment=self.appointment).first() + self.assertNotEqual(link, new_link) + self.assertIsNone(new_link.worker) + + def test_appointment_types_links_kept_if_no_date_changed(self): + # create first link + self.test_appointment_types_links_add_type() + # modify link + link = AppointmentTypeLink.objects.filter(appointment=self.appointment).first() + self.assertIsNone(link.worker) + link.worker = self.worker + link.save() + self.assertEqual(self.worker, link.worker) + # change length + sample_data = self.sample_data.copy() + sample_data['length'] = '100' + sample_data['appointment_types'] = [self.appointment_type.id] + form = AppointmentEditForm(user=self.user, instance=self.appointment, data=sample_data) + self.assertTrue(form.is_valid()) + self.appointment = form.save() + self.assertEqual(100, self.appointment.length) + # check that the appointment links have been kept + links_count = AppointmentTypeLink.objects.filter(appointment=self.appointment).count() + self.assertEqual(1, links_count, + "only one appointment link should exist, {} found".format(links_count)) + new_link = AppointmentTypeLink.objects.filter(appointment=self.appointment).first() + self.assertEqual(link.id, new_link.id) + self.assertEqual(self.worker, new_link.worker) def test_validation(self): form = AppointmentEditForm(user=self.user, data=self.sample_data_with_status)