Skip to content

Commit

Permalink
feat: repurposed HiddenForm to validate has_no_sessions() in validate…
Browse files Browse the repository at this point in the history
…_proposal.py
  • Loading branch information
EdoStorm96 committed Mar 28, 2024
1 parent 075e352 commit c81a7f5
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 15 deletions.
29 changes: 26 additions & 3 deletions proposals/utils/validate_proposal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down
32 changes: 32 additions & 0 deletions tasks/forms.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
15 changes: 3 additions & 12 deletions tasks/views/session_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit c81a7f5

Please sign in to comment.