moved firstboot handling to a firstboot middleware

This commit is contained in:
fonfon 2014-07-10 19:49:41 +03:00
parent cf03ee160a
commit 19f36f67d9
3 changed files with 32 additions and 20 deletions

View File

@ -0,0 +1,25 @@
import logging
from django.core.urlresolvers import reverse
from django.http.response import HttpResponseRedirect
from withsqlite.withsqlite import sqlite_db
import cfg
LOGGER = logging.getLogger(__name__)
class FirstBootMiddleware:
""" Forward to firstboot page if firstboot isn't finished yet """
def process_request(self, request):
with sqlite_db(cfg.store_file, table='firstboot') as database:
if not 'state' in database:
# Permanent redirect causes the browser to cache the redirect,
# preventing the user from navigating to /plinth until the
# browser is restarted.
return HttpResponseRedirect(reverse('first_boot:index'))
if database['state'] < 5:
LOGGER.info('First boot state - %d', database['state'])
return HttpResponseRedirect(reverse('first_boot:state%d' %
database['state']))

View File

@ -183,6 +183,13 @@ def configure_django():
LOGIN_URL=cfg.server_dir + '/accounts/login/',
LOGIN_REDIRECT_URL=cfg.server_dir + '/',
LOGOUT_URL=cfg.server_dir + '/accounts/logout/',
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'modules.first_boot.middleware.FirstBootMiddleware',
),
ROOT_URLCONF='urls',
SESSION_ENGINE='django.contrib.sessions.backends.file',
SESSION_FILE_PATH=sessions_directory,

View File

@ -21,30 +21,10 @@ Main Plinth views
from django.core.urlresolvers import reverse
from django.http.response import HttpResponseRedirect
import logging
from withsqlite.withsqlite import sqlite_db
import cfg
LOGGER = logging.getLogger(__name__)
def index(request):
"""Serve the main index page"""
# TODO: Move firstboot handling to firstboot module somehow
with sqlite_db(cfg.store_file, table='firstboot') as database:
if not 'state' in database:
# Permanent redirect causes the browser to cache the redirect,
# preventing the user from navigating to /plinth until the
# browser is restarted.
return HttpResponseRedirect(reverse('first_boot:index'))
if database['state'] < 5:
LOGGER.info('First boot state - %d', database['state'])
return HttpResponseRedirect(reverse('first_boot:state%d' %
database['state']))
if request.user.is_authenticated():
return HttpResponseRedirect(reverse('apps:index'))