From b16ca88e63daf4a2b012c674fb730ace63b29409 Mon Sep 17 00:00:00 2001 From: Piotr Gawron <piotr.gawron@uni.lu> Date: Thu, 7 Dec 2017 10:37:04 +0100 Subject: [PATCH] when first appointment in first visit is finished then visit dates are adjusted to reflect start of the study --- smash/web/tests/functions.py | 3 ++ smash/web/tests/view/test_appointments.py | 41 +++++++++++++++++++++-- smash/web/views/appointment.py | 11 +++++- 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/smash/web/tests/functions.py b/smash/web/tests/functions.py index 05db98ce..7b5d331d 100644 --- a/smash/web/tests/functions.py +++ b/smash/web/tests/functions.py @@ -1,4 +1,5 @@ import datetime +import logging import os from django.contrib.auth.models import User @@ -11,6 +12,8 @@ from web.models.constants import REDCAP_TOKEN_CONFIGURATION_TYPE, REDCAP_BASE_UR from web.redcap_connector import RedcapSubject from web.views.notifications import get_today_midnight_date +logger = logging.getLogger(__name__) + def create_get_suffix(params): result = "?" diff --git a/smash/web/tests/view/test_appointments.py b/smash/web/tests/view/test_appointments.py index c187af91..add77509 100644 --- a/smash/web/tests/view/test_appointments.py +++ b/smash/web/tests/view/test_appointments.py @@ -4,7 +4,7 @@ import logging from django.urls import reverse from web.forms import AppointmentEditForm, SubjectEditForm, StudySubjectEditForm -from web.models import Appointment, StudySubject +from web.models import Appointment, StudySubject, Visit from web.tests import LoggedInTestCase from web.tests.functions import create_study_subject, create_visit, create_appointment, create_worker, \ create_flying_team, format_form_field @@ -108,8 +108,8 @@ class AppointmentsViewTests(LoggedInTestCase): 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) + response = self.client.post(reverse('web.views.appointment_edit', kwargs={'id': appointment.id}), + data=form_data) self.assertEqual(response.status_code, 200) @@ -119,6 +119,8 @@ class AppointmentsViewTests(LoggedInTestCase): def test_save_appointments_edit_with_valid_nd_number(self): subject = create_study_subject() visit = create_visit(subject) + visit.datetime_begin = "2011-01-01" + visit.save() appointment = create_appointment(visit, get_today_midnight_date()) form_data = self.prepare_form(appointment, subject) @@ -133,6 +135,39 @@ class AppointmentsViewTests(LoggedInTestCase): updated_subject = StudySubject.objects.get(id=subject.id) self.assertTrue(updated_subject.information_sent) + updated_visit = Visit.objects.get(id=visit.id) + + self.assertEqual(appointment.datetime_when, updated_visit.datetime_begin) + self.assertNotEqual(visit.datetime_end, updated_visit.datetime_end) + + def test_save_appointments_edit(self): + subject = create_study_subject() + visit = create_visit(subject) + visit.datetime_begin = "2011-01-01" + visit.save() + visit = Visit.objects.get(id=visit.id) + + appointment = create_appointment(visit, get_today_midnight_date()) + appointment.status = Appointment.APPOINTMENT_STATUS_FINISHED + appointment.save() + + appointment2 = create_appointment(visit, get_today_midnight_date()) + + form_data = self.prepare_form(appointment2, subject) + form_data["appointment-status"] = Appointment.APPOINTMENT_STATUS_FINISHED + form_data["study-subject-nd_number"] = "ND9999" + + response = self.client.post( + reverse('web.views.appointment_edit', kwargs={'id': appointment2.id}), data=form_data) + + self.assertEqual(response.status_code, 302) + + updated_visit = Visit.objects.get(id=visit.id) + + self.assertNotEqual(appointment.datetime_when, updated_visit.datetime_begin) + self.assertEqual(visit.datetime_begin, updated_visit.datetime_begin) + self.assertEqual(visit.datetime_end, updated_visit.datetime_end) + def prepare_form(self, appointment, subject): form_appointment = AppointmentEditForm(user=self.user, instance=appointment, prefix="appointment") form_study_subject = StudySubjectEditForm(instance=subject, prefix="study-subject") diff --git a/smash/web/views/appointment.py b/smash/web/views/appointment.py index 938e7130..ff4afa90 100644 --- a/smash/web/views/appointment.py +++ b/smash/web/views/appointment.py @@ -83,12 +83,16 @@ def appointment_edit(request, id): if not appointment_form.is_valid(): is_valid_form = False + adjust_date = False if the_appointment.visit is not None: + if the_appointment.visit.visit_number == 1 and \ + Appointment.objects.filter(visit=the_appointment.visit, + status=Appointment.APPOINTMENT_STATUS_FINISHED).count() == 0: + adjust_date = True if appointment_form.cleaned_data["status"] == Appointment.APPOINTMENT_STATUS_FINISHED: if re.match('ND[0-9][0-9][0-9][0-9]', study_subject_form.cleaned_data["nd_number"]) is None: study_subject_form.add_error('nd_number', ValidationError("invalid ND number")) is_valid_form = False - if is_valid_form: appointment_form.save() if study_subject_form is not None: @@ -102,6 +106,11 @@ def appointment_edit(request, id): if the_appointment.flying_team is not None and subject.flying_team is None: subject.flying_team = the_appointment.flying_team subject.save() + if adjust_date: + the_appointment.visit.datetime_end = the_appointment.visit.datetime_end + ( + the_appointment.datetime_when - the_appointment.visit.datetime_begin) + the_appointment.visit.datetime_begin = the_appointment.datetime_when + the_appointment.visit.save() messages.success(request, "Modifications saved") if '_continue' in request.POST: -- GitLab