Skip to content
Snippets Groups Projects
Commit c2c2c743 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

Merge branch '154-daily-planning' into 'master'

when appointment is finished, nd number for subject is required

Closes #154

See merge request !78
parents 8dc5eea5 840a96cb
No related branches found
No related tags found
1 merge request!78when appointment is finished, nd number for subject is required
Pipeline #
...@@ -3,12 +3,12 @@ import logging ...@@ -3,12 +3,12 @@ import logging
from django.urls import reverse from django.urls import reverse
from functions import create_subject, create_visit, create_appointment, create_worker, create_flying_team, \ from web.tests.functions import create_subject, create_visit, create_appointment, create_worker, create_flying_team, \
format_form_field format_form_field
from web.forms import AppointmentEditForm, SubjectEditForm from web.forms import AppointmentEditForm, SubjectEditForm
from web.models import Appointment, Subject from web.models import Appointment, Subject
from web.views.notifications import get_today_midnight_date from web.views.notifications import get_today_midnight_date
from . import LoggedInTestCase from web.tests import LoggedInTestCase
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -84,19 +84,36 @@ class AppointmentsViewTests(LoggedInTestCase): ...@@ -84,19 +84,36 @@ class AppointmentsViewTests(LoggedInTestCase):
for key, value in form_appointment.initial.items(): for key, value in form_appointment.initial.items():
if value is not None: if value is not None:
form_data['appointment-{}'.format(key)] = format_form_field(value) form_data['appointment-{}'.format(key)] = format_form_field(value)
form_data['appointment-status'.format(key)] = Appointment.APPOINTMENT_STATUS_FINISHED form_data['appointment-status'] = Appointment.APPOINTMENT_STATUS_FINISHED
self.client.post(reverse('web.views.appointment_edit', kwargs={'id': appointment.id}), data=form_data) self.client.post(reverse('web.views.appointment_edit', kwargs={'id': appointment.id}), data=form_data)
appointment_result = Appointment.objects.filter(id=appointment.id)[0] appointment_result = Appointment.objects.filter(id=appointment.id)[0]
self.assertEqual(Appointment.APPOINTMENT_STATUS_FINISHED, appointment_result.status) self.assertEqual(Appointment.APPOINTMENT_STATUS_FINISHED, appointment_result.status)
def test_save_appointments_edit(self): def test_save_appointments_edit_with_invalid_nd_number(self):
subject = create_subject()
visit = create_visit(subject)
appointment = create_appointment(visit, get_today_midnight_date())
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)
self.assertEqual(response.status_code, 200)
updated_subject = Subject.objects.get(id=subject.id)
self.assertFalse(updated_subject.information_sent)
def test_save_appointments_edit_with_valid_nd_number(self):
subject = create_subject() subject = create_subject()
visit = create_visit(subject) visit = create_visit(subject)
appointment = create_appointment(visit, get_today_midnight_date()) appointment = create_appointment(visit, get_today_midnight_date())
form_data = self.prepare_form(appointment, subject) form_data = self.prepare_form(appointment, subject)
form_data["appointment-status"] = Appointment.APPOINTMENT_STATUS_FINISHED form_data["appointment-status"] = Appointment.APPOINTMENT_STATUS_FINISHED
form_data["subject-nd_number"] = "ND9999"
response = self.client.post( response = self.client.post(
reverse('web.views.appointment_edit', kwargs={'id': appointment.id}), data=form_data) reverse('web.views.appointment_edit', kwargs={'id': appointment.id}), data=form_data)
...@@ -127,9 +144,9 @@ class AppointmentsViewTests(LoggedInTestCase): ...@@ -127,9 +144,9 @@ class AppointmentsViewTests(LoggedInTestCase):
form_data["appointment-status"] = Appointment.APPOINTMENT_STATUS_FINISHED form_data["appointment-status"] = Appointment.APPOINTMENT_STATUS_FINISHED
form_data["appointment-flying_team"] = create_flying_team().id form_data["appointment-flying_team"] = create_flying_team().id
form_data['appointment-status'] = Appointment.APPOINTMENT_STATUS_FINISHED form_data['appointment-status'] = Appointment.APPOINTMENT_STATUS_FINISHED
form_data["subject-nd_number"] = "ND9999"
response = self.client.post( self.client.post(reverse('web.views.appointment_edit', kwargs={'id': appointment.id}), data=form_data)
reverse('web.views.appointment_edit', kwargs={'id': appointment.id}), data=form_data)
updated_subject = Subject.objects.get(id=subject.id) updated_subject = Subject.objects.get(id=subject.id)
self.assertIsNotNone(updated_subject.flying_team) self.assertIsNotNone(updated_subject.flying_team)
# coding=utf-8 # coding=utf-8
import logging import logging
import re
from django.contrib import messages from django.contrib import messages
from django.core.exceptions import ValidationError
from django.shortcuts import get_object_or_404, redirect from django.shortcuts import get_object_or_404, redirect
from . import wrap_response from . import wrap_response
...@@ -64,7 +66,6 @@ def appointment_edit(request, id): ...@@ -64,7 +66,6 @@ def appointment_edit(request, id):
instance=the_appointment, instance=the_appointment,
user=request.user, user=request.user,
prefix="appointment") prefix="appointment")
is_valid_form = True is_valid_form = True
if the_appointment.visit is not None: if the_appointment.visit is not None:
subject_form = SubjectEditForm(request.POST, instance=the_appointment.visit.subject, prefix="subject") subject_form = SubjectEditForm(request.POST, instance=the_appointment.visit.subject, prefix="subject")
...@@ -74,6 +75,12 @@ def appointment_edit(request, id): ...@@ -74,6 +75,12 @@ def appointment_edit(request, id):
if not appointment_form.is_valid(): if not appointment_form.is_valid():
is_valid_form = False is_valid_form = False
if the_appointment.visit is not None:
if appointment_form.cleaned_data["status"] == Appointment.APPOINTMENT_STATUS_FINISHED:
if re.match('ND[0-9][0-9][0-9][0-9]', subject_form.cleaned_data["nd_number"]) is None:
subject_form.add_error('nd_number', ValidationError("invalid ND number"))
is_valid_form = False
if is_valid_form: if is_valid_form:
appointment_form.save() appointment_form.save()
if subject_form is not None: if subject_form is not None:
...@@ -94,6 +101,9 @@ def appointment_edit(request, id): ...@@ -94,6 +101,9 @@ def appointment_edit(request, id):
return redirect('web.views.visit_details', id=the_appointment.visit.id) return redirect('web.views.visit_details', id=the_appointment.visit.id)
else: else:
return redirect('web.views.appointments') return redirect('web.views.appointments')
else:
messages.add_message(request, messages.ERROR, 'Invalid data. Please fix data and try again.')
else: else:
appointment_form = AppointmentEditForm(instance=the_appointment, user=request.user, prefix="appointment") appointment_form = AppointmentEditForm(instance=the_appointment, user=request.user, prefix="appointment")
......
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