mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-09-02 17:48:50 -04:00
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:
@@ -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(
|
||||
|
||||
@@ -54,23 +54,21 @@ def edit_sms_provider_ratio():
|
||||
and provider['active']
|
||||
], key=itemgetter('identifier'))
|
||||
|
||||
form = AdminProviderRatioForm(ratio=providers[0]['priority'])
|
||||
form = AdminProviderRatioForm(providers)
|
||||
|
||||
if len(providers) < 2:
|
||||
abort(400)
|
||||
|
||||
first_provider, second_provider = providers[0:2]
|
||||
|
||||
if form.validate_on_submit():
|
||||
provider_client.update_provider(first_provider['id'], form.percentage_left)
|
||||
provider_client.update_provider(second_provider['id'], form.percentage_right)
|
||||
for provider in providers:
|
||||
field = getattr(form, provider['identifier'])
|
||||
provider_client.update_provider(provider['id'], field.data)
|
||||
return redirect(url_for('.view_providers'))
|
||||
|
||||
return render_template(
|
||||
'views/providers/edit-sms-provider-ratio.html',
|
||||
form=form,
|
||||
first_provider=providers[0]['display_name'],
|
||||
second_provider=providers[1]['display_name'],
|
||||
providers=providers
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user