Skip to content
Snippets Groups Projects
Commit c37bb783 authored by Valentin Groues's avatar Valentin Groues :eyes:
Browse files

reset appointment types links when appointment date changes

parent 6a53c12b
No related branches found
No related tags found
1 merge request!97Resolve "appointment date change"
Pipeline #
......@@ -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': []
......
......@@ -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
......
......@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment