Set user language as active language on login.

This commit is contained in:
Santiago Lamora 2020-01-23 17:37:08 +01:00
parent 24729bf6b7
commit 9866f00d7f
3 changed files with 19 additions and 2 deletions

View File

@ -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

View File

@ -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):

View File

@ -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()