mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-11-29 08:25:10 +00:00
networks: Improvements to interface binding
- Fetch network interfaces list at the time of form creation instead of during definition. - Add help text to interfaces list choice field. - Don't write an explict check for requiring interface selection. required=True, which is default, takes care of that. - Internationalize the empty selection text. - Update the empty selection text. - Re-order interface parameter in the same order of priority/appearance.
This commit is contained in:
1 parent
378d6b8d84
commit
acf3a919f6
3 files changed
+71
-75
No files matched your search
@@ -22,7 +22,17 @@ from gettext import gettext as _
|
||||
from plinth import network
|
||||
from gi.repository import NM as nm
|
||||
|
||||
DEFAULT_SELECT_MSG = 'please select'
|
||||
|
||||
def _get_interface_choices(device_type):
|
||||
"""Return a list of choices for a given device type."""
|
||||
interfaces = network.get_interface_list(device_type)
|
||||
choices = [('', _('-- select --'))]
|
||||
for interface, mac in interfaces.items():
|
||||
display_string = _('{interface} ({mac})').format(interface=interface,
|
||||
mac=mac)
|
||||
choices.append((interface, display_string))
|
||||
|
||||
return choices
|
||||
|
||||
|
||||
class ConnectionTypeSelectForm(forms.Form):
|
||||
@@ -35,17 +45,12 @@ class ConnectionTypeSelectForm(forms.Form):
|
||||
|
||||
class AddEthernetForm(forms.Form):
|
||||
"""Form to create a new ethernet connection."""
|
||||
interfaces = network.get_interface_list(nm.DeviceType.ETHERNET)
|
||||
interfaces_list = (('', DEFAULT_SELECT_MSG), )
|
||||
for interface, mac in interfaces.items():
|
||||
displaystring = str(interface + ' (' + mac + ')')
|
||||
newentry = interfaces_list + ((interface, displaystring), )
|
||||
interfaces_list = newentry
|
||||
|
||||
name = forms.CharField(label=_('Connection Name'))
|
||||
interface = forms.ChoiceField(
|
||||
label=_('Physical Interface'),
|
||||
choices=interfaces_list)
|
||||
choices=(),
|
||||
help_text=_('The network device that this connection should be bound '
|
||||
'to.'))
|
||||
zone = forms.ChoiceField(
|
||||
label=_('Firewall Zone'),
|
||||
help_text=_('The firewall zone will control which services are \
|
||||
@@ -61,26 +66,21 @@ available over this interfaces. Select Internal only for trusted networks.'),
|
||||
validators=[validators.validate_ipv4_address],
|
||||
required=False)
|
||||
|
||||
def clean(self):
|
||||
""" validate the form fields """
|
||||
cleaned_data = super(forms.Form, self).clean()
|
||||
if DEFAULT_SELECT_MSG == cleaned_data.get('interface'):
|
||||
raise forms.ValidationError('Please select a interface to be used')
|
||||
def __init__(self, *args, **kwargs):
|
||||
"""Initialize the form, populate interface choices."""
|
||||
super(AddEthernetForm, self).__init__(*args, **kwargs)
|
||||
choices = _get_interface_choices(nm.DeviceType.ETHERNET)
|
||||
self.fields['interface'].choices = choices
|
||||
|
||||
|
||||
class AddWifiForm(forms.Form):
|
||||
"""Form to create a new wifi connection."""
|
||||
interfaces = network.get_interface_list(nm.DeviceType.WIFI)
|
||||
interfaces_list = (('', DEFAULT_SELECT_MSG), )
|
||||
for interface, mac in interfaces.items():
|
||||
displaystring = str(interface + ' (' + mac + ')')
|
||||
newentry = interfaces_list + ((interface, displaystring), )
|
||||
interfaces_list = newentry
|
||||
|
||||
name = forms.CharField(label=_('Connection Name'))
|
||||
interface = forms.ChoiceField(
|
||||
label=_('Physical interface'),
|
||||
choices=interfaces_list)
|
||||
choices=(),
|
||||
help_text=_('The network device that this connection should be bound '
|
||||
'to.'))
|
||||
zone = forms.ChoiceField(
|
||||
label=_('Firewall Zone'),
|
||||
help_text=_('The firewall zone will control which services are \
|
||||
@@ -116,8 +116,8 @@ Point.'))
|
||||
validators=[validators.validate_ipv4_address],
|
||||
required=False)
|
||||
|
||||
def clean(self):
|
||||
""" validate the form fields """
|
||||
cleaned_data = super(forms.Form, self).clean()
|
||||
if DEFAULT_SELECT_MSG == cleaned_data.get('interface'):
|
||||
raise forms.ValidationError('Please select a interface to be used')
|
||||
def __init__(self, *args, **kwargs):
|
||||
"""Initialize the form, populate interface choices."""
|
||||
super(AddWifiForm, self).__init__(*args, **kwargs)
|
||||
choices = _get_interface_choices(nm.DeviceType.WIFI)
|
||||
self.fields['interface'].choices = choices
|
||||
@@ -75,16 +75,15 @@ def edit(request, uuid):
|
||||
|
||||
if form.is_valid():
|
||||
name = form.cleaned_data['name']
|
||||
interface = form.cleaned_data['interface']
|
||||
zone = form.cleaned_data['zone']
|
||||
ipv4_method = form.cleaned_data['ipv4_method']
|
||||
ipv4_address = form.cleaned_data['ipv4_address']
|
||||
interface = form.cleaned_data['interface']
|
||||
|
||||
if connection.get_connection_type() == '802-3-ethernet':
|
||||
network.edit_ethernet_connection(
|
||||
connection,
|
||||
name, zone,
|
||||
ipv4_method, ipv4_address, interface)
|
||||
connection, name, interface, zone, ipv4_method,
|
||||
ipv4_address)
|
||||
elif connection.get_connection_type() == '802-11-wireless':
|
||||
ssid = form.cleaned_data['ssid']
|
||||
mode = form.cleaned_data['mode']
|
||||
@@ -92,9 +91,8 @@ def edit(request, uuid):
|
||||
passphrase = form.cleaned_data['passphrase']
|
||||
|
||||
network.edit_wifi_connection(
|
||||
connection, name, zone,
|
||||
ssid, mode, auth_mode, passphrase,
|
||||
ipv4_method, ipv4_address, interface)
|
||||
connection, name, interface, zone, ssid, mode, auth_mode,
|
||||
passphrase, ipv4_method, ipv4_address)
|
||||
|
||||
return redirect(reverse_lazy('networks:index'))
|
||||
else:
|
||||
@@ -105,13 +103,12 @@ def edit(request, uuid):
|
||||
else:
|
||||
settings_connection = connection.get_setting_connection()
|
||||
settings_ipv4 = connection.get_setting_ip4_config()
|
||||
form_data['interface'] = connection.get_interface_name()
|
||||
try:
|
||||
form_data['zone'] = settings_connection.get_zone()
|
||||
except KeyError:
|
||||
form_data['zone'] = 'external'
|
||||
|
||||
form_data['interface'] = connection.get_interface_name()
|
||||
|
||||
form_data['ipv4_method'] = settings_ipv4.get_method()
|
||||
|
||||
if settings_ipv4.get_num_addresses():
|
||||
@@ -215,14 +212,13 @@ def add_ethernet(request):
|
||||
form = AddEthernetForm(request.POST)
|
||||
if form.is_valid():
|
||||
name = form.cleaned_data['name']
|
||||
interface = form.cleaned_data['interface']
|
||||
zone = form.cleaned_data['zone']
|
||||
ipv4_method = form.cleaned_data['ipv4_method']
|
||||
ipv4_address = form.cleaned_data['ipv4_address']
|
||||
interface = form.cleaned_data['interface']
|
||||
|
||||
network.add_ethernet_connection(
|
||||
name, zone,
|
||||
ipv4_method, ipv4_address, interface)
|
||||
name, interface, zone, ipv4_method, ipv4_address)
|
||||
return redirect(reverse_lazy('networks:index'))
|
||||
else:
|
||||
form = AddEthernetForm()
|
||||
@@ -250,6 +246,7 @@ def add_wifi(request, ssid=None):
|
||||
form = AddWifiForm(request.POST)
|
||||
if form.is_valid():
|
||||
name = form.cleaned_data['name']
|
||||
interface = form.cleaned_data['interface']
|
||||
zone = form.cleaned_data['zone']
|
||||
ssid = form.cleaned_data['ssid']
|
||||
mode = form.cleaned_data['mode']
|
||||
@@ -257,12 +254,10 @@ def add_wifi(request, ssid=None):
|
||||
passphrase = form.cleaned_data['passphrase']
|
||||
ipv4_method = form.cleaned_data['ipv4_method']
|
||||
ipv4_address = form.cleaned_data['ipv4_address']
|
||||
interface = form.cleaned_data['interface']
|
||||
|
||||
network.add_wifi_connection(
|
||||
name, zone,
|
||||
ssid, mode, auth_mode, passphrase,
|
||||
ipv4_method, ipv4_address, interface)
|
||||
name, interface, zone, ssid, mode, auth_mode, passphrase,
|
||||
ipv4_method, ipv4_address)
|
||||
return redirect(reverse_lazy('networks:index'))
|
||||
else:
|
||||
if form_data:
|
||||
|
||||
Reference in new issue
Block a user