Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/improve forms UI #687

Merged
merged 14 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion main/templates/base/fetc_form_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
</div>
{% endblock %}

{{ form }}
{% block auto-form-render %}{{ form }}{% endblock %}

{% block form-buttons %}
{% block post-form-pre-buttons %}{% endblock %}

Expand Down
13 changes: 10 additions & 3 deletions proposals/utils/validate_proposal.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this file, SessionOverviewForm should not be in the following list:

            if issubclass(
                form_class,
                (
                    InterventionForm,
                    ObservationForm,
                    SessionUpdateForm,
                    SessionOverviewForm,
                ),
            ):
                kwargs["study"] = obj.study

As it has Study as its object already, so obj.study doesn't exist and throws an exception.

Not on-topic for this PR, feel free to convert this to a separate issue.

Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"):
Expand Down Expand Up @@ -288,15 +296,14 @@ def get_form_errors(proposal: Proposal) -> list:
InterventionForm,
ObservationForm,
SessionUpdateForm,
SessionOverviewForm,
),
):
kwargs["study"] = obj.study

instance = form_class(**kwargs)

for field, error in instance.errors.items():
if field in instance.fields:
if field in instance.fields or field == "__all__":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great, can't believe we weren't doing this yet.

troublesome_pages.append(
{
"url": url,
Expand Down
30 changes: 24 additions & 6 deletions studies/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,17 +217,36 @@ 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):
"""
Check for conditional requirements:
- 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 één van de opties te selecteren."), code="required"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recently learned (guess from who) that you're not supposed to write "één" in Dutch if there's no chance of ambiguity. Same with "Tijd om er één aan te maken!", by the book it shouldn't have accents.

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This View is still getting a study kwarg argument from somewhere, causing it to error out.


super(StudyEndForm, self).__init__(*args, **kwargs)

Expand All @@ -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"]

Expand Down
32 changes: 12 additions & 20 deletions tasks/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,44 +162,36 @@ 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)

tasks_duration = self.fields["tasks_duration"]
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):
Expand Down
4 changes: 2 additions & 2 deletions tasks/templates/tasks/session_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ <h4 class="mt-2">
{% include "tasks/task_list.html" %}
</div>
{% empty %}
<div class="alert alert-primary" role="alert">{% trans "Nog geen sessies. Tijd om er één aan te maken!" %}</div>
<div class="alert alert-info" role="alert">{% trans "Nog geen sessies. Tijd om er één aan te maken!" %}</div>
{% endfor %}
{% if can_edit_sessions %}
<div class="mt-3">
<div class="mt-3 mb-3">
<a href="{% url 'tasks:session_create' study.pk %}">
<h5 class = "pt-2 text-center">
{% trans "Nieuwe sessie tevoegen" %}
Expand Down
2 changes: 2 additions & 0 deletions tasks/templates/tasks/session_overview.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ <h2>{% trans "Overzicht van alle sessies en taken" %}</h2>
{% include "tasks/session_list.html" %}
{% endblock %}

{% block auto-form-render %}<!--Override rendering of the form-->{% endblock %}

{% block form-buttons %}
{% include "base/form_buttons.html" %}
{% endblock %}
2 changes: 2 additions & 0 deletions tasks/templates/tasks/session_start.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ <h2>{% trans "Het takenonderzoek en/of interviews" %}</h2>
{% endblocktrans %}
</p>
{% endblock %}

{% block auto-form-render %}<!--Override rendering of the form-->{% endblock %}
2 changes: 1 addition & 1 deletion tasks/templates/tasks/task_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
</table>
{% else %}
<div class="alert alert-info" role="alert">
{% trans "Deze sessie bevat nog geen taken. Klik op het podloodje om taken aan te maken!" %}
{% trans "Deze sessie bevat nog geen taken. Tijd om er één aan te maken!" %}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Translation hasn't been updated

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking we create a seperate issue for translation ... Because a lot of stuff is not translated or not up to date ...

</div>
{% endif %}
{% if can_edit_tasks %}
Expand Down
Loading