firstboot: Indentation & styling fixes

pycodestyle version used is 2.1.0 with default arguments.
This commit is contained in:
Sunil Mohan Adapa 2016-11-12 11:05:40 +05:30 committed by James Valleroy
parent a2d4719685
commit c37aa0fbd5
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
10 changed files with 75 additions and 253 deletions

View File

@ -22,11 +22,15 @@ Plinth module for first boot wizard
version = 1 version = 1
is_essential = True is_essential = True
first_boot_steps = [{'id': 'firstboot_state0',
first_boot_steps = [
{
'id': 'firstboot_state0',
'url': 'first_boot:state0', 'url': 'first_boot:state0',
'order': 0 'order': 0
}, },
{'id': 'firstboot_state10', {
'id': 'firstboot_state10',
'url': 'first_boot:state10', 'url': 'first_boot:state10',
'order': 10 'order': 10
} }

View File

@ -1,196 +0,0 @@
#
# 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/>.
#
"""
Forms for first boot module.
"""
import json
import logging
import requests
from django import forms
from django.contrib import auth
from django.contrib import messages
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext as _, ugettext_lazy
from plinth import actions
from plinth import cfg
from plinth.errors import ActionError, DomainRegistrationError
from plinth.modules.pagekite.utils import PREDEFINED_SERVICES, run
from plinth.modules.security import set_restricted_access
from plinth.modules.users.forms import GROUP_CHOICES, ValidNewUsernameCheckMixin
from plinth.utils import format_lazy
logger = logging.getLogger(__name__)
class State1Form(ValidNewUsernameCheckMixin, auth.forms.UserCreationForm):
"""Firstboot state 1: create a new user."""
def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request')
super().__init__(*args, **kwargs)
def save(self, commit=True):
"""Create and log the user in."""
user = super().save(commit=commit)
if commit:
try:
actions.superuser_run(
'ldap',
['create-user', user.get_username()],
input=self.cleaned_data['password1'].encode())
except ActionError:
messages.error(self.request,
_('Creating LDAP user failed.'))
try:
actions.superuser_run(
'ldap',
['add-user-to-group', user.get_username(), 'admin'])
except ActionError:
messages.error(self.request,
_('Failed to add new user to admin group.'))
# Create initial Django groups
for group_choice in GROUP_CHOICES:
auth.models.Group.objects.get_or_create(name=group_choice[0])
admin_group = auth.models.Group.objects.get(name='admin')
admin_group.user_set.add(user)
self.login_user(self.cleaned_data['username'],
self.cleaned_data['password1'])
# Restrict console login to users in admin or sudo group
try:
set_restricted_access(True)
message = _('Console login access restricted to users in '
'"admin" group. This can be configured in '
'security settings.')
messages.success(self.request, message)
except Exception:
messages.error(self.request,
_('Failed to restrict console access.'))
return user
def login_user(self, username, password):
"""Try to login the user with the credentials provided"""
try:
user = auth.authenticate(username=username, password=password)
auth.login(self.request, user)
except Exception:
pass
else:
message = _('User account created, you are now logged in')
messages.success(self.request, message)
class State5Form(forms.Form):
"""Set up freedombox.me pagekite subdomain"""
DOMAIN_APPENDIX = '.freedombox.me'
# Webservice url for domain validation and registration
service_url = 'http://freedombox.me/cgi-bin/freedomkite.pl'
code_help_text = format_lazy(
ugettext_lazy('The voucher you received with your {box_name} Danube '
'Edition'), box_name=ugettext_lazy(cfg.box_name))
code = forms.CharField(help_text=code_help_text)
domain = forms.SlugField(label=_('Subdomain'),
widget=SubdomainWidget(domain=DOMAIN_APPENDIX),
help_text=_('The subdomain you want to register'))
def clean_domain(self):
"""Append the domain to the users' subdomain"""
return self.cleaned_data['domain'] + self.DOMAIN_APPENDIX
def clean(self):
"""Validate user input (subdomain and code)"""
cleaned_data = super().clean()
# If the subdomain is wrong, don't look if the domain is
# available
if self.errors:
return cleaned_data
self.domain_already_registered = False
code = cleaned_data.get('code')
domain = cleaned_data.get('domain')
response = requests.get(self.service_url, params={'code': code}).json()
# 1. Code is invalid: {}
if 'domain' not in response:
raise ValidationError(_('This code is not valid'), code='invalid')
# 2. Code is valid, domain registered: {'domain': 'xx.freedombox.me'}
elif response['domain']:
if response['domain'] == domain:
self.domain_already_registered = True
else:
message = _('This code is bound to the domain {domain}.') \
.format(domain=response['domain'])
raise ValidationError(message, code='invalid')
# 3. Code is valid, no domain registered: {'domain': None}
elif response['domain'] is None:
# Make sure that the desired domain is available
data = {'domain': domain}
domain_response = requests.get(self.service_url, params=data)
registered_domain = domain_response.json()['domain']
if registered_domain is not None:
message = _('The requested domain is already registered.')
raise ValidationError(message, code='invalid')
return cleaned_data
def register_domain(self):
"""Register a domain (only if it's not already registered)"""
if self.domain_already_registered:
return
data = {'domain': self.cleaned_data['domain'],
'code': self.cleaned_data['code']}
response = requests.post(self.service_url, data)
if not response.ok:
message = _('Domain registration failed: {response}.').format(
response=response.text)
logger.error(message)
raise DomainRegistrationError(message)
def setup_pagekite(self):
"""Configure and enable PageKite service."""
# Set kite name and secret
run(['set-kite', '--kite-name', self.cleaned_data['domain']],
input=self.cleaned_data['code'].encode())
# Set frontend
run(['set-frontend', '%s:80' % self.cleaned_data['domain']])
# Enable PageKite HTTP + HTTPS service
for service_name in ['http', 'https']:
service = PREDEFINED_SERVICES[service_name]['params']
try:
run(['add-service', '--service', json.dumps(service)])
except ActionError as err:
if 'already exists' not in str(err):
raise
run(['start-and-enable'])

View File

@ -43,8 +43,10 @@ class FirstBootMiddleware(object):
if state == 0 and old_state == 10: if state == 0 and old_state == 10:
state = 1 state = 1
kvstore.set('setup_state', 1) kvstore.set('setup_state', 1)
user_requests_firstboot = is_firstboot(request.path) user_requests_firstboot = is_firstboot(request.path)
user_requests_login = request.path.startswith(reverse(settings.LOGIN_URL)) user_requests_login = request.path.startswith(
reverse(settings.LOGIN_URL))
help_index_url = reverse('help:index') help_index_url = reverse('help:index')
user_requests_help = request.path.startswith(help_index_url) user_requests_help = request.path.startswith(help_index_url)
if not user_requests_login and not user_requests_help: if not user_requests_login and not user_requests_help:
@ -65,6 +67,7 @@ def is_firstboot(path):
for step in steps: for step in steps:
if reverse(step.get('url')) == path: if reverse(step.get('url')) == path:
return True return True
return False return False
@ -76,6 +79,7 @@ def get_firstboot_steps():
if getattr(module_object, 'first_boot_steps', None): if getattr(module_object, 'first_boot_steps', None):
for step in module_object.first_boot_steps: for step in module_object.first_boot_steps:
steps.append(step) steps.append(step)
steps = sorted(steps, key=itemgetter('order')) steps = sorted(steps, key=itemgetter('order'))
return steps return steps
@ -85,6 +89,7 @@ def next_step():
global firstboot_steps global firstboot_steps
if len(firstboot_steps) == 0: if len(firstboot_steps) == 0:
firstboot_steps = get_firstboot_steps() firstboot_steps = get_firstboot_steps()
for step in firstboot_steps: for step in firstboot_steps:
done = kvstore.get_default(step.get('id'), 0) done = kvstore.get_default(step.get('id'), 0)
if done == 0: if done == 0:
@ -100,11 +105,13 @@ def mark_step_done(id):
global firstboot_steps global firstboot_steps
if len(firstboot_steps) == 0: if len(firstboot_steps) == 0:
firstboot_steps = get_firstboot_steps() firstboot_steps = get_firstboot_steps()
setup_done = True setup_done = True
for step in firstboot_steps: for step in firstboot_steps:
done = kvstore.get_default(step.get('id'), 0) done = kvstore.get_default(step.get('id'), 0)
if done == 0: if done == 0:
setup_done = False setup_done = False
break break
if setup_done: if setup_done:
kvstore.set('setup_state', 1) kvstore.set('setup_state', 1)

View File

@ -18,7 +18,7 @@
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.shortcuts import render from django.shortcuts import render
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from django.views.generic import CreateView, FormView, TemplateView from django.views.generic import TemplateView
from plinth import network from plinth import network
from .middleware import mark_step_done, next_step from .middleware import mark_step_done, next_step
@ -29,7 +29,7 @@ class State0View(TemplateView):
template_name = 'firstboot_state0.html' template_name = 'firstboot_state0.html'
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
"""Returns the context data""" """Returns the context data for the template."""
context = super(State0View, self).get_context_data(**kwargs) context = super(State0View, self).get_context_data(**kwargs)
mark_step_done('firstboot_state0') mark_step_done('firstboot_state0')
context['next_url'] = next_step() context['next_url'] = next_step()

View File

@ -31,7 +31,9 @@ depends = ['system', 'names']
managed_packages = ['pagekite'] managed_packages = ['pagekite']
first_boot_steps = [{'id': 'pagekite_firstboot', first_boot_steps = [
{
'id': 'pagekite_firstboot',
'url': 'pagekite:firstboot', 'url': 'pagekite:firstboot',
'order': 5, 'order': 5,
}, },

View File

@ -25,13 +25,12 @@ from django.utils.translation import ugettext as _, ugettext_lazy
import json import json
import logging import logging
from . import utils
from plinth import cfg from plinth import cfg
from plinth.errors import ActionError, DomainRegistrationError from plinth.errors import ActionError, DomainRegistrationError
from plinth.modules.pagekite.utils import PREDEFINED_SERVICES, run from plinth.modules.pagekite.utils import PREDEFINED_SERVICES, run
from plinth.utils import format_lazy from plinth.utils import format_lazy
from . import utils
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
@ -71,8 +70,8 @@ class ConfigurationForm(forms.Form):
server_domain = forms.CharField( server_domain = forms.CharField(
label=ugettext_lazy('Server domain'), required=False, label=ugettext_lazy('Server domain'), required=False,
help_text= \ help_text=ugettext_lazy(
ugettext_lazy('Select your pagekite server. Set "pagekite.net" to use ' 'Select your pagekite server. Set "pagekite.net" to use '
'the default pagekite.net server.'), 'the default pagekite.net server.'),
widget=forms.TextInput()) widget=forms.TextInput())
server_port = forms.IntegerField( server_port = forms.IntegerField(
@ -87,8 +86,8 @@ class ConfigurationForm(forms.Form):
kite_secret = TrimmedCharField( kite_secret = TrimmedCharField(
label=ugettext_lazy('Kite secret'), label=ugettext_lazy('Kite secret'),
help_text= \ help_text=ugettext_lazy(
ugettext_lazy('A secret associated with the kite or the default secret ' 'A secret associated with the kite or the default secret '
'for your account if no secret is set on the kite.')) 'for your account if no secret is set on the kite.'))
def save(self, request): def save(self, request):
@ -208,6 +207,8 @@ class BaseCustomServiceForm(forms.Form):
class DeleteCustomServiceForm(BaseCustomServiceForm): class DeleteCustomServiceForm(BaseCustomServiceForm):
"""Form to remove custom service."""
def delete(self, request): def delete(self, request):
service = self.convert_formdata_to_service(self.cleaned_data) service = self.convert_formdata_to_service(self.cleaned_data)
utils.run(['remove-service', '--service', json.dumps(service)]) utils.run(['remove-service', '--service', json.dumps(service)])

View File

@ -14,6 +14,7 @@
# You should have received a copy of the GNU Affero General Public License # 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/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# #
from django.contrib import messages from django.contrib import messages
from django.http.response import HttpResponseRedirect from django.http.response import HttpResponseRedirect
from django.template.response import TemplateResponse from django.template.response import TemplateResponse
@ -21,11 +22,11 @@ from django.urls import reverse, reverse_lazy
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.views.generic import View, TemplateView from django.views.generic import View, TemplateView
from django.views.generic.edit import FormView from django.views.generic.edit import FormView
from plinth.errors import DomainRegistrationError
from . import utils from . import utils
from .forms import ConfigurationForm, StandardServiceForm, \ from .forms import ConfigurationForm, StandardServiceForm, \
AddCustomServiceForm, DeleteCustomServiceForm, State5Form AddCustomServiceForm, DeleteCustomServiceForm, State5Form
from plinth.errors import DomainRegistrationError
from plinth.modules import pagekite from plinth.modules import pagekite
from plinth.modules.first_boot.middleware import mark_step_done from plinth.modules.first_boot.middleware import mark_step_done

View File

@ -34,11 +34,15 @@ depends = ['system']
managed_packages = ['ldapscripts', 'ldap-utils', 'libnss-ldapd', managed_packages = ['ldapscripts', 'ldap-utils', 'libnss-ldapd',
'libpam-ldapd', 'nslcd', 'slapd'] 'libpam-ldapd', 'nslcd', 'slapd']
first_boot_steps = [{'id': 'users_firstboot',
first_boot_steps = [
{
'id': 'users_firstboot',
'url': 'users:firstboot', 'url': 'users:firstboot',
'order': 1 'order': 1
}, },
] ]
title = _('Users and Groups') title = _('Users and Groups')

View File

@ -27,11 +27,9 @@ from django.utils.translation import ugettext as _, ugettext_lazy
from plinth import actions from plinth import actions
from plinth.errors import ActionError from plinth.errors import ActionError
from plinth.modules.security import set_restricted_access from plinth.modules.security import set_restricted_access
# Usernames used by optional services (that might not be installed yet). # Usernames used by optional services (that might not be installed yet).
RESERVED_USERNAMES = [ RESERVED_USERNAMES = [
'debian-deluged', 'debian-deluged',
'Debian-minetest', 'Debian-minetest',
@ -95,8 +93,8 @@ class CreateUserForm(ValidNewUsernameCheckMixin, UserCreationForm):
label=ugettext_lazy('Groups'), label=ugettext_lazy('Groups'),
required=False, required=False,
widget=forms.CheckboxSelectMultiple, widget=forms.CheckboxSelectMultiple,
help_text= \ help_text=ugettext_lazy(
ugettext_lazy('Select which services should be available to the new ' 'Select which services should be available to the new '
'user. The user will be able to log in to services that ' 'user. The user will be able to log in to services that '
'support single sign-on through LDAP, if they are in the ' 'support single sign-on through LDAP, if they are in the '
'appropriate group.<br /><br />Users in the admin group ' 'appropriate group.<br /><br />Users in the admin group '
@ -146,8 +144,8 @@ class UserUpdateForm(ValidNewUsernameCheckMixin, forms.ModelForm):
label=ugettext_lazy('SSH Keys'), label=ugettext_lazy('SSH Keys'),
required=False, required=False,
widget=forms.Textarea, widget=forms.Textarea,
help_text= \ help_text=ugettext_lazy(
ugettext_lazy('Setting an SSH public key will allow this user to ' 'Setting an SSH public key will allow this user to '
'securely log in to the system without using a ' 'securely log in to the system without using a '
'password. You may enter multiple keys, one on each ' 'password. You may enter multiple keys, one on each '
'line. Blank lines and lines starting with # will be ' 'line. Blank lines and lines starting with # will be '

View File

@ -22,13 +22,13 @@ from django.contrib.messages.views import SuccessMessageMixin
from django.urls import reverse, reverse_lazy from django.urls import reverse, reverse_lazy
from django.views.generic.edit import (CreateView, DeleteView, UpdateView, from django.views.generic.edit import (CreateView, DeleteView, UpdateView,
FormView) FormView)
from django.views.generic import ListView, CreateView as CV import django.views.generic
from django.utils.translation import ugettext as _, ugettext_lazy from django.utils.translation import ugettext as _, ugettext_lazy
from plinth import cfg
from .forms import CreateUserForm, UserChangePasswordForm, UserUpdateForm, State1Form
from .forms import CreateUserForm, UserChangePasswordForm, UserUpdateForm, \
State1Form
from plinth import actions from plinth import actions
from plinth import cfg
from plinth.errors import ActionError from plinth.errors import ActionError
from plinth.modules.first_boot.middleware import mark_step_done, next_step from plinth.modules.first_boot.middleware import mark_step_done, next_step
@ -65,7 +65,7 @@ class UserCreate(ContextMixin, SuccessMessageMixin, CreateView):
return kwargs return kwargs
class UserList(ContextMixin, ListView): class UserList(ContextMixin, django.views.generic.ListView):
"""View to list users.""" """View to list users."""
model = User model = User
template_name = 'users_list.html' template_name = 'users_list.html'
@ -168,7 +168,7 @@ class UserChangePassword(ContextMixin, SuccessMessageMixin, FormView):
return super(UserChangePassword, self).form_valid(form) return super(UserChangePassword, self).form_valid(form)
class State1View(CV): class State1View(django.views.generic.CreateView):
"""Create user account and log the user in.""" """Create user account and log the user in."""
template_name = 'firstboot_state1.html' template_name = 'firstboot_state1.html'
form_class = State1Form form_class = State1Form
@ -178,6 +178,7 @@ class State1View(CV):
"""Initialize the view object.""" """Initialize the view object."""
if not cfg.danube_edition: if not cfg.danube_edition:
mark_step_done('pagekite_firstboot') mark_step_done('pagekite_firstboot')
mark_step_done('users_firstboot') mark_step_done('users_firstboot')
self.success_url = next_step() self.success_url = next_step()
return super(State1View, self).__init__(*args, **kwargs) return super(State1View, self).__init__(*args, **kwargs)