From 45b5769ce6232154d3c19569c56557441a7d40ad Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Thu, 23 Sep 2021 18:29:20 -0700 Subject: [PATCH] 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 Reviewed-by: James Valleroy --- plinth/modules/users/forms.py | 5 ----- plinth/modules/users/views.py | 14 +++++++++++++- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/plinth/modules/users/forms.py b/plinth/modules/users/forms.py index 6da76ec07..aa310ab44 100644 --- a/plinth/modules/users/forms.py +++ b/plinth/modules/users/forms.py @@ -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() diff --git a/plinth/modules/users/views.py b/plinth/modules/users/views.py index c7cb8bfc0..c0ea7986e 100644 --- a/plinth/modules/users/views.py +++ b/plinth/modules/users/views.py @@ -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.