first_boot: Fix various issues and refactor

- Fix major regression so that steps can submitted multiple times in
  case of errors.

- Don't serve the welcome page (and other pages) only once.  Show it
  until action is an taken.  This does not apply to the final step.

- Eliminate all coupling of one first boot step on another.

- Move first boot helper methods to __init__.py instead of middleware as
  it is more generic than middleware.

- Implement caching the first boot state to avoid an SQL query on every
  page load.  The down side is that if first boot state is modified in
  the backend DB outside Plinth, Plinth will need to be restarted to
  catch the modified value.

- Mark some methods as private.

- Refactor middleware code for slightly more simplicity.

- Don't show sidebar in pagekite first boot step.  Set width like other
  pages.
This commit is contained in:
Sunil Mohan Adapa authored and James Valleroy committed 2016-12-04 15:24:48 -05:00
1 parent 6cce89ed68
commit f457a0bdd3
9 files changed
+227 -157

No files matched your search

+31 -73
View File
@@ -24,13 +24,12 @@ from django.http.response import HttpResponseRedirect
from django.urls import reverse
from django.conf import settings
import logging
from operator import itemgetter
from plinth import kvstore, module_loader
from plinth import kvstore
from plinth.modules import first_boot
LOGGER = logging.getLogger(__name__)
firstboot_steps = []
class FirstBootMiddleware(object):
"""Forward to firstboot page if firstboot isn't finished yet."""
@@ -38,80 +37,39 @@ class FirstBootMiddleware(object):
@staticmethod
def process_request(request):
"""Handle a request as Django middleware request handler."""
old_state = kvstore.get_default('firstboot_state', 0)
state = kvstore.get_default('setup_state', 0)
if state == 0 and old_state == 10:
state = 1
kvstore.set('setup_state', 1)
user_requests_firstboot = is_firstboot(request.path)
# Don't interfere with login page
user_requests_login = request.path.startswith(
reverse(settings.LOGIN_URL))
help_index_url = reverse('help:index')
user_requests_help = request.path.startswith(help_index_url)
if not user_requests_login and not user_requests_help:
if state == 1 and user_requests_firstboot:
return HttpResponseRedirect(reverse('index'))
elif state == 0 and not user_requests_firstboot:
url = next_step()
return HttpResponseRedirect(reverse(url))
if user_requests_login:
return
# Don't interfere with help pages
user_requests_help = request.path.startswith(reverse('help:index'))
if user_requests_help:
return
def is_firstboot(path):
"""
Returns whether the path is a firstboot step url
:param path: path of current url
:return: true if its a first boot url false otherwise
"""
steps = get_firstboot_steps()
for step in steps:
if reverse(step.get('url')) == path:
return True
state = first_boot.is_completed()
return False
# Migrate from old settings variable
if state == 0:
old_state = kvstore.get_default('firstboot_state', 0)
if old_state == 10:
state = 1
first_boot.set_completed()
user_requests_firstboot = first_boot.is_firstboot_url(request.path)
def get_firstboot_steps():
"""Returns all firstboot steps"""
steps = []
modules = module_loader.loaded_modules
for (module_name, module_object) in modules.items():
if getattr(module_object, 'first_boot_steps', None):
for step in module_object.first_boot_steps:
steps.append(step)
# Redirect to first boot if requesting normal page and first
# boot is not complete.
if state == 0 and not user_requests_firstboot:
next_step = first_boot.next_step_or_none()
if next_step:
return HttpResponseRedirect(reverse(next_step))
else:
# No more steps in first boot
first_boot.set_completed()
steps = sorted(steps, key=itemgetter('order'))
return steps
def next_step():
""" Returns the next first boot step required to run """
global firstboot_steps
if len(firstboot_steps) == 0:
firstboot_steps = get_firstboot_steps()
for step in firstboot_steps:
done = kvstore.get_default(step.get('id'), 0)
if done == 0:
return step.get('url')
def mark_step_done(id):
"""
Marks the status of a first boot step is done
:param id: id of the firstboot step
"""
kvstore.set(id, 1)
global firstboot_steps
if len(firstboot_steps) == 0:
firstboot_steps = get_firstboot_steps()
setup_done = True
for step in firstboot_steps:
done = kvstore.get_default(step.get('id'), 0)
if done == 0:
setup_done = False
break
if setup_done:
kvstore.set('setup_state', 1)
# Redirect to index page if request firstboot after it is
# finished.
if state == 1 and user_requests_firstboot:
return HttpResponseRedirect(reverse('index'))