diff --git a/proposals/utils/validate_proposal.py b/proposals/utils/validate_proposal.py index 810cc21f0..90db5dd2a 100644 --- a/proposals/utils/validate_proposal.py +++ b/proposals/utils/validate_proposal.py @@ -12,7 +12,7 @@ from interventions.forms import InterventionForm from observations.forms import ObservationForm from studies.forms import StudyForm, StudyDesignForm -from tasks.forms import SessionUpdateForm, SessionEndForm, TaskForm +from tasks.forms import SessionUpdateForm, SessionEndForm, TaskForm, SessionOverviewForm from ..forms import ( ProposalForm, WmoForm, @@ -157,7 +157,24 @@ def _build_forms(proposal: Proposal) -> OrderedDict: None, ) - if study.has_sessions: + if study.has_no_sessions(): + session_overview_key = "{}_session_overview".format( + key_base, + ) + forms[session_overview_key] = ( + SessionOverviewForm, + reverse( + "tasks:session_overview", + args=[ + study.pk, + ], + ), + _("Overzicht van het takenonderzoek (traject {})").format( + study.order, + ), + study, + ) + elif study.has_sessions: for session in study.session_set.all(): session_start_key = "{}_session_{}_start".format( key_base, @@ -247,7 +264,13 @@ def get_form_errors(proposal: Proposal) -> list: kwargs["proposal"] = proposal if issubclass( - form_class, (InterventionForm, ObservationForm, SessionUpdateForm) + form_class, + ( + InterventionForm, + ObservationForm, + SessionUpdateForm, + SessionOverviewForm, + ), ): kwargs["study"] = obj.study diff --git a/tasks/forms.py b/tasks/forms.py index 0d3b69fc5..eed1bb07b 100644 --- a/tasks/forms.py +++ b/tasks/forms.py @@ -1,10 +1,12 @@ # -*- encoding: utf-8 -*- from django import forms +from django.forms import ModelForm from django.utils.translation import ugettext_lazy as _ from main.forms import ConditionalModelForm, SoftValidationMixin from main.utils import YES_NO +from tasks.models import Study from .models import Session, Task @@ -155,6 +157,10 @@ class Meta: model = Session fields = ["tasks_duration"] + _soft_validation_fields = [ + "tasks_duration", + ] + def __init__(self, *args, **kwargs): """ - Set the tasks_duration label @@ -189,3 +195,29 @@ def clean_tasks_duration(self): ) return tasks_duration + + +class SessionOverviewForm(SoftValidationMixin, ModelForm): + """This is form is mostly used to make the navigation work on + SessionOverview and SessionStart. However, it is also use to validate + that if study.has_session, it also contains sessions.""" + + class Meta: + model = Study + fields = [] + + def get_soft_validation_fields(self): + return self.errors + + def clean(self): + cleaned_data = super(SessionOverviewForm, self).clean() + + if self.instance.has_no_sessions(): + self.add_error( + None, + _( + "Je hebt aangegeven dat traject {} sessie's en taken bevat, maar er zijn nog geen sessie's aangemaakt." + ).format(self.instance.order), + ) + + return cleaned_data diff --git a/tasks/views/session_views.py b/tasks/views/session_views.py index 6a7a3e981..3eaf45363 100644 --- a/tasks/views/session_views.py +++ b/tasks/views/session_views.py @@ -4,10 +4,9 @@ from django.urls import reverse from django.http import HttpResponseRedirect from django.utils.translation import ugettext_lazy as _ -from django.forms import ModelForm from main.views import AllowErrorsOnBackbuttonMixin, UpdateView, DeleteView, CreateView -from ..forms import SessionUpdateForm, SessionEndForm +from ..forms import SessionUpdateForm, SessionEndForm, SessionOverviewForm from ..models import Session, Study @@ -45,18 +44,10 @@ def delete(self, request, *args, **kwargs): ################## -class HiddenForm(ModelForm): - """This is needed to make the navigation work on this information page.""" - - class Meta: - model = Study - fields = [] - - class SessionStart(AllowErrorsOnBackbuttonMixin, UpdateView): model = Study - form_class = HiddenForm + form_class = SessionOverviewForm template_name = "tasks/session_start.html" def get_next_url(self): @@ -159,7 +150,7 @@ def get_back_url(self): class SessionOverview(AllowErrorsOnBackbuttonMixin, UpdateView): model = Study - form_class = HiddenForm + form_class = SessionOverviewForm template_name = "tasks/session_overview.html" def get_context_data(self, **kwargs):