From 83b4edf95d48d518c348fdb9b5cebc6735889845 Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Tue, 16 Apr 2019 21:41:55 -0700 Subject: [PATCH] Store and retrieve language in user Setting. --- babybuddy/forms.py | 2 +- babybuddy/migrations/0004_settings_language.py | 18 ++++++++++++++++++ babybuddy/models.py | 16 +++++++++++++++- babybuddy/settings/base.py | 2 +- .../babybuddy/user_settings_form.html | 18 +++--------------- 5 files changed, 38 insertions(+), 18 deletions(-) create mode 100644 babybuddy/migrations/0004_settings_language.py diff --git a/babybuddy/forms.py b/babybuddy/forms.py index 017a2191..004d7380 100644 --- a/babybuddy/forms.py +++ b/babybuddy/forms.py @@ -41,4 +41,4 @@ class UserPasswordForm(PasswordChangeForm): class UserSettingsForm(forms.ModelForm): class Meta: model = Settings - fields = ['dashboard_refresh_rate'] + fields = ['dashboard_refresh_rate', 'language'] diff --git a/babybuddy/migrations/0004_settings_language.py b/babybuddy/migrations/0004_settings_language.py new file mode 100644 index 00000000..d473b65b --- /dev/null +++ b/babybuddy/migrations/0004_settings_language.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2 on 2019-04-17 04:02 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('babybuddy', '0003_add_refresh_help_text'), + ] + + operations = [ + migrations.AddField( + model_name='settings', + name='language', + field=models.CharField(choices=[], default='en', max_length=255, verbose_name='Language'), + ), + ] diff --git a/babybuddy/models.py b/babybuddy/models.py index 94d515e2..c9c952c6 100644 --- a/babybuddy/models.py +++ b/babybuddy/models.py @@ -1,11 +1,13 @@ # -*- coding: utf-8 -*- +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.timezone import timedelta from django.utils.text import format_lazy -from django.utils.translation import gettext_lazy as _ +from django.utils.translation import activate, gettext_lazy as _, LANGUAGE_SESSION_KEY from rest_framework.authtoken.models import Token @@ -30,6 +32,12 @@ class Settings(models.Model): (timedelta(minutes=15), _('15 min.')), (timedelta(minutes=30), _('30 min.')), ]) + language = models.CharField( + choices=settings.LANGUAGES, + default=settings.LANGUAGE_CODE, + max_length=255, + verbose_name=_('Language') + ) def __str__(self): return str(format_lazy(_('{user}\'s Settings'), user=self.user)) @@ -65,3 +73,9 @@ 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): + activate(user.settings.language) + request.session[LANGUAGE_SESSION_KEY] = user.settings.language diff --git a/babybuddy/settings/base.py b/babybuddy/settings/base.py index 7299d5c4..94b9e126 100644 --- a/babybuddy/settings/base.py +++ b/babybuddy/settings/base.py @@ -110,7 +110,7 @@ LOGOUT_REDIRECT_URL = '/login/' # Internationalization # https://docs.djangoproject.com/en/1.11/topics/i18n/ -LANGUAGE_CODE = 'en-us' +LANGUAGE_CODE = 'en' TIME_ZONE = os.environ.get('TIME_ZONE', 'Etc/UTC') diff --git a/babybuddy/templates/babybuddy/user_settings_form.html b/babybuddy/templates/babybuddy/user_settings_form.html index a4e535fb..b459d511 100644 --- a/babybuddy/templates/babybuddy/user_settings_form.html +++ b/babybuddy/templates/babybuddy/user_settings_form.html @@ -42,21 +42,9 @@ {% endwith %}
- -
- -
+ {% with form_settings.language as field %} + {% include 'babybuddy/form_field.html' %} + {% endwith %}