mirror of https://github.com/snachodog/mybuddy.git
Remove deprecated use of session-based language handling
Session-based language handling is deprecated in Django 3.x and will be removed in 4.x. https://docs.djangoproject.com/en/3.2/topics/i18n/translation/#explicitly-setting-the-active-language
This commit is contained in:
parent
9e2048b26c
commit
0efe0e4f0b
|
@ -9,16 +9,17 @@ from django.utils import timezone
|
||||||
class UserTimezoneMiddleware:
|
class UserTimezoneMiddleware:
|
||||||
"""
|
"""
|
||||||
Sets the timezone based on a user specific setting that falls back on
|
Sets the timezone based on a user specific setting that falls back on
|
||||||
`settings.TIME_ZONE`.
|
`settings.TIME_ZONE`. This middleware must run after
|
||||||
|
`django.contrib.auth.middleware.AuthenticationMiddleware` because it uses
|
||||||
|
the request.user object.
|
||||||
"""
|
"""
|
||||||
def __init__(self, get_response):
|
def __init__(self, get_response):
|
||||||
self.get_response = get_response
|
self.get_response = get_response
|
||||||
|
|
||||||
def __call__(self, request):
|
def __call__(self, request):
|
||||||
timezone_name = request.session.get('user_timezone')
|
if hasattr(request.user, 'settings') and request.user.settings.timezone:
|
||||||
if timezone_name:
|
|
||||||
try:
|
try:
|
||||||
timezone.activate(pytz.timezone(timezone_name))
|
timezone.activate(pytz.timezone(request.user.settings.timezone))
|
||||||
except pytz.UnknownTimeZoneError:
|
except pytz.UnknownTimeZoneError:
|
||||||
pass
|
pass
|
||||||
return self.get_response(request)
|
return self.get_response(request)
|
||||||
|
|
|
@ -3,11 +3,10 @@ import pytz
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.contrib.auth.signals import user_logged_in
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.db.models.signals import post_save
|
from django.db.models.signals import post_save
|
||||||
from django.dispatch import receiver
|
from django.dispatch import receiver
|
||||||
from django.utils import timezone, translation
|
from django.utils import timezone
|
||||||
from django.utils.text import format_lazy
|
from django.utils.text import format_lazy
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
|
@ -101,15 +100,3 @@ def create_user_settings(sender, instance, created, **kwargs):
|
||||||
@receiver(post_save, sender=User)
|
@receiver(post_save, sender=User)
|
||||||
def save_user_settings(sender, instance, **kwargs):
|
def save_user_settings(sender, instance, **kwargs):
|
||||||
instance.settings.save()
|
instance.settings.save()
|
||||||
|
|
||||||
|
|
||||||
@receiver(user_logged_in)
|
|
||||||
def user_logged_in_callback(sender, request, user, **kwargs):
|
|
||||||
if user.settings.language:
|
|
||||||
translation.activate(user.settings.language)
|
|
||||||
# TODO: Change this behavior as session-based language is deprecated.
|
|
||||||
request.session[
|
|
||||||
translation.LANGUAGE_SESSION_KEY] = user.settings.language
|
|
||||||
if user.settings.timezone:
|
|
||||||
timezone.activate(user.settings.timezone)
|
|
||||||
request.session['user_timezone'] = user.settings.timezone
|
|
||||||
|
|
|
@ -61,11 +61,11 @@ MIDDLEWARE = [
|
||||||
'whitenoise.middleware.WhiteNoiseMiddleware',
|
'whitenoise.middleware.WhiteNoiseMiddleware',
|
||||||
'django.contrib.sessions.middleware.SessionMiddleware',
|
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||||
'babybuddy.middleware.RollingSessionMiddleware',
|
'babybuddy.middleware.RollingSessionMiddleware',
|
||||||
'django.middleware.locale.LocaleMiddleware',
|
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||||
'babybuddy.middleware.UserTimezoneMiddleware',
|
'babybuddy.middleware.UserTimezoneMiddleware',
|
||||||
|
'django.middleware.locale.LocaleMiddleware',
|
||||||
'django.middleware.common.CommonMiddleware',
|
'django.middleware.common.CommonMiddleware',
|
||||||
'django.middleware.csrf.CsrfViewMiddleware',
|
'django.middleware.csrf.CsrfViewMiddleware',
|
||||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
|
||||||
'django.contrib.messages.middleware.MessageMiddleware',
|
'django.contrib.messages.middleware.MessageMiddleware',
|
||||||
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||||
'axes.middleware.AxesMiddleware',
|
'axes.middleware.AxesMiddleware',
|
||||||
|
@ -161,14 +161,14 @@ TIME_ZONE = os.environ.get('TIME_ZONE') or 'UTC'
|
||||||
|
|
||||||
USE_I18N = True
|
USE_I18N = True
|
||||||
|
|
||||||
LANGUAGE_CODE = 'en'
|
LANGUAGE_CODE = 'en-US'
|
||||||
|
|
||||||
LOCALE_PATHS = [
|
LOCALE_PATHS = [
|
||||||
os.path.join(BASE_DIR, "locale"),
|
os.path.join(BASE_DIR, "locale"),
|
||||||
]
|
]
|
||||||
|
|
||||||
LANGUAGES = [
|
LANGUAGES = [
|
||||||
('en', _('English (US)')),
|
('en-US', _('English (US)')),
|
||||||
('en-GB', _('English (UK)')),
|
('en-GB', _('English (UK)')),
|
||||||
('nl', _('Dutch')),
|
('nl', _('Dutch')),
|
||||||
('fr', _('French')),
|
('fr', _('French')),
|
||||||
|
|
|
@ -18,7 +18,6 @@ from django_filters.views import FilterView
|
||||||
from babybuddy import forms
|
from babybuddy import forms
|
||||||
from babybuddy.mixins import LoginRequiredMixin, PermissionRequiredMixin, \
|
from babybuddy.mixins import LoginRequiredMixin, PermissionRequiredMixin, \
|
||||||
StaffOnlyMixin
|
StaffOnlyMixin
|
||||||
from babybuddy.models import user_logged_in_callback
|
|
||||||
|
|
||||||
|
|
||||||
class RootRouter(LoginRequiredMixin, RedirectView):
|
class RootRouter(LoginRequiredMixin, RedirectView):
|
||||||
|
@ -145,7 +144,6 @@ 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()
|
||||||
user_logged_in_callback(UserSettings, request, user)
|
|
||||||
messages.success(request, _('Settings saved!'))
|
messages.success(request, _('Settings saved!'))
|
||||||
return set_language(request)
|
return set_language(request)
|
||||||
return render(request, self.template_name, {
|
return render(request, self.template_name, {
|
||||||
|
|
Loading…
Reference in New Issue