diff --git a/smash/web/models/appointment.py b/smash/web/models/appointment.py index bebf0265dd09b873abc9920bceb918e5d29df565..f71d2bbbf0fabe8258ef335e71b8ebc1fae5e482 100644 --- a/smash/web/models/appointment.py +++ b/smash/web/models/appointment.py @@ -47,6 +47,8 @@ class Appointment(models.Model): visit = models.ForeignKey(Visit, verbose_name='Visit ID', editable=False, + null=True, + blank=True, ) comment = models.TextField(max_length=1024, verbose_name='Comment', @@ -103,7 +105,7 @@ class Appointment(models.Model): return result def title(self): - if self.visit.subject.screening_number == "---": + if self.visit is None: return self.comment.replace("\n", ";").replace("\r", ";") else: title = self.visit.subject.first_name + " " + self.visit.subject.last_name + " type: " diff --git a/smash/web/templates/appointments/edit.html b/smash/web/templates/appointments/edit.html index deb2c44b27d29b220327933c6b32e67b3d7d5af9..15159b329b3b1d34ee927ae1d5b78738ad74e96b 100644 --- a/smash/web/templates/appointments/edit.html +++ b/smash/web/templates/appointments/edit.html @@ -59,41 +59,42 @@ </fieldset> - <fieldset> - <div class="box-header with-border"> - <h3 class="box-title">Subject's details</h3> - </div> - <div class="box-body"> - {% for field in subject_form %} - <div class="col-md-6 form-group {% if field.errors %}has-error{% endif %}"> - <label for="{# TODO #}" class="col-sm-4 control-label"> - {{ field.label }} - </label> + {% if subject_form %} + <fieldset> + <div class="box-header with-border"> + <h3 class="box-title">Subject's details</h3> + </div> - <div class="col-sm-8"> - {{ field|add_class:'form-control' }} - </div> + <div class="box-body"> + {% for field in subject_form %} + <div class="col-md-6 form-group {% if field.errors %}has-error{% endif %}"> + <label for="{# TODO #}" class="col-sm-4 control-label"> + {{ field.label }} + </label> - {% if field.errors %} - <span class="help-block"> - {{ field.errors }} - </span> - {% endif %} - </div> - {% endfor %} - </div><!-- /.box-body --> + <div class="col-sm-8"> + {{ field|add_class:'form-control' }} + </div> - <div class="box-footer"> - <div class="col-sm-6"> - <button type="submit" class="btn btn-block btn-success">Save</button> - </div> - <div class="col-sm-6"> - <a href="{% url 'web.views.appointments' %}" class="btn btn-block btn-default" - onclick="history.back()">Cancel</a> - </div> - </div><!-- /.box-footer --> - </fieldset> + {% if field.errors %} + <span class="help-block"> {{ field.errors }} </span> + {% endif %} + </div> + {% endfor %} + </div><!-- /.box-body --> + + </fieldset> + {% endif %} + <div class="box-footer"> + <div class="col-sm-6"> + <button type="submit" class="btn btn-block btn-success">Save</button> + </div> + <div class="col-sm-6"> + <a href="{% url 'web.views.appointments' %}" class="btn btn-block btn-default" + onclick="history.back()">Cancel</a> + </div> + </div><!-- /.box-footer --> </form> </div> {% endblock %} diff --git a/smash/web/templates/appointments/index.html b/smash/web/templates/appointments/index.html index 8fef6073fa2bcc8dc84688618f365c2658429e45..94a7f026992b5d7be21632e4c073b089c22417db 100644 --- a/smash/web/templates/appointments/index.html +++ b/smash/web/templates/appointments/index.html @@ -79,7 +79,7 @@ {% for approach in approaching_list %} <tr> - {% if approach.visit.subject.screening_number == "---" %} + {% if approach.visit == None %} <td> </td> <td> diff --git a/smash/web/templates/appointments/list.html b/smash/web/templates/appointments/list.html index 19ccaa1f2ca20de2bb8d46e83de62cf0065917de..782f8d5ba79e2aa93e8361023872a950f9acad2c 100644 --- a/smash/web/templates/appointments/list.html +++ b/smash/web/templates/appointments/list.html @@ -38,7 +38,7 @@ {% for approach in appointment_list %} <tr> - {% if approach.visit.subject.screening_number == "---" %} + {% if approach.visit == None %} <td> </td> <td> diff --git a/smash/web/tests/test_model_appointment.py b/smash/web/tests/test_model_appointment.py new file mode 100644 index 0000000000000000000000000000000000000000..252c807c128a7a93b8621ab1059d2cb6ff6c27c2 --- /dev/null +++ b/smash/web/tests/test_model_appointment.py @@ -0,0 +1,17 @@ +from django.test import TestCase + +from functions import create_subject, create_appointment +from functions import create_visit +from web.models import Appointment +from web.models import Visit + + +class AppointmentModelTests(TestCase): + def test_title_for_appointment_without_visit(self): + comment = "some comment title" + appointment = create_appointment() + appointment.visit = None + appointment.comment = comment + appointment.save() + + self.assertEqual(comment, appointment.title()) diff --git a/smash/web/tests/test_view_appointments.py b/smash/web/tests/test_view_appointments.py index 1cbbf13890a52f851d7907e364223ceed8f4874d..3e7c2d53f5c8c9181ae3db7ed632063456173d8f 100644 --- a/smash/web/tests/test_view_appointments.py +++ b/smash/web/tests/test_view_appointments.py @@ -51,3 +51,31 @@ class AppointmentsViewTests(TestCase): self.assertEqual(new_comment, updated_appointment.comment) self.assertEqual(new_status, updated_appointment.status) self.assertEqual(new_last_name, updated_subject.last_name) + + def test_appointments_edit_without_visit(self): + create_worker(self.user) + + appointment = create_appointment() + appointment.visit = None + appointment.save() + + response = self.client.get( + reverse('web.views.appointment_edit', kwargs={'id': appointment.id})) + self.assertEqual(response.status_code, 200) + + def test_save_appointments_edit_without_visit(self): + create_worker(self.user) + + appointment = create_appointment() + appointment.visit = None + appointment.save() + + form_appointment = AppointmentEditForm(user=self.user, instance=appointment, prefix="appointment") + form_data = {} + for key, value in form_appointment.initial.items(): + if value is not None: + form_data['appointment-{}'.format(key)] = value + response = self.client.post( + reverse('web.views.appointment_edit', kwargs={'id': appointment.id}), data=form_data) + + self.assertEqual(response.status_code, 200) diff --git a/smash/web/urls.py b/smash/web/urls.py index 6392138cb655cb16ce445ab0f1511f12b6824431..9fb19ef846112fd2431b5778b7ac14ea72cc3073 100644 --- a/smash/web/urls.py +++ b/smash/web/urls.py @@ -26,9 +26,11 @@ urlpatterns = [ url(r'^appointments/details/(?P<id>\d+)$', views.appointment.appointment_details, name='web.views.appointment_details'), url(r'^appointments/add/(?P<visit_id>\d+)$', views.appointment.appointment_add, name='web.views.appointment_add'), + url(r'^appointments/add/general$', views.appointment.appointment_add, name='web.views.appointment_add'), url(r'^appointments/edit/(?P<id>\d+)$', views.appointment.appointment_edit, name='web.views.appointment_edit'), url(r'^appointments/edit_datetime/(?P<id>\d+)$', views.appointment.appointment_edit_datetime, name='web.views.appointment_edit_datetime'), + url(r'^visits$', views.visit.visits, name='web.views.visits'), url(r'^visits/exceeded$', views.visit.exceeded_visits, name='web.views.exceeded_visits'), url(r'^visits/unfinished$', views.visit.unfinished_visits, name='web.views.unfinished_visits'), diff --git a/smash/web/views/appointment.py b/smash/web/views/appointment.py index 0f1380359af3acbc48f8b7dd08b5c72d1565dbf5..d0a4582f153e4b49b1eca86b48aae2f9952fd816 100644 --- a/smash/web/views/appointment.py +++ b/smash/web/views/appointment.py @@ -42,14 +42,17 @@ def appointment_details(request, id): return wrap_response(request, 'appointments/details.html', {'form': form}) -def appointment_add(request, visit_id): +def appointment_add(request, visit_id=None): full_list = get_calendar_full_appointments(request.user) if request.method == 'POST': form = AppointmentAddForm(request.POST, request.FILES, user=request.user) if form.is_valid(): form.instance.visit_id = visit_id form.save() - return redirect('web.views.visit_details', id=visit_id) + if visit_id is None: + return redirect('web.views.appointments') + else: + return redirect('web.views.visit_details', id=visit_id) else: form = AppointmentAddForm(user=request.user) @@ -59,22 +62,37 @@ def appointment_add(request, visit_id): def appointment_edit(request, id): the_appointment = get_object_or_404(Appointment, id=id) + subject_form = None + if request.method == 'POST': appointment_form = AppointmentEditForm(request.POST, request.FILES, instance=the_appointment, user=request.user, prefix="appointment") - subject_form = SubjectEditForm(request.POST, instance=the_appointment.visit.subject, prefix="subject") - if appointment_form.is_valid() and subject_form.is_valid(): + is_valid_form = True + if the_appointment.visit is not None: + subject_form = SubjectEditForm(request.POST, instance=the_appointment.visit.subject, prefix="subject") + if not subject_form.is_valid(): + is_valid_form = False + + if not appointment_form.is_valid(): + is_valid_form = False + + if is_valid_form: appointment_form.save() - subject_form.save() - return redirect('web.views.appointments') + if subject_form is not None: + subject_form.save() + the_appointment = get_object_or_404(Appointment, id=id) + if (the_appointment.status != Appointment.APPOINTMENT_STATUS_SCHEDULED) and (the_appointment.visit is not None): + return redirect('web.views.visit_details', id=the_appointment.visit.id) + else: + return redirect('web.views.appointments') else: appointment_form = AppointmentEditForm(instance=the_appointment, user=request.user, prefix="appointment") - - subject_form = SubjectEditForm(instance=the_appointment.visit.subject, prefix="subject") + if the_appointment.visit is not None: + subject_form = SubjectEditForm(instance=the_appointment.visit.subject, prefix="subject") return wrap_response(request, 'appointments/edit.html', { 'form': appointment_form,