mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-10 01:28:16 -06:00
16995 Move language handling to Middleware
This commit is contained in:
parent
6b219a279b
commit
7090cbbb46
@ -121,10 +121,6 @@ class LoginView(View):
|
|||||||
|
|
||||||
response = self.redirect_to_next(request, logger)
|
response = self.redirect_to_next(request, logger)
|
||||||
|
|
||||||
# Set the user's preferred language (if any)
|
|
||||||
if language := request.user.config.get('locale.language'):
|
|
||||||
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, language, max_age=request.session.get_expiry_age())
|
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
@ -8,6 +8,7 @@ from django.core.exceptions import ImproperlyConfigured
|
|||||||
from django.db import connection, ProgrammingError
|
from django.db import connection, ProgrammingError
|
||||||
from django.db.utils import InternalError
|
from django.db.utils import InternalError
|
||||||
from django.http import Http404, HttpResponseRedirect
|
from django.http import Http404, HttpResponseRedirect
|
||||||
|
from django.utils import translation
|
||||||
|
|
||||||
from netbox.config import clear_config, get_config
|
from netbox.config import clear_config, get_config
|
||||||
from netbox.context_managers import event_tracking
|
from netbox.context_managers import event_tracking
|
||||||
@ -32,12 +33,30 @@ class CoreMiddleware:
|
|||||||
# Assign a random unique ID to the request. This will be used for change logging.
|
# Assign a random unique ID to the request. This will be used for change logging.
|
||||||
request.id = uuid.uuid4()
|
request.id = uuid.uuid4()
|
||||||
|
|
||||||
|
# Check if the language should be activated before the language cookie has been set.
|
||||||
|
# This will happen for instance on the first request after login.
|
||||||
|
# The language will have been already activated by the framework if the cookie exists.
|
||||||
|
if (
|
||||||
|
request.user.is_authenticated and
|
||||||
|
settings.LANGUAGE_COOKIE_NAME not in request.COOKIES and
|
||||||
|
hasattr(request.user, 'config')
|
||||||
|
):
|
||||||
|
if language := request.user.config.get('locale.language'):
|
||||||
|
translation.activate(language)
|
||||||
|
|
||||||
# Enable the event_tracking context manager and process the request.
|
# Enable the event_tracking context manager and process the request.
|
||||||
with event_tracking(request):
|
with event_tracking(request):
|
||||||
response = self.get_response(request)
|
response = self.get_response(request)
|
||||||
|
|
||||||
# Check if language cookie should be renewed
|
# Check if language cookie should be renewed (persistent logins) or set when it does not exist, yet
|
||||||
if request.user.is_authenticated and settings.SESSION_SAVE_EVERY_REQUEST:
|
if (
|
||||||
|
request.user.is_authenticated and
|
||||||
|
hasattr(request.user, 'config') and
|
||||||
|
(
|
||||||
|
settings.SESSION_SAVE_EVERY_REQUEST or
|
||||||
|
settings.LANGUAGE_COOKIE_NAME not in request.COOKIES
|
||||||
|
)
|
||||||
|
):
|
||||||
if language := request.user.config.get('locale.language'):
|
if language := request.user.config.get('locale.language'):
|
||||||
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, language, max_age=request.session.get_expiry_age())
|
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, language, max_age=request.session.get_expiry_age())
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user