Skip to content

Commit

Permalink
16995 Move language handling to Middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
m2martin authored and jeremystretch committed Sep 23, 2024
1 parent 6b219a2 commit 7090cbb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
4 changes: 0 additions & 4 deletions netbox/account/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,6 @@ def post(self, request):

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

else:
Expand Down
23 changes: 21 additions & 2 deletions netbox/netbox/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.db import connection, ProgrammingError
from django.db.utils import InternalError
from django.http import Http404, HttpResponseRedirect
from django.utils import translation

from netbox.config import clear_config, get_config
from netbox.context_managers import event_tracking
Expand All @@ -32,12 +33,30 @@ def __call__(self, request):
# Assign a random unique ID to the request. This will be used for change logging.
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.
with event_tracking(request):
response = self.get_response(request)

# Check if language cookie should be renewed
if request.user.is_authenticated and settings.SESSION_SAVE_EVERY_REQUEST:
# Check if language cookie should be renewed (persistent logins) or set when it does not exist, yet
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'):
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, language, max_age=request.session.get_expiry_age())

Expand Down

0 comments on commit 7090cbb

Please sign in to comment.