Rewrite SMS ratio form to cope with 3 providers

This replaces the slider with an integer input for each provider.
Unfortunately showing a variable number of inputs isn't easy to
achieve in WTForms [^1], but we think this is the least worst way
to do it vs e.g. not using WTForms at all.

[^1]: https://github.com/wtforms/wtforms/issues/736
This commit is contained in:
Ben Thorner
2022-04-06 15:08:28 +01:00
parent 01389b5edf
commit 7f333ba5fe
5 changed files with 46 additions and 132 deletions

View File

@@ -1740,29 +1740,26 @@ class EstimateUsageForm(StripWhitespaceForm):
return super().validate(*args, **kwargs)
class AdminProviderRatioForm(StripWhitespaceForm):
class AdminProviderRatioForm(Form):
def __init__(self, providers):
# hack: https://github.com/wtforms/wtforms/issues/736
self._unbound_fields = [
(
provider['identifier'],
GovukIntegerField(
provider['display_name'],
validators=[validators.NumberRange(
min=0, max=100, message="Must be between 0 and 100"
)]
)
) for provider in providers
]
ratio = GovukRadiosField(choices=[
(str(value), '{}% / {}%'.format(value, 100 - value))
for value in range(100, -10, -10)
],
param_extensions={
"classes": "govuk-radios--inline",
"fieldset": {
"legend": {
"classes": "govuk-visually-hidden"
}
}
super().__init__(data={
provider['identifier']: provider['priority']
for provider in providers
})
@property
def percentage_left(self):
return int(self.ratio.data)
@property
def percentage_right(self):
return 100 - self.percentage_left
class ServiceContactDetailsForm(StripWhitespaceForm):
contact_details_type = RadioField(