Set auto or static IPv4 address.

This commit is contained in:
James Valleroy 2015-03-09 19:28:04 -04:00 committed by Sunil Mohan Adapa
parent 0f99de9347
commit 1c69b6cdb6
4 changed files with 132 additions and 28 deletions

View File

@ -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 <http://www.gnu.org/licenses/>.
#
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)

View File

@ -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:

View File

@ -38,7 +38,22 @@
{% endblock %}
{% block page_js %}
<script>
<script type="text/javascript">
(function($) {
$('#id_ipv4_address').prop("readOnly", true);
$('#id_name').focus();
$('#id_ipv4_method').change(function() {
if ($('#id_ipv4_method').prop('value') == 'manual') {
$('#id_ipv4_address').prop("readOnly", false);
} else {
$('#id_ipv4_address').prop("readOnly", true);
}
});
})(jQuery);
</script>
{% endblock %}

View File

@ -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 <http://www.gnu.org/licenses/>.
#
{% endcomment %}
{% load bootstrap %}
{% block content %}
<div class="row">
<div class="col-sm-8">
<form class="form" method="post">
{% csrf_token %}
{{ form|bootstrap }}
<input type="submit" class="btn btn-primary" value="Create..."/>
</form>
</div>
</div>
{% endblock %}
{% block page_js %}
<script>
$('#id_type').focus();
</script>
{% endblock %}