Persist user timezone setting with middleware

This commit is contained in:
Christopher C. Wells 2020-02-14 12:35:02 -08:00
parent 8735655f23
commit c05ce176fc
4 changed files with 27 additions and 6 deletions

21
babybuddy/middleware.py Normal file
View File

@ -0,0 +1,21 @@
import pytz
from django.utils import timezone
class UserTimezoneMiddleware:
"""
Sets the timezone based on a user specific setting that falls back on
`settings.TIME_ZONE`.
"""
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
timezone_name = request.session.get('user_timezone')
if timezone_name:
try:
timezone.activate(pytz.timezone(timezone_name))
except pytz.UnknownTimeZoneError:
pass
return self.get_response(request)

View File

@ -87,7 +87,9 @@ def save_user_settings(sender, instance, **kwargs):
def user_logged_in_callback(sender, request, user, **kwargs): def user_logged_in_callback(sender, request, user, **kwargs):
if user.settings.language: if user.settings.language:
translation.activate(user.settings.language) translation.activate(user.settings.language)
# TODO: Change this behavior as session-based language is deprecated.
request.session[ request.session[
translation.LANGUAGE_SESSION_KEY] = user.settings.language translation.LANGUAGE_SESSION_KEY] = user.settings.language
if user.settings.timezone: if user.settings.timezone:
timezone.activate(user.settings.timezone) timezone.activate(user.settings.timezone)
request.session['user_timezone'] = user.settings.timezone

View File

@ -58,6 +58,7 @@ MIDDLEWARE = [
'whitenoise.middleware.WhiteNoiseMiddleware', 'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware', 'django.middleware.locale.LocaleMiddleware',
'babybuddy.middleware.UserTimezoneMiddleware',
'django.middleware.common.CommonMiddleware', 'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware', 'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware',

View File

@ -1,6 +1,4 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import pytz
from django.contrib import messages from django.contrib import messages
from django.contrib.auth import update_session_auth_hash from django.contrib.auth import update_session_auth_hash
from django.contrib.auth.forms import PasswordChangeForm from django.contrib.auth.forms import PasswordChangeForm
@ -9,18 +7,18 @@ from django.contrib.auth.models import User
from django.contrib.messages.views import SuccessMessageMixin 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 import timezone, translation
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 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, LANGUAGE_QUERY_PARAMETER from django.views.i18n import set_language
from django_filters.views import FilterView from django_filters.views import FilterView
from babybuddy import forms from babybuddy import forms
from babybuddy.mixins import PermissionRequired403Mixin, StaffOnlyMixin from babybuddy.mixins import PermissionRequired403Mixin, StaffOnlyMixin
from babybuddy.models import user_logged_in_callback
class RootRouter(LoginRequiredMixin, RedirectView): class RootRouter(LoginRequiredMixin, RedirectView):
@ -143,8 +141,7 @@ 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()
timezone.activate(pytz.timezone(user.settings.timezone)) user_logged_in_callback(UserSettings, request, user)
translation.activate(request.POST.get(LANGUAGE_QUERY_PARAMETER))
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, {