mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-05-13 10:30:16 +00:00
locale: Split the language selection form into a mixin
- It may not be a good thing for the user created/edit forms to inherit from Django Form and ModelForm at the same time. So, simply by introducing a minimal mixin. - Save only when committing. - Use auto-saving feature. Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
parent
842000a01f
commit
6090d5c37d
@ -52,8 +52,8 @@ class DomainSelectionForm(forms.Form):
|
|||||||
'changed later.'), choices=[])
|
'changed later.'), choices=[])
|
||||||
|
|
||||||
|
|
||||||
class LanguageSelectionForm(forms.Form):
|
class LanguageSelectionFormMixin:
|
||||||
"""Form for selecting the user's preferred language """
|
"""Form mixin for selecting the user's preferred language."""
|
||||||
|
|
||||||
language = forms.ChoiceField(
|
language = forms.ChoiceField(
|
||||||
label=_('Language'),
|
label=_('Language'),
|
||||||
@ -76,3 +76,9 @@ class LanguageSelectionForm(forms.Form):
|
|||||||
get_language_info(language_code)['name_local']))
|
get_language_info(language_code)['name_local']))
|
||||||
|
|
||||||
self.fields['language'].choices = supported_languages
|
self.fields['language'].choices = supported_languages
|
||||||
|
|
||||||
|
|
||||||
|
class LanguageSelectionForm(LanguageSelectionFormMixin, forms.Form):
|
||||||
|
"""Language selection form."""
|
||||||
|
|
||||||
|
language = LanguageSelectionFormMixin.language
|
||||||
|
|||||||
@ -22,12 +22,11 @@ from django.contrib import auth, messages
|
|||||||
from django.contrib.auth.forms import SetPasswordForm, UserCreationForm
|
from django.contrib.auth.forms import SetPasswordForm, UserCreationForm
|
||||||
from django.contrib.auth.models import Group, User
|
from django.contrib.auth.models import Group, User
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
from django.utils import translation
|
|
||||||
from django.utils.translation import ugettext as _
|
from django.utils.translation import ugettext as _
|
||||||
from django.utils.translation import ugettext_lazy
|
from django.utils.translation import ugettext_lazy
|
||||||
|
|
||||||
|
import plinth.forms
|
||||||
from plinth import actions, module_loader
|
from plinth import actions, module_loader
|
||||||
from plinth import forms as plinthForms
|
|
||||||
from plinth.errors import ActionError
|
from plinth.errors import ActionError
|
||||||
from plinth.modules import first_boot, users
|
from plinth.modules import first_boot, users
|
||||||
from plinth.modules.security import set_restricted_access
|
from plinth.modules.security import set_restricted_access
|
||||||
@ -76,7 +75,9 @@ class ValidNewUsernameCheckMixin(object):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
class CreateUserForm(ValidNewUsernameCheckMixin, UserCreationForm, plinthForms.LanguageSelectionForm):
|
class CreateUserForm(ValidNewUsernameCheckMixin,
|
||||||
|
plinth.forms.LanguageSelectionFormMixin,
|
||||||
|
UserCreationForm):
|
||||||
"""Custom user create form.
|
"""Custom user create form.
|
||||||
|
|
||||||
Include options to add user to groups.
|
Include options to add user to groups.
|
||||||
@ -93,7 +94,10 @@ class CreateUserForm(ValidNewUsernameCheckMixin, UserCreationForm, plinthForms.L
|
|||||||
'log in to the system through SSH and have '
|
'log in to the system through SSH and have '
|
||||||
'administrative privileges (sudo).'))
|
'administrative privileges (sudo).'))
|
||||||
|
|
||||||
|
language = plinth.forms.LanguageSelectionFormMixin.language
|
||||||
|
|
||||||
class Meta(UserCreationForm.Meta):
|
class Meta(UserCreationForm.Meta):
|
||||||
|
"""Metadata to control automatic form building."""
|
||||||
fields = ('username', 'password1', 'password2', 'groups', 'language')
|
fields = ('username', 'password1', 'password2', 'groups', 'language')
|
||||||
|
|
||||||
def __init__(self, request, *args, **kwargs):
|
def __init__(self, request, *args, **kwargs):
|
||||||
@ -105,9 +109,11 @@ class CreateUserForm(ValidNewUsernameCheckMixin, UserCreationForm, plinthForms.L
|
|||||||
def save(self, commit=True):
|
def save(self, commit=True):
|
||||||
"""Save the user model and create LDAP user if required."""
|
"""Save the user model and create LDAP user if required."""
|
||||||
user = super(CreateUserForm, self).save(commit)
|
user = super(CreateUserForm, self).save(commit)
|
||||||
UserProfile(user=user, preferred_language=self.cleaned_data['language']).save()
|
|
||||||
|
|
||||||
if commit:
|
if commit:
|
||||||
|
user.userprofile.language = self.cleaned_data['language']
|
||||||
|
user.userprofile.save()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
actions.superuser_run('users', [
|
actions.superuser_run('users', [
|
||||||
'create-user', user.get_username()
|
'create-user', user.get_username()
|
||||||
@ -133,7 +139,8 @@ class CreateUserForm(ValidNewUsernameCheckMixin, UserCreationForm, plinthForms.L
|
|||||||
return user
|
return user
|
||||||
|
|
||||||
|
|
||||||
class UserUpdateForm(ValidNewUsernameCheckMixin, forms.ModelForm, plinthForms.LanguageSelectionForm):
|
class UserUpdateForm(ValidNewUsernameCheckMixin,
|
||||||
|
plinth.forms.LanguageSelectionFormMixin, forms.ModelForm):
|
||||||
"""When user info is changed, also updates LDAP user."""
|
"""When user info is changed, also updates LDAP user."""
|
||||||
ssh_keys = forms.CharField(
|
ssh_keys = forms.CharField(
|
||||||
label=ugettext_lazy('SSH Keys'),
|
label=ugettext_lazy('SSH Keys'),
|
||||||
@ -144,6 +151,8 @@ class UserUpdateForm(ValidNewUsernameCheckMixin, forms.ModelForm, plinthForms.La
|
|||||||
'line. Blank lines and lines starting with # will be '
|
'line. Blank lines and lines starting with # will be '
|
||||||
'ignored.'))
|
'ignored.'))
|
||||||
|
|
||||||
|
language = plinth.forms.LanguageSelectionFormMixin.language
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
"""Metadata to control automatic form building."""
|
"""Metadata to control automatic form building."""
|
||||||
fields = ('username', 'groups', 'ssh_keys', 'language', 'is_active')
|
fields = ('username', 'groups', 'ssh_keys', 'language', 'is_active')
|
||||||
@ -181,15 +190,16 @@ class UserUpdateForm(ValidNewUsernameCheckMixin, forms.ModelForm, plinthForms.La
|
|||||||
def save(self, commit=True):
|
def save(self, commit=True):
|
||||||
"""Update LDAP user name and groups after saving user model."""
|
"""Update LDAP user name and groups after saving user model."""
|
||||||
user = super(UserUpdateForm, self).save(commit=False)
|
user = super(UserUpdateForm, self).save(commit=False)
|
||||||
user.userprofile.preferred_language = self.cleaned_data['language']
|
# Profile is auto saved with user object
|
||||||
user.userprofile.save()
|
user.userprofile.language = self.cleaned_data['language']
|
||||||
user.save()
|
|
||||||
|
|
||||||
# If user is updating their own profile then only translate the pages
|
# If user is updating their own profile then only translate the pages
|
||||||
if self.username == self.request.user.username:
|
if self.username == self.request.user.username:
|
||||||
set_language(self.request, None, user.userprofile.language)
|
set_language(self.request, None, user.userprofile.language)
|
||||||
|
|
||||||
if commit:
|
if commit:
|
||||||
|
user.save()
|
||||||
|
|
||||||
output = actions.superuser_run('users',
|
output = actions.superuser_run('users',
|
||||||
['get-user-groups', self.username])
|
['get-user-groups', self.username])
|
||||||
old_groups = output.strip().split('\n')
|
old_groups = output.strip().split('\n')
|
||||||
@ -274,7 +284,6 @@ class FirstBootForm(ValidNewUsernameCheckMixin, auth.forms.UserCreationForm):
|
|||||||
def save(self, commit=True):
|
def save(self, commit=True):
|
||||||
"""Create and log the user in."""
|
"""Create and log the user in."""
|
||||||
user = super().save(commit=commit)
|
user = super().save(commit=commit)
|
||||||
UserProfile(user=user).save()
|
|
||||||
if commit:
|
if commit:
|
||||||
first_boot.mark_step_done('users_firstboot')
|
first_boot.mark_step_done('users_firstboot')
|
||||||
|
|
||||||
|
|||||||
@ -104,8 +104,7 @@ class UserUpdate(ContextMixin, SuccessMessageMixin, UpdateView):
|
|||||||
ssh_keys = actions.superuser_run(
|
ssh_keys = actions.superuser_run(
|
||||||
'ssh', ['get-keys', '--username', self.object.username])
|
'ssh', ['get-keys', '--username', self.object.username])
|
||||||
initial['ssh_keys'] = ssh_keys.strip()
|
initial['ssh_keys'] = ssh_keys.strip()
|
||||||
user_being_edited = User.objects.get(username=self.kwargs['slug'])
|
initial['language'] = self.object.userprofile.language
|
||||||
initial['language'] = user_being_edited.userprofile.preferred_language
|
|
||||||
except ActionError:
|
except ActionError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user