mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-07-14 18:30:15 -04:00
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:
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user