Skip to content

Commit

Permalink
Prevent 'other' radio option showing for advice picklist fields which…
Browse files Browse the repository at this point in the history
… have no picklist options
  • Loading branch information
currycoder committed Dec 20, 2024
1 parent bd51214 commit 002902d
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 24 deletions.
18 changes: 1 addition & 17 deletions caseworker/advice/forms/approval.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from core.common.forms import BaseForm
from crispy_forms_gds.helper import FormHelper
from crispy_forms_gds.layout import Layout, Submit
from crispy_forms_gds.choices import Choice

from caseworker.advice.forms.forms import PicklistAdviceForm
from core.forms.layouts import (
ConditionalCheckboxes,
ConditionalCheckboxesQuestion,
Expand All @@ -28,22 +28,6 @@ def __init__(self, *args, **kwargs):
self.helper.add_input(Submit("submit", "Continue"))


class PicklistAdviceForm(forms.Form):
def _picklist_to_choices(self, picklist_data):
reasons_choices = []
reasons_text = {"other": ""}

for result in picklist_data["results"]:
key = "_".join(result.get("name").lower().split())
choice = Choice(key, result.get("name"))
if result == picklist_data["results"][-1]:
choice = Choice(key, result.get("name"), divider="or")
reasons_choices.append(choice)
reasons_text[key] = result.get("text")
reasons_choices.append(Choice("other", "Other"))
return reasons_choices, reasons_text


class MoveCaseForwardForm(forms.Form):
def __init__(self, move_case_button_label="Move case forward", *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down
2 changes: 1 addition & 1 deletion caseworker/advice/forms/consolidate.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class ConsolidateApprovalForm(PicklistAdviceForm):
def __init__(self, approval_reason, proviso, **kwargs):
super().__init__(**kwargs)

approval_choices, approval_text = self._picklist_to_choices(approval_reason, include_other=False)
approval_choices, approval_text = self._picklist_to_choices(approval_reason)
self.approval_text = approval_text
self.fields["approval_radios"].choices = approval_choices

Expand Down
6 changes: 4 additions & 2 deletions caseworker/advice/forms/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def get_approval_advice_form_factory(advice, approval_reason, proviso, footnote_
class PicklistAdviceForm(forms.Form):
def _picklist_to_choices(self, picklist_data, include_other=True):
reasons_choices = []
reasons_text = {"other": ""}
reasons_text = {}

for result in picklist_data["results"]:
key = "_".join(result.get("name").lower().split())
Expand All @@ -40,7 +40,9 @@ def _picklist_to_choices(self, picklist_data, include_other=True):
choice = Choice(key, result.get("name"), divider="or")
reasons_choices.append(choice)
reasons_text[key] = result.get("text")
if include_other:
picklist_choices = len(reasons_choices) > 0
if include_other and picklist_choices:
reasons_text["other"] = ""
reasons_choices.append(Choice("other", "Other"))
return reasons_choices, reasons_text

Expand Down
5 changes: 1 addition & 4 deletions caseworker/advice/views/tests/test_consolidate_advice.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,10 +401,7 @@ def test_ConsolidateApproveView_GET_canned_snippets(
soup = BeautifulSoup(response.content, "html.parser")
assert "firearm serial numbers" in soup.find("div", {"id": "div_id_proviso_snippets"}).text
assert soup.find("button", attrs={"data-snippet-key": "firearm_serial_numbers"}).text == "Add licence condition"
assert (
soup.find("script", {"id": "proviso"}).text
== '{"other": "", "firearm_serial_numbers": "Firearm serial numbers text"}'
)
assert soup.find("script", {"id": "proviso"}).text == '{"firearm_serial_numbers": "Firearm serial numbers text"}'


def test_ConsolidateApproveView_POST_bad_input(
Expand Down

0 comments on commit 002902d

Please sign in to comment.