mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-09-10 18:22:37 -04:00
Merge pull request #3919 from alphagov/refactor-broadcast-settings
Refactor broadcast settings forms
This commit is contained in:
+37
-54
@@ -881,6 +881,13 @@ class GovukRadiosField(RadioField):
|
||||
return govuk_radios_field_widget(self, field, param_extensions=param_extensions, **kwargs)
|
||||
|
||||
|
||||
class OptionalGovukRadiosField(GovukRadiosField):
|
||||
def pre_validate(self, form):
|
||||
if self.data is None:
|
||||
return
|
||||
super().pre_validate(form)
|
||||
|
||||
|
||||
class OnOffField(GovukRadiosField):
|
||||
|
||||
def __init__(self, label, choices=None, *args, **kwargs):
|
||||
@@ -2358,23 +2365,6 @@ class GoLiveNotesForm(StripWhitespaceForm):
|
||||
|
||||
|
||||
class ServiceBroadcastAccountTypeField(GovukRadiosField):
|
||||
# When receiving Python data, eg when instantiating the form object
|
||||
# we want to convert it from a tuple of
|
||||
# (service_mode, broadcast_channel, allowed_broadcast_provider)
|
||||
# to a value to be used in our form such as "live-severe-ee"
|
||||
def process_data(self, value):
|
||||
if not value or isinstance(value, str):
|
||||
return super().process_data(value)
|
||||
(live, broadcast_channel, allowed_broadcast_provider) = value
|
||||
account_type = None
|
||||
if broadcast_channel:
|
||||
account_type = "live" if live else "training"
|
||||
account_type += f"-{broadcast_channel}"
|
||||
if allowed_broadcast_provider != 'all':
|
||||
account_type += f"-{allowed_broadcast_provider}"
|
||||
|
||||
self.data = account_type
|
||||
|
||||
# After validation we split the value back into its parts of service_mode
|
||||
# broadcast_channel and provider_restriction to be used by the flask route to send to the
|
||||
# API
|
||||
@@ -2383,25 +2373,18 @@ class ServiceBroadcastAccountTypeField(GovukRadiosField):
|
||||
split_values = self.data.split("-")
|
||||
self.service_mode = split_values[0]
|
||||
self.broadcast_channel = split_values[1]
|
||||
self.provider_restriction = split_values[2] if len(split_values) == 3 else 'all'
|
||||
|
||||
|
||||
class OptionalServiceBroadcastAccountTypeField(ServiceBroadcastAccountTypeField):
|
||||
def pre_validate(self, form):
|
||||
if self.data is None:
|
||||
return
|
||||
super().pre_validate(form)
|
||||
self.provider_restriction = split_values[2]
|
||||
|
||||
|
||||
class ServiceBroadcastChannelForm(StripWhitespaceForm):
|
||||
channel = ServiceBroadcastAccountTypeField(
|
||||
channel = GovukRadiosField(
|
||||
'Emergency alerts settings',
|
||||
thing='mode or channel',
|
||||
choices=[
|
||||
("training-test", "Training mode"),
|
||||
("live-test", "Test channel"),
|
||||
("live-severe", "Live channel"),
|
||||
("live-government", "Government channel"),
|
||||
("training", "Training mode"),
|
||||
("test", "Test channel"),
|
||||
("severe", "Live channel"),
|
||||
("government", "Government channel"),
|
||||
],
|
||||
)
|
||||
|
||||
@@ -2411,32 +2394,36 @@ class ServiceBroadcastNetworkForm(StripWhitespaceForm):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.broadcast_channel = broadcast_channel
|
||||
|
||||
self.network_variant.choices = [
|
||||
(f'live-{broadcast_channel}', 'All networks'),
|
||||
('', 'A single network'),
|
||||
]
|
||||
|
||||
self.network.choices = [
|
||||
(f'live-{broadcast_channel}-ee', 'EE'),
|
||||
(f'live-{broadcast_channel}-o2', 'O2'),
|
||||
(f'live-{broadcast_channel}-vodafone', 'Vodafone'),
|
||||
(f'live-{broadcast_channel}-three', 'Three'),
|
||||
]
|
||||
|
||||
network_variant = ServiceBroadcastAccountTypeField(
|
||||
all_networks = OnOffField(
|
||||
'Choose a mobile network',
|
||||
thing='a mobile network',
|
||||
choices=(
|
||||
(True, 'All networks'),
|
||||
(False, 'A single network')
|
||||
),
|
||||
)
|
||||
network = OptionalServiceBroadcastAccountTypeField(
|
||||
network = OptionalGovukRadiosField(
|
||||
'Choose a mobile network',
|
||||
thing='a mobile network',
|
||||
choices=(
|
||||
('ee', 'EE'),
|
||||
('o2', 'O2'),
|
||||
('vodafone', 'Vodafone'),
|
||||
('three', 'Three'),
|
||||
),
|
||||
)
|
||||
|
||||
@property
|
||||
def account_type(self):
|
||||
if self.all_networks.data:
|
||||
provider = 'all'
|
||||
else:
|
||||
provider = self.network.data
|
||||
|
||||
return f'live-{self.broadcast_channel}-{provider}'
|
||||
|
||||
def validate_network(self, field):
|
||||
if not self.network_variant.data and not field.data:
|
||||
if not self.all_networks.data and not field.data:
|
||||
raise ValidationError('Select a mobile network')
|
||||
if self.network_variant.data == 'all':
|
||||
field.data = ''
|
||||
|
||||
|
||||
class ServiceBroadcastAccountTypeForm(StripWhitespaceForm):
|
||||
@@ -2444,16 +2431,12 @@ class ServiceBroadcastAccountTypeForm(StripWhitespaceForm):
|
||||
'Change cell broadcast service type',
|
||||
thing='which type of account this cell broadcast service is',
|
||||
choices=[
|
||||
("training-test", "")
|
||||
] +
|
||||
[
|
||||
(f"live-{broadcast_channel}", "")
|
||||
for broadcast_channel in ["test", "severe", "government"]
|
||||
("training-test-all", "")
|
||||
] +
|
||||
[
|
||||
(f"live-{broadcast_channel}-{provider}", "")
|
||||
for broadcast_channel in ["test", "severe", "government"]
|
||||
for provider in ["ee", "o2", "three", "vodafone"]
|
||||
for provider in ["all", "ee", "o2", "three", "vodafone"]
|
||||
],
|
||||
validators=[DataRequired()]
|
||||
)
|
||||
|
||||
@@ -321,25 +321,27 @@ def service_set_permission(service_id, permission):
|
||||
@main.route("/services/<uuid:service_id>/service-settings/broadcasts", methods=["GET", "POST"])
|
||||
@user_is_platform_admin
|
||||
def service_set_broadcast_channel(service_id):
|
||||
form = ServiceBroadcastChannelForm(
|
||||
channel=(
|
||||
current_service.live,
|
||||
current_service.broadcast_channel,
|
||||
'all',
|
||||
)
|
||||
)
|
||||
if current_service.has_permission('broadcast'):
|
||||
if current_service.live:
|
||||
channel = current_service.broadcast_channel
|
||||
else:
|
||||
channel = 'training'
|
||||
else:
|
||||
channel = None
|
||||
|
||||
form = ServiceBroadcastChannelForm(channel=channel)
|
||||
|
||||
if form.validate_on_submit():
|
||||
if form.channel.service_mode == 'training':
|
||||
if form.channel.data == 'training':
|
||||
return redirect(url_for(
|
||||
'.service_confirm_broadcast_account_type',
|
||||
service_id=current_service.id,
|
||||
account_type=form.channel.data,
|
||||
account_type='training-test-all'
|
||||
))
|
||||
return redirect(url_for(
|
||||
'.service_set_broadcast_network',
|
||||
service_id=current_service.id,
|
||||
broadcast_channel=form.channel.broadcast_channel,
|
||||
broadcast_channel=form.channel.data,
|
||||
))
|
||||
|
||||
return render_template(
|
||||
@@ -353,25 +355,13 @@ def service_set_broadcast_channel(service_id):
|
||||
def service_set_broadcast_network(service_id, broadcast_channel):
|
||||
# only populate old settings when the channel is unchanged
|
||||
if current_service.broadcast_channel == broadcast_channel:
|
||||
if current_service.allowed_broadcast_provider == 'all':
|
||||
form = ServiceBroadcastNetworkForm(
|
||||
broadcast_channel=broadcast_channel,
|
||||
network_variant=(
|
||||
current_service.live,
|
||||
current_service.broadcast_channel,
|
||||
current_service.allowed_broadcast_provider,
|
||||
),
|
||||
)
|
||||
else:
|
||||
form = ServiceBroadcastNetworkForm(
|
||||
broadcast_channel=broadcast_channel,
|
||||
network_variant='',
|
||||
network=(
|
||||
current_service.live,
|
||||
current_service.broadcast_channel,
|
||||
current_service.allowed_broadcast_provider
|
||||
)
|
||||
)
|
||||
provider = current_service.allowed_broadcast_provider
|
||||
|
||||
form = ServiceBroadcastNetworkForm(
|
||||
broadcast_channel=broadcast_channel,
|
||||
all_networks=provider == 'all',
|
||||
network=provider if provider != 'all' else None,
|
||||
)
|
||||
else:
|
||||
form = ServiceBroadcastNetworkForm(
|
||||
broadcast_channel=broadcast_channel
|
||||
@@ -381,7 +371,7 @@ def service_set_broadcast_network(service_id, broadcast_channel):
|
||||
return redirect(url_for(
|
||||
'.service_confirm_broadcast_account_type',
|
||||
service_id=current_service.id,
|
||||
account_type=form.network_variant.data or form.network.data,
|
||||
account_type=form.account_type,
|
||||
))
|
||||
|
||||
return render_template(
|
||||
|
||||
Reference in New Issue
Block a user