Merge pull request 338 from SunilMohanAdapa:

Refactoring of networks module.
This commit is contained in:
Daniel Steglich committed 2016-01-01 22:00:28 +00:00
commit de2d2323d4
3 files changed
+163 -301

No files matched your search

+91 -86
View File
@@ -24,18 +24,6 @@ from plinth.utils import import_from_gi
nm = import_from_gi('NM', '1.0')
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):
"""Form to select type for new connection."""
connection_type = forms.ChoiceField(
@@ -44,8 +32,8 @@ class ConnectionTypeSelectForm(forms.Form):
for key, value in network.CONNECTION_TYPE_NAMES.items()])
class AddEthernetForm(forms.Form):
"""Form to create a new ethernet connection."""
class ConnectionForm(forms.Form):
"""Base form to create/edit a connection."""
name = forms.CharField(label=_('Connection Name'))
interface = forms.ChoiceField(
label=_('Physical Interface'),
@@ -94,53 +82,95 @@ available over this interfaces. Select Internal only for trusted networks.'),
validators=[validators.validate_ipv4_address],
required=False)
@staticmethod
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
def get_settings(self):
"""Return settings dict from cleaned data."""
settings = {}
settings['common'] = {
'name': self.cleaned_data['name'],
'interface': self.cleaned_data['interface'],
'zone': self.cleaned_data['zone'],
}
settings['ipv4'] = self.get_ipv4_settings()
return settings
def get_ipv4_settings(self):
"""Return IPv4 dict from cleaned data."""
ipv4 = {
'method': self.cleaned_data['ipv4_method'],
'address': self.cleaned_data['ipv4_address'],
'netmask': self.cleaned_data['ipv4_netmask'],
'gateway': self.cleaned_data['ipv4_gateway'],
'dns': self.cleaned_data['ipv4_dns'],
'second_dns': self.cleaned_data['ipv4_second_dns'],
}
return ipv4
class EthernetForm(ConnectionForm):
"""Form to create/edit a ethernet connection."""
def __init__(self, *args, **kwargs):
"""Initialize the form, populate interface choices."""
super(AddEthernetForm, self).__init__(*args, **kwargs)
choices = _get_interface_choices(nm.DeviceType.ETHERNET)
super(EthernetForm, self).__init__(*args, **kwargs)
choices = self._get_interface_choices(nm.DeviceType.ETHERNET)
self.fields['interface'].choices = choices
def get_settings(self):
"""Return settings dict from cleaned data."""
settings = super().get_settings()
settings['common']['type'] = '802-3-ethernet'
return settings
class AddPPPoEForm(forms.Form):
class PPPoEForm(EthernetForm):
"""Form to create a new PPPoE connection."""
name = forms.CharField(label=_('Connection Name'))
interface = forms.ChoiceField(
label=_('Physical Interface'),
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 '
'available over this interfaces. Select Internal only '
'for trusted networks.'),
choices=[('external', 'External'), ('internal', 'Internal')])
ipv4_method = None
ipv4_address = None
ipv4_netmask = None
ipv4_gateway = None
ipv4_dns = None
ipv4_second_dns = None
username = forms.CharField(label=_('Username'))
password = forms.CharField(label=_('Password'),
widget=forms.PasswordInput(render_value=True))
show_password = forms.BooleanField(label=_('Show password'),
required=False)
def __init__(self, *args, **kwargs):
"""Initialize the form, populate interface choices."""
super(AddPPPoEForm, self).__init__(*args, **kwargs)
choices = _get_interface_choices(nm.DeviceType.ETHERNET)
self.fields['interface'].choices = choices
def get_settings(self):
"""Return setting dict from cleaned data."""
settings = super().get_settings()
settings['common']['type'] = 'pppoe'
settings['pppoe'] = {
'username': self.cleaned_data['username'],
'password': self.cleaned_data['password'],
}
return settings
def get_ipv4_settings(self):
"""Return IPv4 settings from cleaned data."""
return None
class AddWifiForm(forms.Form):
"""Form to create a new Wi-Fi connection."""
name = forms.CharField(label=_('Connection Name'))
interface = forms.ChoiceField(
label=_('Physical Interface'),
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 \
available over this interfaces. Select Internal only for trusted networks.'),
choices=[('external', 'External'), ('internal', 'Internal')])
class WifiForm(ConnectionForm):
"""Form to create/edit a Wi-Fi connection."""
field_order = ['name', 'interface', 'zone', 'ssid', 'mode', 'auth_mode',
'passphrase', 'ipv4_method', 'ipv4_address', 'ipv4_netmask',
'ipv4_gateway', 'ipv4_dns', 'ipv4_second_dns']
ssid = forms.CharField(
label=_('SSID'),
help_text=_('The visible name of the network.'))
@@ -158,46 +188,21 @@ requires clients to have the password to connect.'),
label=_('Passphrase'),
validators=[validators.MinLengthValidator(8)],
required=False)
ipv4_method = forms.ChoiceField(
label=_('IPv4 Addressing Method'),
choices=[('auto', 'Automatic (DHCP)'),
('shared', 'Shared'),
('manual', 'Manual')],
help_text=_('Select Automatic (DHCP) if you are connecting to an \
existing wireless network. Shared mode is useful when running an Access \
Point.'))
ipv4_address = forms.CharField(
label=_('Address'),
validators=[validators.validate_ipv4_address],
required=False)
ipv4_netmask = forms.CharField(
label=_('Netmask'),
help_text=_('Optional value. If left blank, a default netmask '
'based on the address will be used.'),
validators=[validators.validate_ipv4_address],
required=False)
ipv4_gateway = forms.CharField(
label=_('Gateway'),
help_text=_('Optional value.'),
validators=[validators.validate_ipv4_address],
required=False)
ipv4_dns = forms.CharField(
label=_('DNS Server'),
help_text=_('Optional value. If this value is given and IPv4 '
'addressing method is "Automatic", the DNS Servers '
'provided by a DHCP server will be ignored.'),
validators=[validators.validate_ipv4_address],
required=False)
ipv4_second_dns = forms.CharField(
label=_('Second DNS Server'),
help_text=_('Optional value. If this value is given and IPv4 '
'Addressing Method is "Automatic", the DNS Servers '
'provided by a DHCP server will be ignored.'),
validators=[validators.validate_ipv4_address],
required=False)
def __init__(self, *args, **kwargs):
"""Initialize the form, populate interface choices."""
super(AddWifiForm, self).__init__(*args, **kwargs)
choices = _get_interface_choices(nm.DeviceType.WIFI)
super(WifiForm, self).__init__(*args, **kwargs)
choices = self._get_interface_choices(nm.DeviceType.WIFI)
self.fields['interface'].choices = choices
def get_settings(self):
"""Return setting dict from cleaned data."""
settings = super().get_settings()
settings['common']['type'] = '802-11-wireless'
settings['wireless'] = {
'ssid': self.cleaned_data['ssid'],
'mode': self.cleaned_data['mode'],
'auth_mode': self.cleaned_data['auth_mode'],
'passphrase': self.cleaned_data['passphrase'],
}
return settings
+19 -86
View File
@@ -23,8 +23,8 @@ from django.utils.translation import ugettext as _, ugettext_lazy
from django.views.decorators.http import require_POST
from logging import Logger
from .forms import (ConnectionTypeSelectForm, AddEthernetForm, AddPPPoEForm,
AddWifiForm)
from .forms import (ConnectionTypeSelectForm, EthernetForm, PPPoEForm,
WifiForm)
from plinth import cfg
from plinth import network
from plinth import package
@@ -124,45 +124,14 @@ def edit(request, uuid):
if request.method == 'POST':
if connection.get_connection_type() == '802-11-wireless':
form = AddWifiForm(request.POST)
form = WifiForm(request.POST)
elif connection.get_connection_type() == '802-3-ethernet':
form = AddEthernetForm(request.POST)
form = EthernetForm(request.POST)
elif connection.get_connection_type() == 'pppoe':
form = AddPPPoEForm(request.POST)
form = PPPoEForm(request.POST)
if form.is_valid():
name = form.cleaned_data['name']
interface = form.cleaned_data['interface']
zone = form.cleaned_data['zone']
if connection.get_connection_type() == 'pppoe':
username = form.cleaned_data['username']
password = form.cleaned_data['password']
else:
ipv4_method = form.cleaned_data['ipv4_method']
ipv4_address = form.cleaned_data['ipv4_address']
ipv4_netmask = form.cleaned_data['ipv4_netmask']
ipv4_gateway = form.cleaned_data['ipv4_gateway']
ipv4_dns = form.cleaned_data['ipv4_dns']
ipv4_second_dns = form.cleaned_data['ipv4_second_dns']
if connection.get_connection_type() == '802-3-ethernet':
network.edit_ethernet_connection(
connection, name, interface, zone, ipv4_method,
ipv4_address, ipv4_netmask, ipv4_gateway, ipv4_dns,
ipv4_second_dns)
elif connection.get_connection_type() == '802-11-wireless':
ssid = form.cleaned_data['ssid']
mode = form.cleaned_data['mode']
auth_mode = form.cleaned_data['auth_mode']
passphrase = form.cleaned_data['passphrase']
network.edit_wifi_connection(
connection, name, interface, zone, ssid, mode, auth_mode,
passphrase, ipv4_method, ipv4_address, ipv4_netmask,
ipv4_gateway, ipv4_dns, ipv4_second_dns)
elif connection.get_connection_type() == 'pppoe':
network.edit_pppoe_connection(
connection, name, interface, zone, username, password)
network.edit_connection(connection, form.get_settings())
return redirect(reverse_lazy('networks:index'))
else:
@@ -215,15 +184,15 @@ def edit(request, uuid):
except KeyError:
form_data['auth_mode'] = 'open'
form = AddWifiForm(form_data)
form = WifiForm(form_data)
elif settings_connection.get_connection_type() == '802-3-ethernet':
form = AddEthernetForm(form_data)
form = EthernetForm(form_data)
elif settings_connection.get_connection_type() == 'pppoe':
settings_pppoe = connection.get_setting_pppoe()
form_data['username'] = settings_pppoe.get_username()
secrets = connection.get_secrets('pppoe')
form_data['password'] = secrets['pppoe']['password']
form = AddPPPoEForm(form_data)
form = PPPoEForm(form_data)
return TemplateResponse(request, 'connections_edit.html',
{'title': _('Edit Connection'),
@@ -302,24 +271,12 @@ def add_ethernet(request):
form = None
if request.method == 'POST':
form = AddEthernetForm(request.POST)
form = EthernetForm(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']
ipv4_netmask = form.cleaned_data['ipv4_netmask']
ipv4_gateway = form.cleaned_data['ipv4_gateway']
ipv4_dns = form.cleaned_data['ipv4_dns']
ipv4_second_dns = form.cleaned_data['ipv4_second_dns']
network.add_ethernet_connection(
name, interface, zone, ipv4_method, ipv4_address, ipv4_netmask,
ipv4_gateway, ipv4_dns, ipv4_second_dns)
network.add_connection(form.get_settings())
return redirect(reverse_lazy('networks:index'))
else:
form = AddEthernetForm()
form = EthernetForm()
return TemplateResponse(request, 'connections_create.html',
{'title': _('Adding New Ethernet Connection'),
@@ -332,19 +289,12 @@ def add_pppoe(request):
form = None
if request.method == 'POST':
form = AddPPPoEForm(request.POST)
form = PPPoEForm(request.POST)
if form.is_valid():
name = form.cleaned_data['name']
interface = form.cleaned_data['interface']
zone = form.cleaned_data['zone']
username = form.cleaned_data['username']
password = form.cleaned_data['password']
network.add_pppoe_connection(
name, interface, zone, username, password)
network.add_connection(form.get_settings())
return redirect(reverse_lazy('networks:index'))
else:
form = AddPPPoEForm()
form = PPPoEForm()
return TemplateResponse(request, 'connections_create.html',
{'title': _('Adding New PPPoE Connection'),
@@ -368,32 +318,15 @@ def add_wifi(request, ssid=None, interface_name=None):
'ipv4_method': 'auto'}
if request.method == 'POST':
form = AddWifiForm(request.POST)
form = WifiForm(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']
auth_mode = form.cleaned_data['auth_mode']
passphrase = form.cleaned_data['passphrase']
ipv4_method = form.cleaned_data['ipv4_method']
ipv4_address = form.cleaned_data['ipv4_address']
ipv4_netmask = form.cleaned_data['ipv4_netmask']
ipv4_gateway = form.cleaned_data['ipv4_gateway']
ipv4_dns = form.cleaned_data['ipv4_dns']
ipv4_second_dns = form.cleaned_data['ipv4_second_dns']
network.add_wifi_connection(
name, interface, zone, ssid, mode, auth_mode, passphrase,
ipv4_method, ipv4_address, ipv4_netmask, ipv4_gateway,
ipv4_dns, ipv4_second_dns)
network.add_connection(form.get_settings())
return redirect(reverse_lazy('networks:index'))
else:
if form_data:
form = AddWifiForm(form_data)
form = WifiForm(form_data)
else:
form = AddWifiForm()
form = WifiForm()
return TemplateResponse(request, 'connections_create.html',
{'title': _('Adding New Wi-Fi Connection'),