From 19f36f67d953f7866c9bf39c3cfb78ec7876d587 Mon Sep 17 00:00:00 2001 From: fonfon Date: Thu, 10 Jul 2014 19:49:41 +0300 Subject: [PATCH] moved firstboot handling to a firstboot middleware --- modules/first_boot/middleware.py | 25 +++++++++++++++++++++++++ plinth.py | 7 +++++++ views.py | 20 -------------------- 3 files changed, 32 insertions(+), 20 deletions(-) create mode 100644 modules/first_boot/middleware.py diff --git a/modules/first_boot/middleware.py b/modules/first_boot/middleware.py new file mode 100644 index 000000000..3d354b1ea --- /dev/null +++ b/modules/first_boot/middleware.py @@ -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'])) diff --git a/plinth.py b/plinth.py index eb4ca1edc..54101b16f 100755 --- a/plinth.py +++ b/plinth.py @@ -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, diff --git a/views.py b/views.py index a27abd70a..481911979 100644 --- a/views.py +++ b/views.py @@ -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'))