Reorganize python sources into 'plinth' package

This commit is contained in:
Sunil Mohan Adapa committed 2014-08-29 12:57:27 +05:30
1 parent eaacd3864b
commit 65fa648d9f
109 files changed
+63 -61

No files matched your search

+24
View File
@@ -0,0 +1,24 @@
#
# This file is part of Plinth.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
Plinth module for first boot wizard
"""
from . import first_boot
__all__ = ['first_boot']
+188
View File
@@ -0,0 +1,188 @@
"""
First Boot: Initial Plinth Configuration.
See docs/design/first-connection.mdwn for details.
The Plinth first-connection process has several stages:
0. The user connects to Plinth for the first time and is redirected from
the home page to the Hello page.
1. The user sets the Box's name, the administrator's name and
password, and the box's PGP key (optional).
2. The box generates and the user receives any PGP keys.
3. The box detects the network's configuration and restarts networking.
4. The user interacts with the box normally.
"""
from django import forms
from django.contrib import messages
from django.core import validators
from django.core.urlresolvers import reverse
from django.http.response import HttpResponseRedirect
from django.template.response import TemplateResponse
from gettext import gettext as _
from plinth import cfg
from plinth.modules.config import config
from plinth.modules.lib.auth import add_user
from withsqlite.withsqlite import sqlite_db
## TODO: flesh out these tests values
def valid_box_key(value):
"""Check whether box key is valid"""
del value # Unused
return True
class State0Form(forms.Form): # pylint: disable-msg=W0232
"""First boot state 0 form"""
hostname = forms.CharField(
label=_('Name your FreedomBox'),
help_text=_('For convenience, your FreedomBox needs a name. It \
should be something short that does not contain spaces or punctuation. \
"Willard" would be a good name while "Freestyle McFreedomBox!!!" would \
not. It must be alphanumeric, start with an alphabet and must not be greater \
than 63 characters in length.'),
validators=[
validators.RegexValidator(r'^[a-zA-Z][a-zA-Z0-9]{,62}$',
_('Invalid hostname'))])
username = forms.CharField(label=_('Username'))
password = forms.CharField(label=_('Password'),
widget=forms.PasswordInput())
box_key = forms.CharField(
label=_('Box\'s key (optional)'), required=False,
widget=forms.Textarea(), validators=[valid_box_key],
help_text=_('Cryptographic keys are used so that Box\'s identity can \
proved when talking to you. This key can be auto-generated, but if one \
already exists (from a prior FreedomBox, for example), you can paste it \
below. This key should not be the same as your key because you are not your \
FreedomBox!'))
def index(request):
"""Serve the index first boot page"""
return state0(request)
def generate_box_key():
"""Generate a box key"""
return "fake key"
def state0(request):
"""
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).
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(reverse('index'))
except KeyError:
pass
## Until LDAP is in place, we'll put the box key in the cfg.store_file
status = get_state0()
form = None
if request.method == 'POST':
form = State0Form(request.POST, prefix='firstboot')
# pylint: disable-msg=E1101
if form.is_valid():
success = _apply_state0(request, status, form.cleaned_data)
if success:
# Everything is good, permanently mark and move to page 2
_write_state(1)
return HttpResponseRedirect(reverse('first_boot:state1'))
else:
form = State0Form(initial=status, prefix='firstboot')
return TemplateResponse(request, 'firstboot_state0.html',
{'title': _('First Boot!'),
'form': form})
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(request, old_state, new_state):
"""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.error(
request, _('User account creation failed: %s') % error)
success = False
else:
messages.success(request, _('User account created'))
return success
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.
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)
return TemplateResponse(request, 'firstboot_state1.html',
{'title': _('Installing the Certificate')})
def _read_state():
"""Read the current state from database"""
with sqlite_db(cfg.store_file, table='firstboot',
autocommit=True) as database:
return database['state']
def _write_state(state):
"""Write state to database"""
with sqlite_db(cfg.store_file, table='firstboot',
autocommit=True) as database:
database['state'] = state
+55
View File
@@ -0,0 +1,55 @@
#
# This file is part of Plinth.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
Django middleware to redirect to firstboot wizard if it has not be run
yet.
"""
from django.core.urlresolvers import reverse
from django.http.response import HttpResponseRedirect
import logging
import cfg
from withsqlite.withsqlite import sqlite_db
LOGGER = logging.getLogger(__name__)
class FirstBootMiddleware(object):
"""Forward to firstboot page if firstboot isn't finished yet."""
@staticmethod
def process_request(request):
"""Handle a request as Django middleware request handler."""
# Prevent redirecting to first boot wizard in a loop by
# checking if we are already in first boot wizard.
if request.path.startswith(reverse('first_boot:index')):
return
with sqlite_db(cfg.store_file, table='firstboot') as database:
if 'state' not 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']))
@@ -0,0 +1,28 @@
{% comment %}
#
# This file is part of Plinth.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
{% endcomment %}
<div class="well sidebar-nav">
<h3>Getting Help</h3>
<p>We've done our best to make your FreedomBox easy to use. If
you have questions during setup, there are a few places to turn
for help. TODO: add links to such help.</p>
</div>
@@ -0,0 +1,55 @@
{% extends "base.html" %}
{% comment %}
#
# This file is part of Plinth.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
{% endcomment %}
{% load bootstrap %}
{% block main_block %}
<h2>Welcome to Your FreedomBox!</h2>
<p>Welcome. It looks like this FreedomBox isn't set up yet. We'll
need to ask you a just few questions to get started.</p>
<form class="form" method="post">
{% csrf_token %}
{% include 'bootstrapform/field.html' with field=form.hostname %}
<p><strong>Initial user and password.</strong> Access to this web
interface is protected by knowing a username and password.
Provide one here to register the initial privileged user. The
password can be changed and other users added later.</p>
{% include 'bootstrapform/field.html' with field=form.username %}
{% include 'bootstrapform/field.html' with field=form.password %}
{% include 'bootstrapform/field.html' with field=form.box_key %}
<input type="submit" class="btn-primary" value="Box it up!"/>
</form>
{% endblock %}
{% block sidebar_right_block %}
{% include "firstboot_sidebar.html" %}
{% endblock %}
@@ -0,0 +1,41 @@
{% extends "base.html" %}
{% comment %}
#
# This file is part of Plinth.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
{% endcomment %}
{% load bootstrap %}
{% block main_block %}
<p>Welcome screen not completely implemented yet. Press <a
href="{% url 'apps:index' %}">continue</a> to see the rest of the
web interface.</p>
<ul>
<li>TODO: explain all this cert stuff to the user.</li>
<li>TODO: add instrux for installing certificate.</li>
<li>TODO: add steps for after you have installed certificate.</li>
</ul>
{% endblock %}
{% block sidebar_right_block %}
{% include "firstboot_sidebar.html" %}
{% endblock %}
+30
View File
@@ -0,0 +1,30 @@
#
# This file is part of Plinth.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
URLs for the First Boot module
"""
from django.conf.urls import patterns, url
urlpatterns = patterns( # pylint: disable-msg=C0103
'plinth.modules.first_boot.first_boot',
url(r'^firstboot/$', 'index', name='index'),
url(r'^firstboot/state0/$', 'state0', name='state0'),
url(r'^firstboot/state1/$', 'state1', name='state1')
)