From b5f6821fd77d290c02298e1807cefab3cce4440a Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Mon, 24 Feb 2020 19:37:18 -0500 Subject: [PATCH] networks: Fixes for networks wizards - Make first wizard steps for router configuration and Internet connection type private. Since they run after the admin user page, the steps can run for logged in users. - Add option to the Internet connection type wizard, to let the user say that they don't know the type. This allows them to have a stress-free first setup experience. The option can act as if they have don't have public IP address at all. This is an extension of the proposed user experience. - Implement class based views for simplicity. - Update various IDs for consistency (even though other IDs in the networks module don't conform). - Iron out inconsistent terminology. Setup vs Configuration, Help vs. Wizard, etc. Tests performed: - Run first boot wizard from the beginning. Notice that both the wizard steps appear properly after the user login step (without permission denied problems). When not logged in, accessing the wizard steps with URL should redirect to login page. - During first boot wizard, select 'Skip this step' in both the steps after selecting non-default options. Values set should be default options as confirmed from networks page. - During first boot wizard, select non-default values, the values should be set properly as confirmed from networks page. - From the networks page select each value of the wizard. The option should get saved properly. - In case of Internet connection type, when the wizard values changes, the networks page should reflect the value properly. This should also show properly after skipping the step during first boot. - During first boot, the default value for Internet connection type should be 'I don't know' and router configuration should be 'Not configured'. Signed-off-by: Sunil Mohan Adapa Reviewed-by: Nektarios Katakis --- plinth/modules/networks/__init__.py | 4 +- plinth/modules/networks/forms.py | 7 +- plinth/modules/networks/networks.py | 137 ++++++++---------- .../internet_connectivity_firstboot.html | 6 +- .../templates/internet_connectivity_main.html | 8 +- .../router_configuration_firstboot.html | 6 +- .../templates/router_configuration_main.html | 2 +- plinth/modules/networks/urls.py | 22 ++- 8 files changed, 97 insertions(+), 95 deletions(-) diff --git a/plinth/modules/networks/__init__.py b/plinth/modules/networks/__init__.py index 34139ed7f..74005b15e 100644 --- a/plinth/modules/networks/__init__.py +++ b/plinth/modules/networks/__init__.py @@ -21,12 +21,12 @@ managed_packages = ['network-manager', 'batctl'] first_boot_steps = [ { 'id': 'internet_connectivity_type_wizard', - 'url': 'networks:firstboot_internet_connection_type', + 'url': 'networks:internet-connection-type-first-boot', 'order': 3, }, { 'id': 'router_setup_wizard', - 'url': 'networks:firstboot_router_setup', + 'url': 'networks:router-configuration-first-boot', 'order': 4, }, ] diff --git a/plinth/modules/networks/forms.py b/plinth/modules/networks/forms.py index 0203838e5..2443d7c73 100644 --- a/plinth/modules/networks/forms.py +++ b/plinth/modules/networks/forms.py @@ -334,13 +334,18 @@ class InternetConnectionTypeForm(forms.Form): '{box_name} provides many workaround solutions but each ' 'solution has some limitations.

'), box_name=cfg.box_name, allow_markup=True)), + ('unknown', + format_lazy( + _('I do not know the type of connection my ISP provides ' + '

You will be suggested the most ' + 'conservative actions.

'), allow_markup=True)), ], required=True, widget=forms.RadioSelect, ) -class RouterConfigurationWizardForm(forms.Form): +class RouterConfigurationForm(forms.Form): """Form to suggest how to configure a router. Suggest depending on wan connectivity/specific setup. The choice will diff --git a/plinth/modules/networks/networks.py b/plinth/modules/networks/networks.py index a40266ea4..0616fddd7 100644 --- a/plinth/modules/networks/networks.py +++ b/plinth/modules/networks/networks.py @@ -8,13 +8,14 @@ from django.template.response import TemplateResponse from django.urls import reverse_lazy 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.modules import first_boot, networks from .forms import (ConnectionTypeSelectForm, EthernetForm, GenericForm, InternetConnectionTypeForm, PPPoEForm, - RouterConfigurationWizardForm, WifiForm) + RouterConfigurationForm, WifiForm) logger = logging.getLogger(__name__) @@ -24,7 +25,7 @@ def index(request): connections = network.get_connection_list() internet_connection_type = kvstore.get_default( - networks.INTERNET_CONNECTION_TYPE_KEY, None) + networks.INTERNET_CONNECTION_TYPE_KEY, 'unknown') return TemplateResponse( request, 'networks_configuration.html', { 'app_id': 'networks', @@ -405,89 +406,79 @@ def delete(request, uuid): }) -def router_configuration_help_page(request): - """Show the router configuration wizard page/form. +class RouterConfigurationView(FormView): + """View for router configuration form.""" + template_name = 'router_configuration_update.html' + form_class = RouterConfigurationForm + success_url = reverse_lazy('networks:index') - Used both for fistboot step and same networks page. - - """ - is_firstboot = True \ - if 'firstboot' in request.build_absolute_uri() else False - - if request.method == 'POST' and request.POST['router_config']: - form = RouterConfigurationWizardForm(request.POST) - if form.is_valid(): - logger.info('Updating router configuration setup with value: %s' % - request.POST['router_config']) - kvstore.set(networks.ROUTER_CONFIGURATION_TYPE_KEY, - request.POST['router_config']) - if is_firstboot: - resp = reverse_lazy(first_boot.next_step()) - else: - resp = reverse_lazy('networks:index') - messages.success(request, _('Router configuration type saved.')) - - return redirect(resp) - else: - html = 'router_configuration_update.html' - initial = { + def get_initial(self): + """Return initial data for the form.""" + return { 'router_config': kvstore.get_default(networks.ROUTER_CONFIGURATION_TYPE_KEY, - 'not_configured'), + 'not_configured') } - template_kwargs = { - 'form': RouterConfigurationWizardForm(initial=initial), - } - if is_firstboot: - html = 'router_configuration_firstboot.html' - # mark step done on firstboot visit to get the next_step - first_boot.mark_step_done('router_setup_wizard') - template_kwargs.update({ - 'first_boot_next_step': reverse_lazy(first_boot.next_step()), - }) - - return TemplateResponse(request, html, template_kwargs) + 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_) + return super().form_valid(form) -def internet_connection_type_help_page(request): - """Show the internet connection type page. +class RouterConfigurationFirstBootView(RouterConfigurationView): + """View for router configuration form during first wizard.""" + template_name = 'router_configuration_firstboot.html' - Used for first boot step and networks page. + def get_success_url(self): + """Return the next wizard step after this one.""" + return reverse_lazy(first_boot.next_step()) - """ - is_firstboot = True \ - if 'firstboot' in request.build_absolute_uri() else False + def form_valid(self, form): + """Mark the first wizard step as done, save value and redirect.""" + first_boot.mark_step_done('router_setup_wizard') + if 'skip' in form.data: + return FormView.form_valid(self, form) - if request.method == 'POST' and request.POST['internet_connection_type']: - form = InternetConnectionTypeForm(request.POST) - if form.is_valid(): - logger.info('Updating internet connectivity type with value: %s' % - request.POST['internet_connection_type']) - kvstore.set( - networks.INTERNET_CONNECTION_TYPE_KEY, - request.POST['internet_connection_type'], - ) - if is_firstboot: - return redirect(reverse_lazy(first_boot.next_step())) - else: - messages.success(request, _('Internet connection type saved.')) - return redirect(reverse_lazy('networks:index')) - else: - html = 'internet_connectivity_type.html' - initial = { + return super().form_valid(form) + + +class InternetConnectionTypeView(FormView): + """View for Internet connection type form.""" + template_name = 'internet_connectivity_type.html' + form_class = InternetConnectionTypeForm + success_url = reverse_lazy('networks:index') + + def get_initial(self): + """Return initial data for the form.""" + return { 'internet_connection_type': kvstore.get_default(networks.INTERNET_CONNECTION_TYPE_KEY, - None) + 'unknown') } - template_kwargs = {'form': InternetConnectionTypeForm(initial=initial)} - if is_firstboot: - html = 'internet_connectivity_firstboot.html' - # mark step done on firstboot visit to get the next_step - first_boot.mark_step_done('internet_connectivity_type_wizard') - template_kwargs.update({ - 'first_boot_next_step': reverse_lazy(first_boot.next_step()), - }) + 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_) + return super().form_valid(form) - return TemplateResponse(request, html, template_kwargs) + +class InternetConnectionTypeFirstBootView(InternetConnectionTypeView): + """View to show Internet connection type form during first wizard.""" + template_name = 'internet_connectivity_firstboot.html' + + def get_success_url(self): + """Return the next wizard step after this one.""" + return reverse_lazy(first_boot.next_step()) + + def form_valid(self, form): + """Mark the first wizard step as done, save value and redirect.""" + first_boot.mark_step_done('internet_connectivity_type_wizard') + if 'skip' in form.data: + return FormView.form_valid(self, form) + + return super().form_valid(form) diff --git a/plinth/modules/networks/templates/internet_connectivity_firstboot.html b/plinth/modules/networks/templates/internet_connectivity_firstboot.html index 04c14e38e..6bd240144 100644 --- a/plinth/modules/networks/templates/internet_connectivity_firstboot.html +++ b/plinth/modules/networks/templates/internet_connectivity_firstboot.html @@ -15,7 +15,9 @@ {{ form|bootstrap }} - {% trans "Skip this step" %} - + + {% endblock %} diff --git a/plinth/modules/networks/templates/internet_connectivity_main.html b/plinth/modules/networks/templates/internet_connectivity_main.html index 25b30d5bd..ae6c23f0c 100644 --- a/plinth/modules/networks/templates/internet_connectivity_main.html +++ b/plinth/modules/networks/templates/internet_connectivity_main.html @@ -27,12 +27,16 @@ {% blocktrans trimmed %} My ISP provides a public IP address that may change over time. {% endblocktrans %} - {% else %} + {% elif internet_connectivity_type == "private_ip" %} {% blocktrans trimmed %} My ISP does not provide a public IP address. {% endblocktrans %} + {% else %} + {% blocktrans trimmed %} + I do not know the type of connection my ISP provides. + {% endblocktrans %} {% endif %} - {% trans 'Update...' %} diff --git a/plinth/modules/networks/templates/router_configuration_firstboot.html b/plinth/modules/networks/templates/router_configuration_firstboot.html index 81cfd8c8d..74877e993 100644 --- a/plinth/modules/networks/templates/router_configuration_firstboot.html +++ b/plinth/modules/networks/templates/router_configuration_firstboot.html @@ -15,8 +15,10 @@ {{ form|bootstrap }} - {% trans "Skip this step" %} - + + {% endblock %} diff --git a/plinth/modules/networks/templates/router_configuration_main.html b/plinth/modules/networks/templates/router_configuration_main.html index c3d109fba..b8c4369ed 100644 --- a/plinth/modules/networks/templates/router_configuration_main.html +++ b/plinth/modules/networks/templates/router_configuration_main.html @@ -24,7 +24,7 @@ Your {{ box_name }} gets its Internet from your Router via Wi-Fi or Ethernet cable. This is a typical home setup. {% endblocktrans %} - {% trans 'Update...' %} diff --git a/plinth/modules/networks/urls.py b/plinth/modules/networks/urls.py index 872c3a02d..637ca3f7f 100644 --- a/plinth/modules/networks/urls.py +++ b/plinth/modules/networks/urls.py @@ -4,7 +4,6 @@ URLs for the Network module """ from django.conf.urls import url -from stronghold.decorators import public from . import networks as views @@ -27,16 +26,15 @@ urlpatterns = [ r'(?P[^/]+)/)?$', views.add_wifi, name='add_wifi'), url(r'^sys/networks/(?P[\w.@+-]+)/delete/$', views.delete, name='delete'), - url(r'^sys/networks/router-setup-guide/$', - views.router_configuration_help_page, - name='router_setup'), - url(r'^sys/networks/firstboot/router_setup/$', - public(views.router_configuration_help_page), - name='firstboot_router_setup'), + url(r'^sys/networks/router-configuration/$', + views.RouterConfigurationView.as_view(), name='router-configuration'), + url(r'^sys/networks/firstboot/router-configuration/$', + views.RouterConfigurationFirstBootView.as_view(), + name='router-configuration-first-boot'), url(r'^sys/networks/internet-connection-type/$', - views.internet_connection_type_help_page, - name='internet_connection_type_setup'), - url(r'^sys/networks/firstboot/internet_connection_type/$', - public(views.internet_connection_type_help_page), - name='firstboot_internet_connection_type'), + views.InternetConnectionTypeView.as_view(), + name='internet-connection-type'), + url(r'^sys/networks/firstboot/internet-connection-type/$', + views.InternetConnectionTypeFirstBootView.as_view(), + name='internet-connection-type-first-boot'), ]