Skip to content

Commit

Permalink
Ensure checkbox licence conditions collapse to single textbox when th…
Browse files Browse the repository at this point in the history
…ere are no picklist conditions
  • Loading branch information
currycoder committed Dec 20, 2024
1 parent 002902d commit 2ff1228
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
2 changes: 0 additions & 2 deletions caseworker/advice/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,5 @@ class AdviceType:

class AdviceSteps:
RECOMMEND_APPROVAL = "recommend_approval"
DESNZ_APPROVAL = "desnz_approval"
FCDO_APPROVAL = "fcdo_approval"
LICENCE_CONDITIONS = "licence_conditions"
LICENCE_FOOTNOTES = "licence_footnotes"
32 changes: 16 additions & 16 deletions caseworker/advice/forms/approval.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,10 @@ def get_layout_fields(self):
)


class LicenceConditionsForm(PicklistAdviceForm, BaseForm):
class PicklistLicenceConditionsForm(PicklistAdviceForm, BaseForm):
class Layout:
TITLE = "Add licence conditions (optional)"

proviso = forms.CharField(
widget=forms.Textarea(attrs={"rows": 7, "class": "govuk-!-margin-top-4"}),
label="",
required=False,
)

approval_radios = forms.ChoiceField(
label="What is your reason for approving?",
required=False,
widget=forms.RadioSelect,
choices=(),
)
proviso_checkboxes = forms.MultipleChoiceField(
label="",
required=False,
Expand All @@ -96,14 +84,13 @@ class Layout:

def clean(self):
cleaned_data = super().clean()
# only return proviso (text) for selected radios, nothing else matters, join by 2 newlines
# only return proviso (text) for selected checkboxes, nothing else matters, join by 2 newlines
return {"proviso": "\r\n\r\n".join([cleaned_data[selected] for selected in cleaned_data["proviso_checkboxes"]])}

def __init__(self, *args, **kwargs):
proviso = kwargs.pop("proviso")

proviso_choices, proviso_text = self._picklist_to_choices(proviso)
self.proviso_text = proviso_text

self.conditional_checkbox_choices = (
ConditionalCheckboxesQuestion(choices.label, choices.value) for choices in proviso_choices
Expand All @@ -121,10 +108,23 @@ def __init__(self, *args, **kwargs):
)

def get_layout_fields(self):

return (ConditionalCheckboxes("proviso_checkboxes", *self.conditional_checkbox_choices),)


class SimpleLicenceConditionsForm(BaseForm):
class Layout:
TITLE = "Add licence conditions (optional)"

proviso = forms.CharField(
widget=forms.Textarea(attrs={"rows": 7, "class": "govuk-!-margin-top-4"}),
label="Licence condition",
required=False,
)

def get_layout_fields(self):
return ("proviso",)


class FootnotesApprovalAdviceForm(PicklistAdviceForm, BaseForm):
class Layout:
TITLE = "Add instructions to the exporter, or a reporting footnote (optional)"
Expand Down
17 changes: 15 additions & 2 deletions caseworker/advice/views/approval.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from caseworker.advice.conditionals import form_add_licence_conditions, is_desnz_team
from caseworker.advice.forms.approval import (
FootnotesApprovalAdviceForm,
LicenceConditionsForm,
PicklistLicenceConditionsForm,
SimpleLicenceConditionsForm,
RecommendAnApprovalForm,
)
from caseworker.advice.payloads import GiveApprovalAdvicePayloadBuilder
Expand All @@ -23,7 +24,7 @@ class GiveApprovalAdviceView(LoginRequiredMixin, CaseContextMixin, BaseSessionWi

form_list = [
(AdviceSteps.RECOMMEND_APPROVAL, RecommendAnApprovalForm),
(AdviceSteps.LICENCE_CONDITIONS, LicenceConditionsForm),
(AdviceSteps.LICENCE_CONDITIONS, PicklistLicenceConditionsForm),
(AdviceSteps.LICENCE_FOOTNOTES, FootnotesApprovalAdviceForm),
]

Expand All @@ -39,6 +40,18 @@ class GiveApprovalAdviceView(LoginRequiredMixin, CaseContextMixin, BaseSessionWi
AdviceSteps.LICENCE_FOOTNOTES: footnote_picklist,
}

def get_form(self, step=None, data=None, files=None):

if step == AdviceSteps.LICENCE_CONDITIONS:
picklist_form_kwargs = self.step_kwargs[AdviceSteps.LICENCE_CONDITIONS](self)
picklist_options_exist = len(picklist_form_kwargs["proviso"]["results"]) > 0
if picklist_options_exist:
return PicklistLicenceConditionsForm(data=data, **picklist_form_kwargs)
else:
return SimpleLicenceConditionsForm(data=data)

return super().get_form(step, data, files)

def get_success_url(self):
return reverse("cases:view_my_advice", kwargs=self.kwargs)

Expand Down

0 comments on commit 2ff1228

Please sign in to comment.