Skip to content

Commit

Permalink
fix: Language chosen in user preferences persists on subsequent reque…
Browse files Browse the repository at this point in the history
…sts (wagtail#4310)
  • Loading branch information
bmihelac committed Aug 27, 2019
1 parent e263aaf commit f494937
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ Changelog
* Fix: Output form media on add/edit image forms with custom models (Matt Westcott)
* Fix: Layout for the clear checkbox in default FileField widget (Mikalai Radchuk)
* Fix: Remove ASCII conversion from Postgres search backend, to support stemming in non-Latin alphabets (Pavel Denisov)
* Fix: Language chosen in user preferences persists on subsequent requests (Bojan Mihelac)
* Update release schedule info and move to wiki, linked from docs and readme (Matt Westcott)


2.6.1 (05.08.2019)
~~~~~~~~~~~~~~~~~~

Expand Down
14 changes: 9 additions & 5 deletions wagtail/admin/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from django.shortcuts import redirect
from django.urls import reverse
from django.utils.timezone import activate as activate_tz
from django.utils.translation import activate as activate_lang
from django.utils.translation import ugettext as _
from django.utils.translation import override

from wagtail.admin import messages
from wagtail.core.models import GroupPagePermission
Expand Down Expand Up @@ -154,13 +154,17 @@ def decorated_view(request, *args, **kwargs):
return reject_request(request)

if user.has_perms(['wagtailadmin.access_admin']):
preferred_language = None
if hasattr(user, 'wagtail_userprofile'):
language = user.wagtail_userprofile.get_preferred_language()
l18n.set_language(language)
activate_lang(language)
preferred_language = user.wagtail_userprofile.get_preferred_language()
l18n.set_language(preferred_language)
time_zone = user.wagtail_userprofile.get_current_time_zone()
activate_tz(time_zone)
return view_func(request, *args, **kwargs)
if preferred_language:
with override(preferred_language):
return view_func(request, *args, **kwargs)
else:
return view_func(request, *args, **kwargs)

if not request.is_ajax():
messages.error(request, _('You do not have permission to access the admin'))
Expand Down
10 changes: 10 additions & 0 deletions wagtail/admin/tests/test_account_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from django.core import mail
from django.test import TestCase, override_settings
from django.urls import reverse
from django.utils.translation import get_language

from wagtail.admin.locale import (
WAGTAILADMIN_PROVIDED_LANGUAGES, get_available_admin_languages, get_available_admin_time_zones)
Expand Down Expand Up @@ -411,6 +412,15 @@ def test_unset_language_preferences(self):
# Check that the current language is assumed as English
self.assertEqual(profile.get_preferred_language(), "en")

def test_language_preferences_reapplies_original_language(self):
post_data = {
'preferred_language': 'es'
}
response = self.client.post(reverse('wagtailadmin_account_language_preferences'), post_data)
self.assertRedirects(response, reverse('wagtailadmin_account'))

self.assertEqual(get_language(), "en")

def test_change_name(self):
"""
This tests that the change name view responds with a change name page
Expand Down
6 changes: 3 additions & 3 deletions wagtail/admin/views/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.shortcuts import redirect, render
from django.urls import reverse, reverse_lazy
from django.utils.translation import ugettext as _
from django.utils.translation import activate
from django.utils.translation import override

from wagtail.admin.forms.auth import LoginForm, PasswordResetForm
from wagtail.core import hooks
Expand Down Expand Up @@ -174,8 +174,8 @@ def language_preferences(request):
user_profile = form.save()
# This will set the language only for this request/thread
# (so that the 'success' messages is in the right language)
activate(user_profile.get_preferred_language())
messages.success(request, _("Your preferences have been updated."))
with override(user_profile.get_preferred_language()):
messages.success(request, _("Your preferences have been updated."))
return redirect('wagtailadmin_account')
else:
form = PreferredLanguageForm(instance=UserProfile.get_for_user(request.user))
Expand Down

0 comments on commit f494937

Please sign in to comment.