Refactor some of the logic into form

It’s messy having a lot of logic in the view methods. Handling the form
stuff can be better encapsulated inside the `Form` subclass itself.
This commit is contained in:
Chris Hill-Scott
2018-08-31 17:31:26 +01:00
parent 80801d827a
commit d80b735b2b
3 changed files with 50 additions and 23 deletions

View File

@@ -682,15 +682,37 @@ class ServiceSwitchLettersForm(StripWhitespaceForm):
)
class BrandingStyle(RadioField):
def post_validate(self, form, validation_stopped):
if self.data == 'None':
self.data = None
class ServiceSetBranding(StripWhitespaceForm):
branding_style = RadioField(
branding_style = BrandingStyle(
'Branding style',
validators=[
DataRequired()
]
)
DEFAULT = ('None', 'GOV.UK')
def __init__(self, all_email_brandings, current_email_branding):
super().__init__(branding_style=current_email_branding)
self.branding_style.choices = sorted(
all_email_brandings + [self.DEFAULT],
key=lambda branding: (
branding[0] != current_email_branding,
branding[0] is not self.DEFAULT[0],
branding[1].lower(),
),
)
class ServicePreviewBranding(StripWhitespaceForm):