diff --git a/main/templates/base/fetc_form_base.html b/main/templates/base/fetc_form_base.html index b8db75a29..4ad468896 100644 --- a/main/templates/base/fetc_form_base.html +++ b/main/templates/base/fetc_form_base.html @@ -31,7 +31,8 @@ {% endblock %} - {{ form }} + {% block auto-form-render %}{{ form }}{% endblock %} + {% block form-buttons %} {% block post-form-pre-buttons %}{% endblock %} diff --git a/proposals/utils/validate_proposal.py b/proposals/utils/validate_proposal.py index 77b3d1690..1133447d2 100644 --- a/proposals/utils/validate_proposal.py +++ b/proposals/utils/validate_proposal.py @@ -11,7 +11,7 @@ from interventions.forms import InterventionForm from observations.forms import ObservationForm -from studies.forms import StudyForm, StudyDesignForm +from studies.forms import StudyForm, StudyDesignForm, StudyEndForm from tasks.forms import SessionUpdateForm, SessionEndForm, TaskForm, SessionOverviewForm from ..forms import ( ProposalForm, @@ -134,6 +134,14 @@ def _build_forms(proposal: Proposal) -> OrderedDict: study, ) + end_key = "{}_end".format(key_base) + forms[end_key] = ( + StudyEndForm, + reverse("studies:design_end", args=[study.pk]), + _("Trajectoverzicht (traject {})").format(study.order), + study, + ) + if study.has_intervention: intervention_key = "{}_intervention".format(key_base) if hasattr(study, "intervention"): @@ -288,7 +296,6 @@ def get_form_errors(proposal: Proposal) -> list: InterventionForm, ObservationForm, SessionUpdateForm, - SessionOverviewForm, ), ): kwargs["study"] = obj.study @@ -296,7 +303,7 @@ def get_form_errors(proposal: Proposal) -> list: instance = form_class(**kwargs) for field, error in instance.errors.items(): - if field in instance.fields: + if field in instance.fields or field == "__all__": troublesome_pages.append( { "url": url, diff --git a/studies/forms.py b/studies/forms.py index 3b364dd16..aac7cadbf 100644 --- a/studies/forms.py +++ b/studies/forms.py @@ -217,7 +217,16 @@ class StudyDesignForm(TemplatedModelForm): class Meta: model = Study - fields = [] + fields = [ + "has_intervention", + "has_observation", + "has_sessions", + ] + widgets = { + "has_intervention": forms.HiddenInput(), + "has_observation": forms.HiddenInput(), + "has_sessions": forms.HiddenInput(), + } def clean(self): """ @@ -225,9 +234,19 @@ def clean(self): - at least one of the fields has to be checked """ cleaned_data = super(StudyDesignForm, self).clean() - if not cleaned_data: - msg = _("Je dient minstens één van de opties te selecteren") - self.add_error("study_types", forms.ValidationError(msg, code="required")) + + # This solution is a bit funky, but by using add_error(), it appends our + # error msg to a built-in required error message. + if not "study_types" in cleaned_data: + error = forms.ValidationError( + _("Je dient minstens een van de opties te selecteren."), code="required" + ) + self.errors["study_types"] = error + + # this checks the hidden fields, and could be used for validating this + # form elsewhere + if not any(cleaned_data.values()): + self.add_error(None, _("Er is nog geen onderzoekstype geselecteerd.")) class StudyConsentForm(ConditionalModelForm): @@ -285,7 +304,6 @@ def __init__(self, *args, **kwargs): - Remove empty label from deception/negativity/stressful/risk field and reset the choices - mark_safe the labels of negativity/stressful/risk """ - self.study = kwargs.pop("study", None) super(StudyEndForm, self).__init__(*args, **kwargs) @@ -302,7 +320,7 @@ def __init__(self, *args, **kwargs): self.fields["stressful"].label = mark_safe(self.fields["stressful"].label) self.fields["risk"].label = mark_safe(self.fields["risk"].label) - if not self.study.has_sessions: + if not self.instance.has_sessions: del self.fields["deception"] del self.fields["deception_details"] diff --git a/studies/views/study_views.py b/studies/views/study_views.py index fe4dfc0d2..c9efe4e65 100644 --- a/studies/views/study_views.py +++ b/studies/views/study_views.py @@ -150,12 +150,6 @@ def get_context_data(self, **kwargs): context["proposal"] = self.object.proposal return context - def get_form_kwargs(self): - """Sets the Study as a form kwarg""" - kwargs = super(StudyEnd, self).get_form_kwargs() - kwargs["study"] = self.object - return kwargs - def get_next_url(self): """ If there is another Study in this Proposal, continue to that one. diff --git a/tasks/forms.py b/tasks/forms.py index 1137cea2f..9671e5149 100644 --- a/tasks/forms.py +++ b/tasks/forms.py @@ -162,14 +162,9 @@ class Meta: model = Session fields = ["tasks_duration"] - _soft_validation_fields = [ - "tasks_duration", - ] - def __init__(self, *args, **kwargs): """ - Set the tasks_duration label - - Set the tasks_duration as required """ super(SessionEndForm, self).__init__(*args, **kwargs) @@ -177,29 +172,26 @@ def __init__(self, *args, **kwargs): label = tasks_duration.label % self.instance.net_duration() tasks_duration.label = mark_safe(label) - def is_initial_visit(self) -> bool: - return True - def clean(self): cleaned_data = super(SessionEndForm, self).clean() - self.mark_soft_required(cleaned_data, "tasks_duration") - - return cleaned_data - - def clean_tasks_duration(self): - """ - Check that the net duration is at least equal to the gross duration - """ tasks_duration = self.cleaned_data.get("tasks_duration") - if tasks_duration and tasks_duration < self.instance.net_duration(): - raise forms.ValidationError( + if tasks_duration is not None and tasks_duration < self.instance.net_duration(): + self.add_error( + "tasks_duration", _("Totale sessieduur moet minstens gelijk zijn aan netto sessieduur."), - code="comparison", ) - return tasks_duration + if self.instance.tasks_number == 0: + self.add_error( + None, + _("Sessie {} bevat nog geen taken. Voeg minstens één taak toe.").format( + self.instance.order + ), + ) + + return cleaned_data class SessionOverviewForm(SoftValidationMixin, ModelForm): diff --git a/tasks/templates/tasks/session_list.html b/tasks/templates/tasks/session_list.html index 5992d9942..d0ec4ba12 100644 --- a/tasks/templates/tasks/session_list.html +++ b/tasks/templates/tasks/session_list.html @@ -32,10 +32,10 @@

{% include "tasks/task_list.html" %} {% empty %} - + {% endfor %} {% if can_edit_sessions %} -
+