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:
Sunil Mohan Adapa 2020-09-02 15:34:47 -07:00 committed by Veiko Aasa
parent 3375ba7d19
commit f58b5f8962
No known key found for this signature in database
GPG Key ID: 478539CAE680674E
2 changed files with 44 additions and 28 deletions

View File

@ -10,7 +10,7 @@ from django.utils.translation import ugettext_lazy as _
from plinth import actions from plinth import actions
from plinth import app as app_module from plinth import app as app_module
from plinth import daemon, menu, network from plinth import daemon, kvstore, menu, network
version = 1 version = 1
@ -47,10 +47,6 @@ logger = Logger(__name__)
app = None 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): class NetworksApp(app_module.App):
"""FreedomBox app for Networks.""" """FreedomBox app for Networks."""
@ -94,6 +90,38 @@ def setup(helper, old_version=None):
helper.call('post', app.enable) 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(): def _get_shared_interfaces():
"""Get active network interfaces in shared mode.""" """Get active network interfaces in shared mode."""
shared_interfaces = [] shared_interfaces = []

View File

@ -11,7 +11,7 @@ from django.utils.translation import ugettext as _
from django.views.decorators.http import require_POST from django.views.decorators.http import require_POST
from django.views.generic.edit import FormView from django.views.generic.edit import FormView
from plinth import kvstore, network from plinth import network
from plinth.modules import first_boot, networks from plinth.modules import first_boot, networks
from .forms import (ConnectionTypeSelectForm, EthernetForm, GenericForm, from .forms import (ConnectionTypeSelectForm, EthernetForm, GenericForm,
@ -25,10 +25,8 @@ def index(request):
"""Show connection list.""" """Show connection list."""
connections = network.get_connection_list() connections = network.get_connection_list()
network_topology = kvstore.get_default(networks.NETWORK_TOPOLOGY_TYPE_KEY, network_topology_type = networks.get_network_topology_type()
'to_router') internet_connection_type = networks.get_internet_connection_type()
internet_connection_type = kvstore.get_default(
networks.INTERNET_CONNECTION_TYPE_KEY, 'unknown')
return TemplateResponse( return TemplateResponse(
request, 'networks_configuration.html', { request, 'networks_configuration.html', {
'app_id': 'networks', 'app_id': 'networks',
@ -37,7 +35,7 @@ def index(request):
'has_diagnostics': True, 'has_diagnostics': True,
'is_enabled': True, 'is_enabled': True,
'connections': connections, 'connections': connections,
'network_topology': network_topology, 'network_topology': network_topology_type,
'internet_connectivity_type': internet_connection_type, 'internet_connectivity_type': internet_connection_type,
}) })
@ -418,18 +416,14 @@ class NetworkTopologyView(FormView):
def get_initial(self): def get_initial(self):
"""Get initial form data.""" """Get initial form data."""
return { return {'network_topology': networks.get_network_topology_type()}
'network_topology':
kvstore.get_default(networks.NETWORK_TOPOLOGY_TYPE_KEY,
'to_router')
}
def form_valid(self, form): def form_valid(self, form):
"""Save value to DB.""" """Save value to DB."""
network_topology = form.cleaned_data['network_topology'] network_topology = form.cleaned_data['network_topology']
logger.info('Updating network topology type with value %s' % logger.info('Updating network topology type with value %s' %
network_topology) network_topology)
kvstore.set(networks.NETWORK_TOPOLOGY_TYPE_KEY, network_topology) networks.set_network_topology_type(network_topology)
if network_topology == 'to_router': if network_topology == 'to_router':
self.success_url = reverse_lazy('networks:router-configuration') self.success_url = reverse_lazy('networks:router-configuration')
@ -462,17 +456,13 @@ class RouterConfigurationView(FormView):
def get_initial(self): def get_initial(self):
"""Return initial data for the form.""" """Return initial data for the form."""
return { return {'router_config': networks.get_router_configuration_type()}
'router_config':
kvstore.get_default(networks.ROUTER_CONFIGURATION_TYPE_KEY,
'not_configured')
}
def form_valid(self, form): def form_valid(self, form):
"""Save value to DB and redirect.""" """Save value to DB and redirect."""
type_ = form.cleaned_data['router_config'] type_ = form.cleaned_data['router_config']
logger.info('Updating router configuration: %s', type_) 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) return super().form_valid(form)
@ -482,8 +472,7 @@ class RouterConfigurationFirstBootView(RouterConfigurationView):
def dispatch(self, request, *args, **kwargs): def dispatch(self, request, *args, **kwargs):
"""Don't show wizard step if FreedomBox is not behind a router.""" """Don't show wizard step if FreedomBox is not behind a router."""
network_topology = kvstore.get_default( network_topology = networks.get_network_topology_type()
networks.NETWORK_TOPOLOGY_TYPE_KEY, 'to_router')
if network_topology != 'to_router': if network_topology != 'to_router':
first_boot.mark_step_done('router_setup_wizard') first_boot.mark_step_done('router_setup_wizard')
return HttpResponseRedirect(reverse_lazy(first_boot.next_step())) return HttpResponseRedirect(reverse_lazy(first_boot.next_step()))
@ -513,15 +502,14 @@ class InternetConnectionTypeView(FormView):
"""Return initial data for the form.""" """Return initial data for the form."""
return { return {
'internet_connection_type': 'internet_connection_type':
kvstore.get_default(networks.INTERNET_CONNECTION_TYPE_KEY, networks.get_internet_connection_type()
'unknown')
} }
def form_valid(self, form): def form_valid(self, form):
"""Save value to DB and redirect.""" """Save value to DB and redirect."""
type_ = form.cleaned_data['internet_connection_type'] type_ = form.cleaned_data['internet_connection_type']
logger.info('Updating internet connectivity type: %s', 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) return super().form_valid(form)