Move letter-specific logic down to its form class

For letters we still have the conditional reveal on the radio form
for the "something else" option.
This commit is contained in:
Ben Thorner
2022-03-22 16:39:59 +00:00
parent 352cdb80fa
commit f022836f4d

View File

@@ -2187,21 +2187,9 @@ class AdminSetOrganisationForm(StripWhitespaceForm):
class ChooseBrandingForm(StripWhitespaceForm):
FALLBACK_OPTION_VALUE = 'something_else'
FALLBACK_OPTION = (FALLBACK_OPTION_VALUE, 'Something else')
options = RadioField('Choose your new branding')
something_else = TextAreaField('Describe the branding you want')
def __init__(self, service, branding_type):
super().__init__()
self.branding_type = branding_type
self.options.choices = tuple(self.get_available_choices(service, branding_type))
self.options.label.text = 'Choose your new {} branding'.format(branding_type)
if self.something_else_is_only_option:
self.options.data = self.FALLBACK_OPTION_VALUE
@classmethod
def get_available_choices(cls, service, branding_type):
return (
@@ -2213,26 +2201,34 @@ class ChooseBrandingForm(StripWhitespaceForm):
def something_else_is_only_option(self):
return self.options.choices == (self.FALLBACK_OPTION,)
def validate_something_else(self, field):
if self.branding_type == 'letter':
if (
self.something_else_is_only_option
or self.options.data == self.FALLBACK_OPTION_VALUE
) and not field.data:
raise ValidationError('Cannot be empty')
if self.options.data != self.FALLBACK_OPTION_VALUE:
field.data = ''
class ChooseEmailBrandingForm(ChooseBrandingForm):
def __init__(self, service_id):
super().__init__(service_id, branding_type='email')
options = RadioField('Choose your new email branding')
def __init__(self, service):
super().__init__()
self.options.choices = tuple(self.get_available_choices(service, 'email'))
class ChooseLetterBrandingForm(ChooseBrandingForm):
def __init__(self, service_id):
super().__init__(service_id, branding_type='letter')
options = RadioField('Choose your new letter branding')
something_else = TextAreaField('Describe the branding you want')
def __init__(self, service):
super().__init__()
self.options.choices = tuple(self.get_available_choices(service, 'letter'))
if self.something_else_is_only_option:
self.options.data = self.FALLBACK_OPTION_VALUE
def validate_something_else(self, field):
if (
self.something_else_is_only_option
or self.options.data == self.FALLBACK_OPTION_VALUE
) and not field.data:
raise ValidationError('Cannot be empty')
if self.options.data != self.FALLBACK_OPTION_VALUE:
field.data = ''
class SomethingElseBrandingForm(StripWhitespaceForm):