Made Suggested Changes

This commit is contained in:
Hemanth Kumar Veeranki 2016-11-03 23:08:42 +05:30 committed by James Valleroy
parent 0290f68ae6
commit 6011a7adfa
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
7 changed files with 75 additions and 61 deletions

View File

@ -22,7 +22,6 @@ Forms for first boot module.
import json
import logging
import requests
import subprocess
from django import forms
from django.contrib import auth
@ -43,6 +42,7 @@ 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)
@ -103,22 +103,6 @@ class State1Form(ValidNewUsernameCheckMixin, auth.forms.UserCreationForm):
messages.success(self.request, message)
class SubdomainWidget(forms.widgets.TextInput):
"""Append the domain to the subdomain bootstrap input field"""
def __init__(self, domain, *args, **kwargs):
"""Intialize the widget by storing the domain value."""
super().__init__(*args, **kwargs)
self.domain = domain
def render(self, *args, **kwargs):
"""Return the HTML for the widget."""
inputfield = super().render(*args, **kwargs)
return """<div class="input-group">
{0}
<span class="input-group-addon">{1}</span>
</div>""".format(inputfield, self.domain)
class State5Form(forms.Form):
"""Set up freedombox.me pagekite subdomain"""
DOMAIN_APPENDIX = '.freedombox.me'

View File

@ -26,10 +26,11 @@ from django.conf import settings
import logging
from operator import itemgetter
from plinth import kvstore, module_loader
from django.shortcuts import render
LOGGER = logging.getLogger(__name__)
firstboot_steps = []
class FirstBootMiddleware(object):
"""Forward to firstboot page if firstboot isn't finished yet."""
@ -37,7 +38,11 @@ class FirstBootMiddleware(object):
@staticmethod
def process_request(request):
"""Handle a request as Django middleware request handler."""
state = kvstore.get_default('firstboot_state', 0)
old_state = kvstore.get_default('firstboot_state', 0)
state = kvstore.get_default('setup_state', 0)
if state == 0 and old_state == 10:
state = 1
kvstore.set('setup_state', 1)
user_requests_firstboot = is_firstboot(request.path)
user_requests_login = request.path.startswith(reverse(settings.LOGIN_URL))
help_index_url = reverse('help:index')
@ -77,8 +82,10 @@ def get_firstboot_steps():
def next_step():
""" Returns the next first boot step required to run """
steps = get_firstboot_steps()
for step in steps:
global firstboot_steps
if len(firstboot_steps) == 0:
firstboot_steps = get_firstboot_steps()
for step in firstboot_steps:
done = kvstore.get_default(step.get('id'), 0)
if done == 0:
return step.get('url')
@ -90,3 +97,14 @@ def mark_step_done(id):
:param id: id of the firstboot step
"""
kvstore.set(id, 1)
global firstboot_steps
if len(firstboot_steps) == 0:
firstboot_steps = get_firstboot_steps()
setup_done = True
for step in firstboot_steps:
done = kvstore.get_default(step.get('id'), 0)
if done == 0:
setup_done = False
break
if setup_done:
kvstore.set('setup_state', 1)

View File

@ -15,19 +15,11 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from django.contrib import messages
from django.contrib.auth.models import User
from django.http.response import HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse_lazy
from django.utils.translation import ugettext as _
from django.views.generic import CreateView, FormView, TemplateView
from plinth import cfg
from plinth import kvstore
from plinth import network
from plinth.errors import DomainRegistrationError
from .forms import State1Form, State5Form
from .middleware import mark_step_done, next_step
@ -51,7 +43,7 @@ def state10(request):
"""
# Make sure that a user exists before finishing firstboot
if User.objects.all():
mark_step_done('firstboot_state')
mark_step_done('firstboot_state10')
connections = network.get_connection_list()

View File

@ -27,7 +27,6 @@ import logging
from plinth import cfg
from plinth.errors import ActionError, DomainRegistrationError
from plinth.modules.first_boot.forms import SubdomainWidget
from plinth.modules.pagekite.utils import PREDEFINED_SERVICES, run
from plinth.utils import format_lazy
@ -38,6 +37,7 @@ LOGGER = logging.getLogger(__name__)
class TrimmedCharField(forms.CharField):
"""Trim the contents of a CharField"""
def clean(self, value):
"""Clean and validate the field value"""
if value:
@ -46,6 +46,23 @@ class TrimmedCharField(forms.CharField):
return super(TrimmedCharField, self).clean(value)
class SubdomainWidget(forms.widgets.TextInput):
"""Append the domain to the subdomain bootstrap input field"""
def __init__(self, domain, *args, **kwargs):
"""Intialize the widget by storing the domain value."""
super().__init__(*args, **kwargs)
self.domain = domain
def render(self, *args, **kwargs):
"""Return the HTML for the widget."""
inputfield = super().render(*args, **kwargs)
return """<div class="input-group">
{0}
<span class="input-group-addon">{1}</span>
</div>""".format(inputfield, self.domain)
class ConfigurationForm(forms.Form):
"""Configure PageKite credentials and frontend"""
@ -191,7 +208,6 @@ class BaseCustomServiceForm(forms.Form):
class DeleteCustomServiceForm(BaseCustomServiceForm):
def delete(self, request):
service = self.convert_formdata_to_service(self.cleaned_data)
utils.run(['remove-service', '--service', json.dumps(service)])
@ -244,6 +260,8 @@ class AddCustomServiceForm(BaseCustomServiceForm):
messages.error(request, _('This service already exists'))
else:
raise
class State5Form(forms.Form):
"""Set up freedombox.me pagekite subdomain"""
DOMAIN_APPENDIX = '.freedombox.me'

View File

@ -21,7 +21,6 @@ from django.urls import reverse, reverse_lazy
from django.utils.translation import ugettext_lazy as _
from django.views.generic import View, TemplateView
from django.views.generic.edit import FormView
from plinth import kvstore
from plinth.errors import DomainRegistrationError
from . import utils

View File

@ -28,9 +28,10 @@ from django.utils.translation import ugettext as _, ugettext_lazy
from plinth import actions
from plinth.errors import ActionError
# Usernames used by optional services (that might not be installed yet).
from plinth.modules.security import set_restricted_access
# Usernames used by optional services (that might not be installed yet).
RESERVED_USERNAMES = [
'debian-deluged',
'Debian-minetest',
@ -55,6 +56,7 @@ GROUP_CHOICES = (
class ValidNewUsernameCheckMixin(object):
"""Mixin to check if a username is valid for created new user."""
def clean_username(self):
"""Check for username collisions with system users."""
username = self.cleaned_data['username']
@ -244,8 +246,10 @@ class UserChangePasswordForm(SetPasswordForm):
return user
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)

View File

@ -22,10 +22,9 @@ from django.contrib.messages.views import SuccessMessageMixin
from django.urls import reverse, reverse_lazy
from django.views.generic.edit import (CreateView, DeleteView, UpdateView,
FormView)
from django.views.generic import ListView, CreateView
from django.views.generic import ListView, CreateView as CV
from django.utils.translation import ugettext as _, ugettext_lazy
from plinth import cfg
from plinth import kvstore
from .forms import CreateUserForm, UserChangePasswordForm, UserUpdateForm, State1Form
@ -169,7 +168,7 @@ class UserChangePassword(ContextMixin, SuccessMessageMixin, FormView):
return super(UserChangePassword, self).form_valid(form)
class State1View(CreateView):
class State1View(CV):
"""Create user account and log the user in."""
template_name = 'firstboot_state1.html'
form_class = State1Form