Add form to edit network connections.

This commit is contained in:
James Valleroy 2015-03-09 20:39:09 -04:00 committed by Sunil Mohan Adapa
parent 1c69b6cdb6
commit 201c96cb36
4 changed files with 123 additions and 0 deletions

View File

@ -77,6 +77,65 @@ def index(request):
'connections': connections})
@login_required
def edit(request, conn_id):
"""Serve connection editing form."""
form = None
name = urllib.parse.unquote_plus(conn_id)
form_data = {'name': name}
conn_found = False
for conn in NetworkManager.Settings.ListConnections():
settings = conn.GetSettings()
if settings['connection']['id'] == name:
conn_found = True
break
if not conn_found:
return redirect(reverse_lazy('network:index'))
if request.method == 'POST':
if settings['connection']['type'] == '802-11-wireless':
form = AddWifiForm(request.POST)
else:
form = AddEthernetForm(request.POST)
if form.is_valid():
new_settings = {
'connection': {
'id': form.cleaned_data['name'],
'type': settings['connection']['type'],
'uuid': settings['connection']['uuid'],
},
'ipv4': {'method': form.cleaned_data['ipv4_method']},
}
if form.cleaned_data['ipv4_method'] == 'manual':
new_settings['ipv4']['addresses'] = [
(form.cleaned_data['ipv4_address'],
24, # CIDR prefix length
'0.0.0.0')] # gateway
if settings['connection']['type'] == '802-3-ethernet':
new_settings['802-3-ethernet'] = {}
elif settings['connection']['type'] == '802-11-wireless':
new_settings['802-11-wireless'] = {
'ssid': form.cleaned_data['ssid'],
}
conn.Update(new_settings)
return redirect(reverse_lazy('network:index'))
else:
form_data['ipv4_method'] = settings['ipv4']['method']
if settings['ipv4']['addresses']:
form_data['ipv4_address'] = settings['ipv4']['addresses'][0][0]
if settings['connection']['type'] == '802-11-wireless':
form_data['ssid'] = settings['802-11-wireless']['ssid']
form = AddWifiForm(form_data)
else:
form = AddEthernetForm(form_data)
return TemplateResponse(request, 'connections_edit.html',
{'title': _('Edit Connection'),
'subsubmenu': subsubmenu,
'form': form})
@login_required
def activate(request, conn_id):
"""Activate the connection."""

View File

@ -0,0 +1,61 @@
{% 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="Edit Connection"/>
</form>
</div>
</div>
{% endblock %}
{% block page_js %}
<script type="text/javascript">
(function($) {
if ($('#id_ipv4_method').prop('value') != 'manual') {
$('#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

@ -51,6 +51,7 @@
</a>
<a class="connection-edit-label"
href="{% url 'network:edit' conn.id %}"
title="Edit connection {{ conn.name }}">
{{ conn.name }}
</a>

View File

@ -25,6 +25,8 @@ from django.conf.urls import patterns, url
urlpatterns = patterns(
'plinth.modules.network.network',
url(r'^sys/network/$', 'index', name='index'),
url(r'^sys/network/(?P<conn_id>[\w.@+-]+)/edit/$',
'edit', name='edit'),
url(r'^sys/network/(?P<conn_id>[\w.@+-]+)/activate/$',
'activate', name='activate'),
url(r'^sys/network/(?P<conn_id>[\w.@+-]+)/deactivate/$',