mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-06-03 10:50:20 +00:00
networks: Expose API to get/set network meta info
- API can be consumed by other apps. - Consistently, get/set store keys and default values without repeated code. Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: Veiko Aasa <veiko17@disroot.org>
This commit is contained in:
parent
3375ba7d19
commit
f58b5f8962
@ -10,7 +10,7 @@ from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from plinth import actions
|
||||
from plinth import app as app_module
|
||||
from plinth import daemon, menu, network
|
||||
from plinth import daemon, kvstore, menu, network
|
||||
|
||||
version = 1
|
||||
|
||||
@ -47,10 +47,6 @@ logger = Logger(__name__)
|
||||
|
||||
app = None
|
||||
|
||||
NETWORK_TOPOLOGY_TYPE_KEY = 'networks_topology_type'
|
||||
ROUTER_CONFIGURATION_TYPE_KEY = 'networks_router_configuration_type'
|
||||
INTERNET_CONNECTION_TYPE_KEY = 'networks_internet_type'
|
||||
|
||||
|
||||
class NetworksApp(app_module.App):
|
||||
"""FreedomBox app for Networks."""
|
||||
@ -94,6 +90,38 @@ def setup(helper, old_version=None):
|
||||
helper.call('post', app.enable)
|
||||
|
||||
|
||||
def get_network_topology_type():
|
||||
"""Return the currently configured network topology type or default."""
|
||||
return kvstore.get_default('networks_topology_type', 'to_router')
|
||||
|
||||
|
||||
def set_network_topology_type(network_topology_type):
|
||||
"""Store the network topology type."""
|
||||
kvstore.set('networks_topology_type', network_topology_type)
|
||||
|
||||
|
||||
def get_internet_connection_type():
|
||||
"""Return the currently configured internet connection type or default."""
|
||||
return kvstore.get_default('networks_internet_type', 'unknown')
|
||||
|
||||
|
||||
def set_internet_connection_type(internet_connection_type):
|
||||
"""Store the internet connection type."""
|
||||
return kvstore.set('networks_internet_type', internet_connection_type)
|
||||
|
||||
|
||||
def get_router_configuration_type():
|
||||
"""Return the currently configured router configuration type or default."""
|
||||
return kvstore.get_default('networks_router_configuration_type',
|
||||
'not_configured')
|
||||
|
||||
|
||||
def set_router_configuration_type(router_configuration_type):
|
||||
"""Store the router configuration type."""
|
||||
return kvstore.set('networks_router_configuration_type',
|
||||
router_configuration_type)
|
||||
|
||||
|
||||
def _get_shared_interfaces():
|
||||
"""Get active network interfaces in shared mode."""
|
||||
shared_interfaces = []
|
||||
|
||||
@ -11,7 +11,7 @@ from django.utils.translation import ugettext as _
|
||||
from django.views.decorators.http import require_POST
|
||||
from django.views.generic.edit import FormView
|
||||
|
||||
from plinth import kvstore, network
|
||||
from plinth import network
|
||||
from plinth.modules import first_boot, networks
|
||||
|
||||
from .forms import (ConnectionTypeSelectForm, EthernetForm, GenericForm,
|
||||
@ -25,10 +25,8 @@ def index(request):
|
||||
"""Show connection list."""
|
||||
connections = network.get_connection_list()
|
||||
|
||||
network_topology = kvstore.get_default(networks.NETWORK_TOPOLOGY_TYPE_KEY,
|
||||
'to_router')
|
||||
internet_connection_type = kvstore.get_default(
|
||||
networks.INTERNET_CONNECTION_TYPE_KEY, 'unknown')
|
||||
network_topology_type = networks.get_network_topology_type()
|
||||
internet_connection_type = networks.get_internet_connection_type()
|
||||
return TemplateResponse(
|
||||
request, 'networks_configuration.html', {
|
||||
'app_id': 'networks',
|
||||
@ -37,7 +35,7 @@ def index(request):
|
||||
'has_diagnostics': True,
|
||||
'is_enabled': True,
|
||||
'connections': connections,
|
||||
'network_topology': network_topology,
|
||||
'network_topology': network_topology_type,
|
||||
'internet_connectivity_type': internet_connection_type,
|
||||
})
|
||||
|
||||
@ -418,18 +416,14 @@ class NetworkTopologyView(FormView):
|
||||
|
||||
def get_initial(self):
|
||||
"""Get initial form data."""
|
||||
return {
|
||||
'network_topology':
|
||||
kvstore.get_default(networks.NETWORK_TOPOLOGY_TYPE_KEY,
|
||||
'to_router')
|
||||
}
|
||||
return {'network_topology': networks.get_network_topology_type()}
|
||||
|
||||
def form_valid(self, form):
|
||||
"""Save value to DB."""
|
||||
network_topology = form.cleaned_data['network_topology']
|
||||
logger.info('Updating network topology type with value %s' %
|
||||
network_topology)
|
||||
kvstore.set(networks.NETWORK_TOPOLOGY_TYPE_KEY, network_topology)
|
||||
networks.set_network_topology_type(network_topology)
|
||||
if network_topology == 'to_router':
|
||||
self.success_url = reverse_lazy('networks:router-configuration')
|
||||
|
||||
@ -462,17 +456,13 @@ class RouterConfigurationView(FormView):
|
||||
|
||||
def get_initial(self):
|
||||
"""Return initial data for the form."""
|
||||
return {
|
||||
'router_config':
|
||||
kvstore.get_default(networks.ROUTER_CONFIGURATION_TYPE_KEY,
|
||||
'not_configured')
|
||||
}
|
||||
return {'router_config': networks.get_router_configuration_type()}
|
||||
|
||||
def form_valid(self, form):
|
||||
"""Save value to DB and redirect."""
|
||||
type_ = form.cleaned_data['router_config']
|
||||
logger.info('Updating router configuration: %s', type_)
|
||||
kvstore.set(networks.ROUTER_CONFIGURATION_TYPE_KEY, type_)
|
||||
networks.set_router_configuration_type(type_)
|
||||
return super().form_valid(form)
|
||||
|
||||
|
||||
@ -482,8 +472,7 @@ class RouterConfigurationFirstBootView(RouterConfigurationView):
|
||||
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
"""Don't show wizard step if FreedomBox is not behind a router."""
|
||||
network_topology = kvstore.get_default(
|
||||
networks.NETWORK_TOPOLOGY_TYPE_KEY, 'to_router')
|
||||
network_topology = networks.get_network_topology_type()
|
||||
if network_topology != 'to_router':
|
||||
first_boot.mark_step_done('router_setup_wizard')
|
||||
return HttpResponseRedirect(reverse_lazy(first_boot.next_step()))
|
||||
@ -513,15 +502,14 @@ class InternetConnectionTypeView(FormView):
|
||||
"""Return initial data for the form."""
|
||||
return {
|
||||
'internet_connection_type':
|
||||
kvstore.get_default(networks.INTERNET_CONNECTION_TYPE_KEY,
|
||||
'unknown')
|
||||
networks.get_internet_connection_type()
|
||||
}
|
||||
|
||||
def form_valid(self, form):
|
||||
"""Save value to DB and redirect."""
|
||||
type_ = form.cleaned_data['internet_connection_type']
|
||||
logger.info('Updating internet connectivity type: %s', type_)
|
||||
kvstore.set(networks.INTERNET_CONNECTION_TYPE_KEY, type_)
|
||||
networks.set_internet_connection_type(type_)
|
||||
return super().form_valid(form)
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user