users: Help set language cookie when user profile is edited

This patch only ensures that response object is send along with set_language()
call. In later changes, response object can be used by set_language() to set the
language cookie.

Tests:

- Relevant functional tests pass.

- Edit current user's language. The language is immediately set.

- Edit another user's language. The language of the current session is not changed.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2021-09-23 18:29:20 -07:00 committed by James Valleroy
parent 79e5edb097
commit 45b5769ce6
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
2 changed files with 13 additions and 6 deletions

View File

@ -19,7 +19,6 @@ from plinth import actions
from plinth.errors import ActionError
from plinth.modules import first_boot
from plinth.modules.security import set_restricted_access
from plinth.translation import set_language
from plinth.utils import is_user_admin
from . import get_last_admin_user
@ -244,10 +243,6 @@ class UserUpdateForm(ValidNewUsernameCheckMixin, PasswordConfirmForm,
auth_username = self.request.user.username
confirm_password = self.cleaned_data['confirm_password']
# If user is updating their own profile then only translate the pages
if self.username == auth_username:
set_language(self.request, None, user.userprofile.language)
if commit:
user.save()
self.save_m2m()

View File

@ -13,7 +13,7 @@ from django.utils.translation import gettext_lazy
from django.views.generic.edit import (CreateView, DeleteView, FormView,
UpdateView)
from plinth import actions
from plinth import actions, translation
from plinth.errors import ActionError
from plinth.modules import first_boot
from plinth.utils import is_user_admin
@ -108,6 +108,18 @@ class UserUpdate(ContextMixin, SuccessMessageMixin, UpdateView):
"""Return the URL to redirect to in case of successful updation."""
return reverse('users:edit', kwargs={'slug': self.object.username})
def form_valid(self, form):
"""Set the user language if necessary."""
response = super().form_valid(form)
# If user is updating their own profile then set the language for
# current session too.
if self.object.username == self.request.user.username:
translation.set_language(self.request, response,
self.request.user.userprofile.language)
return response
class UserDelete(ContextMixin, DeleteView):
"""Handle deleting users, showing a confirmation dialog first.