From 9866f00d7fdd13ea15e22612170d32a09ae33afb Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Thu, 23 Jan 2020 17:37:08 +0100 Subject: [PATCH] Set user language as active language on login. --- musician/forms.py | 1 + musician/models.py | 8 +++++++- musician/views.py | 12 +++++++++++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/musician/forms.py b/musician/forms.py index 5fcafbe..7a66a00 100644 --- a/musician/forms.py +++ b/musician/forms.py @@ -17,5 +17,6 @@ class LoginForm(AuthenticationForm): else: self.username = username self.token = orchestra.auth_token + self.user = orchestra.retrieve_profile() return self.cleaned_data diff --git a/musician/models.py b/musician/models.py index a0c058f..a9b9698 100644 --- a/musician/models.py +++ b/musician/models.py @@ -109,14 +109,20 @@ class UserAccount(OrchestraModel): @classmethod def new_from_json(cls, data, **kwargs): billing = None + language = None last_login = None if 'billcontact' in data: billing = BillingContact.new_from_json(data['billcontact']) + # Django expects that language code is lowercase + if 'language' in data: + language = data['language'].lower() + if 'last_login' in data: last_login = parse_datetime(data['last_login']) - return super().new_from_json(data=data, billing=billing, last_login=last_login) + + return super().new_from_json(data=data, billing=billing, language=language, last_login=last_login) class DatabaseUser(OrchestraModel): diff --git a/musician/views.py b/musician/views.py index e96fa24..102a66d 100644 --- a/musician/views.py +++ b/musician/views.py @@ -1,9 +1,11 @@ from itertools import groupby +from django.conf import settings from django.core.exceptions import ImproperlyConfigured from django.http import HttpResponse, HttpResponseRedirect from django.shortcuts import render from django.urls import reverse_lazy +from django.utils import translation from django.utils.http import is_safe_url from django.utils.translation import gettext_lazy as _ from django.views import View @@ -305,7 +307,15 @@ class LoginView(FormView): def form_valid(self, form): """Security check complete. Log the user in.""" auth_login(self.request, form.username, form.token) - return HttpResponseRedirect(self.get_success_url()) + + # set user language as active language + user_language = form.user.language + translation.activate(user_language) + + response = HttpResponseRedirect(self.get_success_url()) + response.set_cookie(settings.LANGUAGE_COOKIE_NAME, user_language) + + return response def get_success_url(self): url = self.get_redirect_url()