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:
Sunil Mohan Adapa authored and James Valleroy committed 2018-02-21 21:08:33 +01:00
1 parent 842000a01f
commit 6090d5c37d
3 files changed
+27 -13

No files matched your search

+18 -9
View File
@@ -22,12 +22,11 @@ from django.contrib import auth, messages
from django.contrib.auth.forms import SetPasswordForm, UserCreationForm
from django.contrib.auth.models import Group, User
from django.core.exceptions import ValidationError
from django.utils import translation
from django.utils.translation import ugettext as _
from django.utils.translation import ugettext_lazy
import plinth.forms
from plinth import actions, module_loader
from plinth import forms as plinthForms
from plinth.errors import ActionError
from plinth.modules import first_boot, users
from plinth.modules.security import set_restricted_access
@@ -76,7 +75,9 @@ class ValidNewUsernameCheckMixin(object):
return True
class CreateUserForm(ValidNewUsernameCheckMixin, UserCreationForm, plinthForms.LanguageSelectionForm):
class CreateUserForm(ValidNewUsernameCheckMixin,
plinth.forms.LanguageSelectionFormMixin,
UserCreationForm):
"""Custom user create form.
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 '
'administrative privileges (sudo).'))
language = plinth.forms.LanguageSelectionFormMixin.language
class Meta(UserCreationForm.Meta):
"""Metadata to control automatic form building."""
fields = ('username', 'password1', 'password2', 'groups', 'language')
def __init__(self, request, *args, **kwargs):
@@ -105,9 +109,11 @@ class CreateUserForm(ValidNewUsernameCheckMixin, UserCreationForm, plinthForms.L
def save(self, commit=True):
"""Save the user model and create LDAP user if required."""
user = super(CreateUserForm, self).save(commit)
UserProfile(user=user, preferred_language=self.cleaned_data['language']).save()
if commit:
user.userprofile.language = self.cleaned_data['language']
user.userprofile.save()
try:
actions.superuser_run('users', [
'create-user', user.get_username()
@@ -133,7 +139,8 @@ class CreateUserForm(ValidNewUsernameCheckMixin, UserCreationForm, plinthForms.L
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."""
ssh_keys = forms.CharField(
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 '
'ignored.'))
language = plinth.forms.LanguageSelectionFormMixin.language
class Meta:
"""Metadata to control automatic form building."""
fields = ('username', 'groups', 'ssh_keys', 'language', 'is_active')
@@ -181,15 +190,16 @@ class UserUpdateForm(ValidNewUsernameCheckMixin, forms.ModelForm, plinthForms.La
def save(self, commit=True):
"""Update LDAP user name and groups after saving user model."""
user = super(UserUpdateForm, self).save(commit=False)
user.userprofile.preferred_language = self.cleaned_data['language']
user.userprofile.save()
user.save()
# Profile is auto saved with user object
user.userprofile.language = self.cleaned_data['language']
# If user is updating their own profile then only translate the pages
if self.username == self.request.user.username:
set_language(self.request, None, user.userprofile.language)
if commit:
user.save()
output = actions.superuser_run('users',
['get-user-groups', self.username])
old_groups = output.strip().split('\n')
@@ -274,7 +284,6 @@ class FirstBootForm(ValidNewUsernameCheckMixin, auth.forms.UserCreationForm):
def save(self, commit=True):
"""Create and log the user in."""
user = super().save(commit=commit)
UserProfile(user=user).save()
if commit:
first_boot.mark_step_done('users_firstboot')
+1 -2
View File
@@ -104,8 +104,7 @@ class UserUpdate(ContextMixin, SuccessMessageMixin, UpdateView):
ssh_keys = actions.superuser_run(
'ssh', ['get-keys', '--username', self.object.username])
initial['ssh_keys'] = ssh_keys.strip()
user_being_edited = User.objects.get(username=self.kwargs['slug'])
initial['language'] = user_being_edited.userprofile.preferred_language
initial['language'] = self.object.userprofile.language
except ActionError:
pass