mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-05-27 10:44:33 +00:00
Enable and disable network connections
This commit is contained in:
parent
6ad1fb9ce7
commit
17040d4762
@ -15,6 +15,7 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
from dbus.exceptions import DBusException
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.core.urlresolvers import reverse_lazy
|
||||
@ -51,6 +52,17 @@ def init():
|
||||
def index(request):
|
||||
"""Show connection list."""
|
||||
connections = []
|
||||
active = []
|
||||
|
||||
for conn in NetworkManager.NetworkManager.ActiveConnections:
|
||||
try:
|
||||
settings = conn.Connection.GetSettings()['connection']
|
||||
except DBusException:
|
||||
# DBusException can be thrown here if the index is quickly loaded
|
||||
# after a connection is deactivated.
|
||||
continue
|
||||
active.append(settings['id'])
|
||||
|
||||
for conn in NetworkManager.Settings.ListConnections():
|
||||
settings = conn.GetSettings()['connection']
|
||||
# Display a friendly type name if known.
|
||||
@ -59,13 +71,75 @@ def index(request):
|
||||
connections.append({
|
||||
'name': settings['id'],
|
||||
'id': urllib.parse.quote_plus(settings['id']),
|
||||
'type': conn_type
|
||||
'type': conn_type,
|
||||
'is_active': settings['id'] in active,
|
||||
})
|
||||
connections.sort(key=lambda x: x['is_active'], reverse=True)
|
||||
|
||||
return TemplateResponse(request, 'connections_list.html',
|
||||
{'title': _('Network Connections'),
|
||||
'connections': connections})
|
||||
|
||||
|
||||
@login_required
|
||||
def activate(request, conn_id):
|
||||
"""Activate the connection."""
|
||||
name = urllib.parse.unquote_plus(conn_id)
|
||||
|
||||
# Find the connection
|
||||
connections = NetworkManager.Settings.ListConnections()
|
||||
connections = dict([(x.GetSettings()['connection']['id'], x)
|
||||
for x in connections])
|
||||
conn = connections[name]
|
||||
|
||||
# Find a suitable device
|
||||
ctype = conn.GetSettings()['connection']['type']
|
||||
if ctype == 'vpn':
|
||||
for dev in NetworkManager.NetworkManager.GetDevices():
|
||||
if (dev.State == NetworkManager.NM_DEVICE_STATE_ACTIVATED
|
||||
and dev.Managed):
|
||||
break
|
||||
else:
|
||||
messages.error(
|
||||
request,
|
||||
_('Failed to activate connection: '
|
||||
'No active, managed device found'))
|
||||
return redirect(reverse_lazy('network:index'))
|
||||
else:
|
||||
dtype = {
|
||||
'802-11-wireless': NetworkManager.NM_DEVICE_TYPE_WIFI,
|
||||
'802-3-ethernet': NetworkManager.NM_DEVICE_TYPE_ETHERNET,
|
||||
'gsm': NetworkManager.NM_DEVICE_TYPE_MODEM,
|
||||
}.get(ctype, ctype)
|
||||
|
||||
for dev in NetworkManager.NetworkManager.GetDevices():
|
||||
if (dev.DeviceType == dtype
|
||||
and dev.State == NetworkManager.NM_DEVICE_STATE_DISCONNECTED):
|
||||
break
|
||||
else:
|
||||
messages.error(
|
||||
request,
|
||||
_('Failed to activate connection: '
|
||||
'No suitable and available %s device found' % ctype))
|
||||
return redirect(reverse_lazy('network:index'))
|
||||
|
||||
NetworkManager.NetworkManager.ActivateConnection(conn, dev, "/")
|
||||
messages.success(request, _('Activated connection %s.') % name)
|
||||
return redirect(reverse_lazy('network:index'))
|
||||
|
||||
|
||||
@login_required
|
||||
def deactivate(request, conn_id):
|
||||
"""Deactivate the connection."""
|
||||
name = urllib.parse.unquote_plus(conn_id)
|
||||
active = NetworkManager.NetworkManager.ActiveConnections
|
||||
active = dict([(x.Connection.GetSettings()['connection']['id'], x)
|
||||
for x in active])
|
||||
NetworkManager.NetworkManager.DeactivateConnection(active[name])
|
||||
messages.success(request, _('Deactivated connection %s.') % name)
|
||||
return redirect(reverse_lazy('network:index'))
|
||||
|
||||
|
||||
@login_required
|
||||
def delete(request, conn_id):
|
||||
"""Handle deleting connections, showing a confirmation dialog first.
|
||||
@ -81,7 +155,7 @@ def delete(request, conn_id):
|
||||
conn.Delete()
|
||||
messages.success(request, _('Connection %s deleted.') % name)
|
||||
return redirect(reverse_lazy('network:index'))
|
||||
messages.failure(
|
||||
messages.error(
|
||||
request,
|
||||
_('Failed to delete connection %s: not found.') % name)
|
||||
return redirect(reverse_lazy('network:index'))
|
||||
|
||||
@ -24,7 +24,11 @@
|
||||
<style type="text/css">
|
||||
.connection-edit-label {
|
||||
display: inline-block;
|
||||
width: 75%;
|
||||
width: 40%;
|
||||
}
|
||||
.connection-type-label {
|
||||
display: inline-block;
|
||||
width: 20%;
|
||||
}
|
||||
.list-group-item .btn {
|
||||
margin: -5px 0;
|
||||
@ -35,7 +39,7 @@
|
||||
{% block content %}
|
||||
|
||||
<div class="row">
|
||||
<div class="col-sm-4">
|
||||
<div class="col-sm-6">
|
||||
<div class="list-group">
|
||||
{% for conn in connections %}
|
||||
<div class="list-group-item clearfix">
|
||||
@ -46,11 +50,45 @@
|
||||
aria-hidden="true"></span>
|
||||
</a>
|
||||
|
||||
<a class='connection-edit-label'
|
||||
<a class="connection-edit-label"
|
||||
title="Edit connection {{ conn.name }}">
|
||||
{{ conn.name }}
|
||||
</a>
|
||||
{{ conn.type }}
|
||||
|
||||
<span class="connection-type-label">{{ conn.type }}</span>
|
||||
|
||||
{% if conn.is_active %}
|
||||
<div class="btn-group">
|
||||
<button type="button"
|
||||
class="btn btn-success btn-xs dropdown-toggle"
|
||||
data-toggle="dropdown" aria-expanded="false">
|
||||
Active <span class="caret"></span>
|
||||
</button>
|
||||
<ul class="dropdown-menu" role="menu">
|
||||
<li>
|
||||
<a href="{% url 'network:deactivate' conn.id %}">
|
||||
Deactivate
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
{% else %}
|
||||
<div class="btn-group">
|
||||
<button type="button"
|
||||
class="btn btn-warning btn-xs dropdown-toggle"
|
||||
data-toggle="dropdown" aria-expanded="false">
|
||||
Not Active <span class="caret"></span>
|
||||
</button>
|
||||
<ul class="dropdown-menu" role="menu">
|
||||
<li>
|
||||
<a href="{% url 'network:activate' conn.id %}">
|
||||
Activate
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
|
||||
@ -25,6 +25,10 @@ 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.@+-]+)/activate/$',
|
||||
'activate', name='activate'),
|
||||
url(r'^sys/network/(?P<conn_id>[\w.@+-]+)/deactivate/$',
|
||||
'deactivate', name='deactivate'),
|
||||
url(r'^sys/network/(?P<conn_id>[\w.@+-]+)/delete/$',
|
||||
'delete', name='delete')
|
||||
'delete', name='delete'),
|
||||
)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user