mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-11-29 08:25:10 +00:00
Use Django dispatcher instead of CherryPy dispatcher
This commit is big because anything small breaks the code. - Django dispatcher is based on regular expressions and does not need a tree structure - Reduces a lot of unnecessary dependencies among modules - Use Django sessions middlewear instead of CherryPy sessions - Introduce dependency based modules instead of numeric load order - Remove PagePlugin and simply use Django views - Eliminate page rendering wrappers in favor of Django context processors - Use custom auth for now until replaced by Django auth middlewear - Use Django templated 404 and 500 error pages
This commit is contained in:
1 parent
657bb11bbc
commit
58d13e3ed8
42 files changed
+1281
-1289
No files matched your search
+103
-111
@@ -18,16 +18,16 @@ The Plinth first-connection process has several stages:
|
||||
4. The user interacts with the box normally.
|
||||
"""
|
||||
|
||||
import cherrypy
|
||||
from django import forms
|
||||
from django.core import validators
|
||||
from django.http.response import HttpResponseRedirect
|
||||
from django.template.response import TemplateResponse
|
||||
from gettext import gettext as _
|
||||
from plugin_mount import PagePlugin
|
||||
|
||||
from ..lib.auth import add_user
|
||||
from ..config import config
|
||||
from withsqlite.withsqlite import sqlite_db
|
||||
import cfg
|
||||
import util
|
||||
|
||||
|
||||
## TODO: flesh out these tests values
|
||||
@@ -66,132 +66,124 @@ below. This key should not be the same as your key because you are not your \
|
||||
FreedomBox!'))
|
||||
|
||||
|
||||
class FirstBoot(PagePlugin):
|
||||
"""First boot wizard"""
|
||||
def index(request):
|
||||
"""Serve the index first boot page"""
|
||||
return state0(request)
|
||||
|
||||
def __init__(self):
|
||||
super(FirstBoot, self).__init__()
|
||||
|
||||
# this is the url this page will hang off of (/firstboot)
|
||||
self.register_page('firstboot')
|
||||
self.register_page('firstboot/state0')
|
||||
self.register_page('firstboot/state1')
|
||||
def generate_box_key():
|
||||
"""Generate a box key"""
|
||||
return "fake key"
|
||||
|
||||
@cherrypy.expose
|
||||
def index(self, *args, **kwargs):
|
||||
return self.state0(*args, **kwargs)
|
||||
|
||||
def generate_box_key(self):
|
||||
return "fake key"
|
||||
def state0(request):
|
||||
"""
|
||||
In this state, we do time config over HTTP, name the box and
|
||||
server key selection.
|
||||
|
||||
@cherrypy.expose
|
||||
def state0(self, **kwargs):
|
||||
"""
|
||||
In this state, we do time config over HTTP, name the box and
|
||||
server key selection.
|
||||
All the parameters are form inputs. They get passed in when
|
||||
the form is submitted. This method checks the inputs and if
|
||||
they validate, uses them to take action. If they do not
|
||||
validate, it displays the form to give the user a chance to
|
||||
input correct values. It might display an error message (in
|
||||
the message parameter).
|
||||
|
||||
All the parameters are form inputs. They get passed in when
|
||||
the form is submitted. This method checks the inputs and if
|
||||
they validate, uses them to take action. If they do not
|
||||
validate, it displays the form to give the user a chance to
|
||||
input correct values. It might display an error message (in
|
||||
the message parameter).
|
||||
message is an optional string that we can display to the
|
||||
user. It's a good place to put error messages.
|
||||
"""
|
||||
try:
|
||||
if _read_state() >= 5:
|
||||
return HttpResponseRedirect(cfg.server_dir)
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
message is an optional string that we can display to the
|
||||
user. It's a good place to put error messages.
|
||||
"""
|
||||
try:
|
||||
if FirstBoot._read_state() >= 5:
|
||||
raise cherrypy.HTTPRedirect(cfg.server_dir, 302)
|
||||
except KeyError:
|
||||
pass
|
||||
## Until LDAP is in place, we'll put the box key in the cfg.store_file
|
||||
status = get_state0()
|
||||
|
||||
## Until LDAP is in place, we'll put the box key in the cfg.store_file
|
||||
status = self.get_state0()
|
||||
form = None
|
||||
messages = []
|
||||
|
||||
form = None
|
||||
messages = []
|
||||
if request.method == 'POST':
|
||||
form = State0Form(request.POST, prefix='firstboot')
|
||||
# pylint: disable-msg=E1101
|
||||
if form.is_valid():
|
||||
success = _apply_state0(status, form.cleaned_data, messages)
|
||||
|
||||
if kwargs:
|
||||
form = State0Form(kwargs, prefix='firstboot')
|
||||
# pylint: disable-msg=E1101
|
||||
if form.is_valid():
|
||||
success = self._apply_state0(status, form.cleaned_data,
|
||||
messages)
|
||||
if success:
|
||||
# Everything is good, permanently mark and move to page 2
|
||||
_write_state(1)
|
||||
return HttpResponseRedirect(
|
||||
cfg.server_dir + '/firstboot/state1')
|
||||
else:
|
||||
form = State0Form(initial=status, prefix='firstboot')
|
||||
|
||||
if success:
|
||||
# Everything is good, permanently mark and move to page 2
|
||||
FirstBoot._write_state(1)
|
||||
raise cherrypy.HTTPRedirect('state1', 302)
|
||||
return TemplateResponse(request, 'firstboot_state0.html',
|
||||
{'title': _('First Boot!'),
|
||||
'form': form,
|
||||
'messages_': messages})
|
||||
|
||||
|
||||
def get_state0():
|
||||
"""Return the state for form state 0"""
|
||||
with sqlite_db(cfg.store_file, table="thisbox", autocommit=True) as \
|
||||
database:
|
||||
return {'hostname': config.get_hostname(),
|
||||
'box_key': database.get('box_key', None)}
|
||||
|
||||
|
||||
def _apply_state0(old_state, new_state, messages):
|
||||
"""Apply changes in state 0 form"""
|
||||
success = True
|
||||
with sqlite_db(cfg.store_file, table="thisbox", autocommit=True) as \
|
||||
database:
|
||||
database['about'] = 'Information about this FreedomBox'
|
||||
|
||||
if new_state['box_key']:
|
||||
database['box_key'] = new_state['box_key']
|
||||
elif not old_state['box_key']:
|
||||
database['box_key'] = generate_box_key()
|
||||
|
||||
if old_state['hostname'] != new_state['hostname']:
|
||||
config.set_hostname(new_state['hostname'])
|
||||
|
||||
error = add_user(new_state['username'], new_state['password'],
|
||||
'First user, please change', '', True)
|
||||
if error:
|
||||
messages.append(
|
||||
('error', _('User account creation failed: %s') % error))
|
||||
success = False
|
||||
else:
|
||||
form = State0Form(initial=status, prefix='firstboot')
|
||||
messages.append(('success', _('User account created')))
|
||||
|
||||
return util.render_template(template='firstboot_state0',
|
||||
title=_('First Boot!'), form=form,
|
||||
messages=messages)
|
||||
return success
|
||||
|
||||
@staticmethod
|
||||
def get_state0():
|
||||
"""Return the state for form state 0"""
|
||||
with sqlite_db(cfg.store_file, table="thisbox", autocommit=True) as \
|
||||
database:
|
||||
return {'hostname': config.get_hostname(),
|
||||
'box_key': database.get('box_key', None)}
|
||||
|
||||
def _apply_state0(self, old_state, new_state, messages):
|
||||
"""Apply changes in state 0 form"""
|
||||
success = True
|
||||
with sqlite_db(cfg.store_file, table="thisbox", autocommit=True) as \
|
||||
database:
|
||||
database['about'] = 'Information about this FreedomBox'
|
||||
def state1(request):
|
||||
"""
|
||||
State 1 is when we have a box name and key. In this state,
|
||||
our task is to provide a certificate and maybe to guide the
|
||||
user through installing it. We automatically move to State 2,
|
||||
which is an HTTPS connection.
|
||||
|
||||
if new_state['box_key']:
|
||||
database['box_key'] = new_state['box_key']
|
||||
elif not old_state['box_key']:
|
||||
database['box_key'] = self.generate_box_key()
|
||||
TODO: HTTPS failure in State 2 should returns to state 1.
|
||||
"""
|
||||
# TODO complete first_boot handling
|
||||
# Make sure the user is not stuck on a dead end for now.
|
||||
_write_state(5)
|
||||
|
||||
if old_state['hostname'] != new_state['hostname']:
|
||||
config.set_hostname(new_state['hostname'])
|
||||
return TemplateResponse(request, 'firstboot_state1.html',
|
||||
{'title': _('Installing the Certificate')})
|
||||
|
||||
error = add_user(new_state['username'], new_state['password'],
|
||||
'First user, please change', '', True)
|
||||
if error:
|
||||
messages.append(
|
||||
('error', _('User account creation failed: %s') % error))
|
||||
success = False
|
||||
else:
|
||||
messages.append(('success', _('User account created')))
|
||||
|
||||
return success
|
||||
def _read_state():
|
||||
"""Read the current state from database"""
|
||||
with sqlite_db(cfg.store_file, table='firstboot',
|
||||
autocommit=True) as database:
|
||||
return database['state']
|
||||
|
||||
@staticmethod
|
||||
@cherrypy.expose
|
||||
def state1():
|
||||
"""
|
||||
State 1 is when we have a box name and key. In this state,
|
||||
our task is to provide a certificate and maybe to guide the
|
||||
user through installing it. We automatically move to State 2,
|
||||
which is an HTTPS connection.
|
||||
|
||||
TODO: HTTPS failure in State 2 should returns to state 1.
|
||||
"""
|
||||
# TODO complete first_boot handling
|
||||
# Make sure the user is not stuck on a dead end for now.
|
||||
FirstBoot._write_state(5)
|
||||
|
||||
return util.render_template(template='firstboot_state1',
|
||||
title=_('Installing the Certificate'))
|
||||
|
||||
@staticmethod
|
||||
def _read_state():
|
||||
"""Read the current state from database"""
|
||||
with sqlite_db(cfg.store_file, table='firstboot',
|
||||
autocommit=True) as database:
|
||||
return database['state']
|
||||
|
||||
@staticmethod
|
||||
def _write_state(state):
|
||||
"""Write state to database"""
|
||||
with sqlite_db(cfg.store_file, table='firstboot',
|
||||
autocommit=True) as database:
|
||||
database['state'] = state
|
||||
def _write_state(state):
|
||||
"""Write state to database"""
|
||||
with sqlite_db(cfg.store_file, table='firstboot',
|
||||
autocommit=True) as database:
|
||||
database['state'] = state
|
||||
Reference in new issue
Block a user