Use cookie for language setting

Django 4.0 will deprecate the use of session for language setting.
This commit is contained in:
Christopher C. Wells 2020-01-04 08:09:28 -08:00
parent a11524a47d
commit d68410cdae
4 changed files with 7 additions and 50 deletions

View File

@ -65,6 +65,7 @@
</div> </div>
</div> </div>
</fieldset> </fieldset>
<input type="hidden" name="next" value="{% url 'babybuddy:user-settings' %}" />
<button type="submit" class="btn btn-primary">{% trans "Submit" %}</button> <button type="submit" class="btn btn-primary">{% trans "Submit" %}</button>
</form> </form>
</div> </div>

View File

@ -30,7 +30,8 @@ class FormsTestCase(TestCase):
'last_name': 'Name', 'last_name': 'Name',
'email': 'user@user.user', 'email': 'user@user.user',
'dashboard_refresh_rate': '', 'dashboard_refresh_rate': '',
'language': 'en' 'language': 'en',
'next': '/user/settings/'
} }
def test_change_password(self): def test_change_password(self):

View File

@ -1,45 +0,0 @@
# -*- coding: utf-8 -*-
from django.test import TestCase
from django.test import Client as HttpClient
from django.contrib.auth.models import User
from django.core.management import call_command
from faker import Factory
class TranslationTestCase(TestCase):
@classmethod
def setUpClass(cls):
super(TranslationTestCase, cls).setUpClass()
fake = Factory.create()
call_command('migrate', verbosity=0)
cls.c = HttpClient()
fake_user = fake.simple_profile()
cls.credentials = {
'username': fake_user['username'],
'password': fake.password()
}
cls.user = User.objects.create_user(
is_superuser=True, **cls.credentials)
cls.params_template = {
'first_name': 'User',
'last_name': 'Name',
'email': 'user@user.user',
'dashboard_refresh_rate': '',
'language': 'en'
}
cls.c.login(**cls.credentials)
def test_translation_fr(self):
params = self.params_template.copy()
params['language'] = 'fr'
page = self.c.post('/user/settings/', data=params, follow=True)
self.assertContains(page, 'Paramètres Utilisateur')
page = self.c.get('/welcome/')
self.assertContains(page, 'Bienvenue à Baby Buddy!')

View File

@ -8,11 +8,11 @@ from django.contrib.messages.views import SuccessMessageMixin
from django.shortcuts import redirect, render from django.shortcuts import redirect, render
from django.urls import reverse, reverse_lazy from django.urls import reverse, reverse_lazy
from django.utils.text import format_lazy from django.utils.text import format_lazy
from django.utils.translation import gettext as _, gettext_lazy from django.utils.translation import activate, gettext as _, gettext_lazy
from django.views.generic import View from django.views.generic import View
from django.views.generic.base import TemplateView, RedirectView from django.views.generic.base import TemplateView, RedirectView
from django.views.generic.edit import CreateView, UpdateView, DeleteView from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.views.i18n import set_language from django.views.i18n import set_language, LANGUAGE_QUERY_PARAMETER
from django_filters.views import FilterView from django_filters.views import FilterView
@ -140,9 +140,9 @@ class UserSettings(LoginRequiredMixin, View):
user_settings = form_settings.save(commit=False) user_settings = form_settings.save(commit=False)
user.settings = user_settings user.settings = user_settings
user.save() user.save()
set_language(request) activate(request.POST.get(LANGUAGE_QUERY_PARAMETER))
messages.success(request, _('Settings saved!')) messages.success(request, _('Settings saved!'))
return redirect('babybuddy:user-settings') return set_language(request)
return render(request, self.template_name, { return render(request, self.template_name, {
'user_form': form_user, 'user_form': form_user,
'settings_form': form_settings 'settings_form': form_settings