diff --git a/src/api/views.py b/src/api/views.py index b36b3ee72..2199e15ac 100644 --- a/src/api/views.py +++ b/src/api/views.py @@ -72,7 +72,6 @@ def check_domain_available(domain): given domain doesn't end with .gov, ".gov" is added when looking for a match. If check fails, throws a RegistryError. """ - Domain = apps.get_model("registrar.Domain") if domain.endswith(".gov"): diff --git a/src/registrar/forms/domain_request_wizard.py b/src/registrar/forms/domain_request_wizard.py index 0f3b0c3f3..3be5c4cc8 100644 --- a/src/registrar/forms/domain_request_wizard.py +++ b/src/registrar/forms/domain_request_wizard.py @@ -864,13 +864,19 @@ class CisaRepresentativeForm(BaseDeletableRegistrarForm): ) -class CisaRepresentativeYesNoForm(RegistrarForm): +class BaseYesNoForm(RegistrarForm): + """Used for forms with a yes/no form with a hidden input on toggle""" + + form_is_checked = None + typed_choice_field_name = None + def __init__(self, *args, **kwargs): """Extend the initialization of the form from RegistrarForm __init__""" super().__init__(*args, **kwargs) + # set the initial value based on attributes of domain request if self.domain_request: - if self.domain_request.has_cisa_representative(): + if self.form_is_checked: initial_value = True else: initial_value = False @@ -878,8 +884,8 @@ class CisaRepresentativeYesNoForm(RegistrarForm): # No pre-selection for new domain requests initial_value = None - self.fields["has_cisa_representative"] = forms.TypedChoiceField( - coerce=lambda x: x.lower() == "true" if x is not None else None, # coerce strings to bool, excepting None + self.fields[self.typed_choice_field_name] = forms.TypedChoiceField( + coerce=lambda x: x.lower() == "true" if x is not None else None, choices=((True, "Yes"), (False, "No")), initial=initial_value, widget=forms.RadioSelect, @@ -889,6 +895,16 @@ class CisaRepresentativeYesNoForm(RegistrarForm): ) +class CisaRepresentativeYesNoForm(BaseYesNoForm): + """Yes/no toggle for the CISA regions question on additional details""" + + # Note that these can be set in __init__ if you need more fine-grained control + form_is_checked = property( + lambda self: self.domain_request.has_cisa_representative() if self.domain_request else False + ) + typed_choice_field_name = "has_cisa_representative" + + class AdditionalDetailsForm(BaseDeletableRegistrarForm): anything_else = forms.CharField( required=True, @@ -903,29 +919,14 @@ class AdditionalDetailsForm(BaseDeletableRegistrarForm): ) -class AdditionalDetailsYesNoForm(RegistrarForm): - def __init__(self, *args, **kwargs): - """Extend the initialization of the form from RegistrarForm __init__""" - super().__init__(*args, **kwargs) - # set the initial value based on attributes of domain request - if self.domain_request: - if self.domain_request.has_anything_else_text(): - initial_value = True - else: - initial_value = False - else: - # No pre-selection for new domain requests - initial_value = None +class AdditionalDetailsYesNoForm(BaseYesNoForm): + """Yes/no toggle for the anything else question on additional details""" - self.fields["has_anything_else_text"] = forms.TypedChoiceField( - coerce=lambda x: x.lower() == "true" if x is not None else None, # coerce strings to bool, excepting None - choices=((True, "Yes"), (False, "No")), - initial=initial_value, - widget=forms.RadioSelect, - error_messages={ - "required": "This question is required.", # TODO-NL: (design check) - is this required? - }, - ) + # Note that these can be set in __init__ if you need more fine-grained control + form_is_checked = property( + lambda self: self.domain_request.has_anything_else_text() if self.domain_request else False + ) + typed_choice_field_name = "has_anything_else_text" class RequirementsForm(RegistrarForm):