Add user configurable timezone setting

This commit is contained in:
Christopher C. Wells 2020-02-14 09:22:25 -08:00 committed by Christopher Charbonneau Wells
parent ea6476f9ca
commit 1514e77f8c
5 changed files with 54 additions and 16 deletions

View File

@ -41,4 +41,4 @@ class UserPasswordForm(PasswordChangeForm):
class UserSettingsForm(forms.ModelForm): class UserSettingsForm(forms.ModelForm):
class Meta: class Meta:
model = Settings model = Settings
fields = ['dashboard_refresh_rate', 'language'] fields = ['dashboard_refresh_rate', 'language', 'timezone']

File diff suppressed because one or more lines are too long

View File

@ -1,13 +1,14 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
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.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.timezone import timedelta from django.utils import timezone, translation
from django.utils.text import format_lazy from django.utils.text import format_lazy
from django.utils import translation
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from rest_framework.authtoken.models import Token from rest_framework.authtoken.models import Token
@ -21,17 +22,17 @@ class Settings(models.Model):
'support refresh on focus.'), 'support refresh on focus.'),
blank=True, blank=True,
null=True, null=True,
default=timedelta(minutes=1), default=timezone.timedelta(minutes=1),
choices=[ choices=[
(None, _('disabled')), (None, _('disabled')),
(timedelta(minutes=1), _('1 min.')), (timezone.timedelta(minutes=1), _('1 min.')),
(timedelta(minutes=2), _('2 min.')), (timezone.timedelta(minutes=2), _('2 min.')),
(timedelta(minutes=3), _('3 min.')), (timezone.timedelta(minutes=3), _('3 min.')),
(timedelta(minutes=4), _('4 min.')), (timezone.timedelta(minutes=4), _('4 min.')),
(timedelta(minutes=5), _('5 min.')), (timezone.timedelta(minutes=5), _('5 min.')),
(timedelta(minutes=10), _('10 min.')), (timezone.timedelta(minutes=10), _('10 min.')),
(timedelta(minutes=15), _('15 min.')), (timezone.timedelta(minutes=15), _('15 min.')),
(timedelta(minutes=30), _('30 min.')), (timezone.timedelta(minutes=30), _('30 min.')),
]) ])
language = models.CharField( language = models.CharField(
choices=settings.LANGUAGES, choices=settings.LANGUAGES,
@ -39,6 +40,12 @@ class Settings(models.Model):
max_length=255, max_length=255,
verbose_name=_('Language') verbose_name=_('Language')
) )
timezone = models.CharField(
choices=tuple(zip(pytz.all_timezones, pytz.all_timezones)),
default=timezone.get_default_timezone_name(),
max_length=100,
verbose_name=_('Timezone')
)
def __str__(self): def __str__(self):
return str(format_lazy(_('{user}\'s Settings'), user=self.user)) return str(format_lazy(_('{user}\'s Settings'), user=self.user))
@ -78,5 +85,9 @@ def save_user_settings(sender, instance, **kwargs):
@receiver(user_logged_in) @receiver(user_logged_in)
def user_logged_in_callback(sender, request, user, **kwargs): def user_logged_in_callback(sender, request, user, **kwargs):
translation.activate(user.settings.language) if user.settings.language:
request.session[translation.LANGUAGE_SESSION_KEY] = user.settings.language translation.activate(user.settings.language)
request.session[
translation.LANGUAGE_SESSION_KEY] = user.settings.language
if user.settings.timezone:
timezone.activate(user.settings.timezone)

View File

@ -51,6 +51,11 @@
{% include 'babybuddy/form_field.html' %} {% include 'babybuddy/form_field.html' %}
{% endwith %} {% endwith %}
</div> </div>
<div class="form-group row">
{% with form_settings.timezone as field %}
{% include 'babybuddy/form_field.html' %}
{% endwith %}
</div>
</fieldset> </fieldset>
<fieldset> <fieldset>
<legend>{% trans "Dashboard" %}</legend> <legend>{% trans "Dashboard" %}</legend>

View File

@ -1,4 +1,6 @@
# -*- 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
@ -7,8 +9,9 @@ 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 activate, 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
@ -140,7 +143,8 @@ 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()
activate(request.POST.get(LANGUAGE_QUERY_PARAMETER)) timezone.activate(pytz.timezone(user.settings.timezone))
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, {