From 1c69b6cdb62831b3058fa3126f0069c700151cc9 Mon Sep 17 00:00:00 2001 From: James Valleroy Date: Mon, 9 Mar 2015 19:28:04 -0400 Subject: [PATCH] Set auto or static IPv4 address. --- plinth/modules/network/forms.py | 51 +++++++++++++++++++ plinth/modules/network/network.py | 48 ++++++++--------- .../network/templates/connections_create.html | 17 ++++++- .../templates/connections_type_select.html | 44 ++++++++++++++++ 4 files changed, 132 insertions(+), 28 deletions(-) create mode 100644 plinth/modules/network/forms.py create mode 100644 plinth/modules/network/templates/connections_type_select.html diff --git a/plinth/modules/network/forms.py b/plinth/modules/network/forms.py new file mode 100644 index 000000000..f8780934b --- /dev/null +++ b/plinth/modules/network/forms.py @@ -0,0 +1,51 @@ +# +# This file is part of Plinth. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# + +from django import forms +from gettext import gettext as _ + + +CONNECTION_TYPE_NAMES = { + '802-3-ethernet': 'Ethernet', + '802-11-wireless': 'Wi-Fi', +} + + +class ConnectionTypeSelectForm(forms.Form): + """Form to select type for new connection.""" + conn_type = forms.ChoiceField( + label=_('Connection Type'), + choices=[(k, v) for k, v in CONNECTION_TYPE_NAMES.items()]) + + +class AddEthernetForm(forms.Form): + """Form to create a new ethernet connection.""" + name = forms.CharField(label=_('Connection Name')) + ipv4_method = forms.ChoiceField( + label=_('IPv4 Addressing Method'), + choices=[('auto', 'Automatic (DHCP)'), ('manual', 'Manual')]) + ipv4_address = forms.CharField(label=_('Address'), required=False) + + +class AddWifiForm(forms.Form): + """Form to create a new wifi connection.""" + name = forms.CharField(label=_('Connection Name')) + ssid = forms.CharField(label=_('SSID')) + ipv4_method = forms.ChoiceField( + label=_('IPv4 Addressing Method'), + choices=[('auto', 'Automatic (DHCP)'), ('manual', 'Manual')]) + ipv4_address = forms.CharField(label=_('Address'), required=False) diff --git a/plinth/modules/network/network.py b/plinth/modules/network/network.py index f297af025..6e67c1a28 100644 --- a/plinth/modules/network/network.py +++ b/plinth/modules/network/network.py @@ -16,7 +16,6 @@ # from dbus.exceptions import DBusException -from django import forms from django.contrib import messages from django.contrib.auth.decorators import login_required from django.core.urlresolvers import reverse_lazy @@ -27,6 +26,8 @@ import NetworkManager import uuid import urllib +from .forms import CONNECTION_TYPE_NAMES, ConnectionTypeSelectForm, \ + AddEthernetForm, AddWifiForm from plinth import cfg @@ -35,11 +36,6 @@ subsubmenu = [{'url': reverse_lazy('network:index'), {'url': reverse_lazy('network:add'), 'text': _('Add Connection')}] -CONNECTION_TYPE_NAMES = { - '802-3-ethernet': 'Ethernet', - '802-11-wireless': 'Wi-Fi', -} - def init(): """Initialize the Network module.""" @@ -140,20 +136,13 @@ def deactivate(request, conn_id): return redirect(reverse_lazy('network:index')) -class ConnectionAddForm(forms.Form): - """Form to select type for new connection.""" - conn_type = forms.ChoiceField( - label=_('Connection Type'), - choices=[(k, v) for k, v in CONNECTION_TYPE_NAMES.items()]) - - @login_required def add(request): """Serve the connection type selection form.""" form = None if request.method == 'POST': - form = ConnectionAddForm(request.POST) + form = ConnectionTypeSelectForm(request.POST) if form.is_valid(): conn_type = form.cleaned_data['conn_type'] if conn_type == '802-3-ethernet': @@ -161,18 +150,13 @@ def add(request): elif conn_type == '802-11-wireless': return redirect(reverse_lazy('network:add_wifi')) else: - form = ConnectionAddForm() - return TemplateResponse(request, 'connections_add.html', + form = ConnectionTypeSelectForm() + return TemplateResponse(request, 'connections_type_select.html', {'title': _('Add Connection'), 'subsubmenu': subsubmenu, 'form': form}) -class AddEthernetForm(forms.Form): - """Form to create a new ethernet connection.""" - name = forms.CharField(label=_('Connection Name')) - - @login_required def add_ethernet(request): """Serve ethernet connection create form.""" @@ -188,7 +172,15 @@ def add_ethernet(request): 'uuid': str(uuid.uuid4()), }, '802-3-ethernet': {}, + 'ipv4': {'method': form.cleaned_data['ipv4_method']}, } + + if form.cleaned_data['ipv4_method'] == 'manual': + conn['ipv4']['addresses'] = [ + (form.cleaned_data['ipv4_address'], + 24, # CIDR prefix length + '0.0.0.0')] # gateway + NetworkManager.Settings.AddConnection(conn) return redirect(reverse_lazy('network:index')) else: @@ -200,12 +192,6 @@ def add_ethernet(request): 'form': form}) -class AddWifiForm(forms.Form): - """Form to create a new wifi connection.""" - name = forms.CharField(label=_('Connection Name')) - ssid = forms.CharField(label=_('SSID')) - - @login_required def add_wifi(request): """Serve wifi connection create form.""" @@ -223,7 +209,15 @@ def add_wifi(request): '802-11-wireless': { 'ssid': form.cleaned_data['ssid'], }, + 'ipv4': {'method': form.cleaned_data['ipv4_method']}, } + + if form.cleaned_data['ipv4_method'] == 'manual': + conn['ipv4']['addresses'] = [ + (form.cleaned_data['ipv4_address'], + 24, # CIDR prefix length + '0.0.0.0')] # gateway + NetworkManager.Settings.AddConnection(conn) return redirect(reverse_lazy('network:index')) else: diff --git a/plinth/modules/network/templates/connections_create.html b/plinth/modules/network/templates/connections_create.html index 98adcae23..2d562d1d9 100644 --- a/plinth/modules/network/templates/connections_create.html +++ b/plinth/modules/network/templates/connections_create.html @@ -38,7 +38,22 @@ {% endblock %} {% block page_js %} - + {% endblock %} diff --git a/plinth/modules/network/templates/connections_type_select.html b/plinth/modules/network/templates/connections_type_select.html new file mode 100644 index 000000000..153dc2ba0 --- /dev/null +++ b/plinth/modules/network/templates/connections_type_select.html @@ -0,0 +1,44 @@ +{% extends "base.html" %} +{% comment %} +# +# This file is part of Plinth. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +{% endcomment %} + +{% load bootstrap %} + +{% block content %} + +
+
+
+ {% csrf_token %} + + {{ form|bootstrap }} + + + +
+
+
+ +{% endblock %} + +{% block page_js %} + +{% endblock %}