From b917fa3c7410671722b2499cf13d95cd693a24cc Mon Sep 17 00:00:00 2001 From: Tom Byers Date: Fri, 26 Jun 2020 22:18:24 +0100 Subject: [PATCH] Make OnOffField inherit from GovukRadiosField Means that ServiceOnOffSettingForm.enabled ends up using the GovukRadiosField methods for rendering. --- app/main/forms.py | 64 +++++++++++++++++++++++------------------------ 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/app/main/forms.py b/app/main/forms.py index 5d2709d77..70274168e 100644 --- a/app/main/forms.py +++ b/app/main/forms.py @@ -593,38 +593,6 @@ class PostalAddressField(TextAreaField): self.data = PostalAddress(valuelist[0]).normalised -class OnOffField(RadioField): - - def __init__(self, label, choices=None, *args, **kwargs): - choices = choices or [ - (True, 'On'), - (False, 'Off'), - ] - super().__init__( - label, - choices=choices, - thing=f'{choices[0][1].lower()} or {choices[1][1].lower()}', - *args, - **kwargs, - ) - - def process_formdata(self, valuelist): - if valuelist: - value = valuelist[0] - self.data = (value == 'True') if value in ['True', 'False'] else value - - def iter_choices(self): - for value, label in self.choices: - # This overrides WTForms default behaviour which is to check - # self.coerce(value) == self.data - # where self.coerce returns a string for a boolean input - yield ( - value, - label, - (self.data in {value, self.coerce(value)}) - ) - - class LoginForm(StripWhitespaceForm): email_address = GovukEmailField('Email address', validators=[ Length(min=5, max=255), @@ -951,6 +919,38 @@ class GovukRadiosField(RadioField): return govuk_radios_field_widget(self, field, param_extensions=param_extensions, **kwargs) +class OnOffField(GovukRadiosField): + + def __init__(self, label, choices=None, *args, **kwargs): + choices = choices or [ + (True, 'On'), + (False, 'Off'), + ] + super().__init__( + label, + choices=choices, + thing=f'{choices[0][1].lower()} or {choices[1][1].lower()}', + *args, + **kwargs, + ) + + def process_formdata(self, valuelist): + if valuelist: + value = valuelist[0] + self.data = (value == 'True') if value in ['True', 'False'] else value + + def iter_choices(self): + for value, label in self.choices: + # This overrides WTForms default behaviour which is to check + # self.coerce(value) == self.data + # where self.coerce returns a string for a boolean input + yield ( + value, + label, + (self.data in {value, self.coerce(value)}) + ) + + # guard against data entries that aren't a role in permissions def filter_by_permissions(valuelist): if valuelist is None: