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:
|
||||
"""
|
||||
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):
|
||||
self.get_response = get_response
|
||||
|
||||
def __call__(self, request):
|
||||
timezone_name = request.session.get('user_timezone')
|
||||
if timezone_name:
|
||||
if hasattr(request.user, 'settings') and request.user.settings.timezone:
|
||||
try:
|
||||
timezone.activate(pytz.timezone(timezone_name))
|
||||
timezone.activate(pytz.timezone(request.user.settings.timezone))
|
||||
except pytz.UnknownTimeZoneError:
|
||||
pass
|
||||
return self.get_response(request)
|
||||
|
|
|
@ -3,11 +3,10 @@ import pytz
|
|||
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.models import User
|
||||
from django.contrib.auth.signals import user_logged_in
|
||||
from django.db import models
|
||||
from django.db.models.signals import post_save
|
||||
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.translation import gettext_lazy as _
|
||||
|
||||
|
@ -101,15 +100,3 @@ def create_user_settings(sender, instance, created, **kwargs):
|
|||
@receiver(post_save, sender=User)
|
||||
def save_user_settings(sender, instance, **kwargs):
|
||||
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',
|
||||
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||
'babybuddy.middleware.RollingSessionMiddleware',
|
||||
'django.middleware.locale.LocaleMiddleware',
|
||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||
'babybuddy.middleware.UserTimezoneMiddleware',
|
||||
'django.middleware.locale.LocaleMiddleware',
|
||||
'django.middleware.common.CommonMiddleware',
|
||||
'django.middleware.csrf.CsrfViewMiddleware',
|
||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||
'django.contrib.messages.middleware.MessageMiddleware',
|
||||
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||
'axes.middleware.AxesMiddleware',
|
||||
|
@ -161,14 +161,14 @@ TIME_ZONE = os.environ.get('TIME_ZONE') or 'UTC'
|
|||
|
||||
USE_I18N = True
|
||||
|
||||
LANGUAGE_CODE = 'en'
|
||||
LANGUAGE_CODE = 'en-US'
|
||||
|
||||
LOCALE_PATHS = [
|
||||
os.path.join(BASE_DIR, "locale"),
|
||||
]
|
||||
|
||||
LANGUAGES = [
|
||||
('en', _('English (US)')),
|
||||
('en-US', _('English (US)')),
|
||||
('en-GB', _('English (UK)')),
|
||||
('nl', _('Dutch')),
|
||||
('fr', _('French')),
|
||||
|
|
|
@ -18,7 +18,6 @@ from django_filters.views import FilterView
|
|||
from babybuddy import forms
|
||||
from babybuddy.mixins import LoginRequiredMixin, PermissionRequiredMixin, \
|
||||
StaffOnlyMixin
|
||||
from babybuddy.models import user_logged_in_callback
|
||||
|
||||
|
||||
class RootRouter(LoginRequiredMixin, RedirectView):
|
||||
|
@ -145,7 +144,6 @@ class UserSettings(LoginRequiredMixin, View):
|
|||
user_settings = form_settings.save(commit=False)
|
||||
user.settings = user_settings
|
||||
user.save()
|
||||
user_logged_in_callback(UserSettings, request, user)
|
||||
messages.success(request, _('Settings saved!'))
|
||||
return set_language(request)
|
||||
return render(request, self.template_name, {
|
||||
|
|
Loading…
Reference in New Issue