Skip to content

Commit

Permalink
Align language cookie with session lifetime
Browse files Browse the repository at this point in the history
  • Loading branch information
m2martin committed Jun 30, 2024
1 parent 4f93782 commit 8577ff1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
4 changes: 2 additions & 2 deletions netbox/account/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def post(self, request):

# Set the user's preferred language (if any)
if language := request.user.config.get('locale.language'):
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, language)
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, language, max_age=request.session.get_expiry_age())

return response

Expand Down Expand Up @@ -206,7 +206,7 @@ def post(self, request):

# Set/clear language cookie
if language := form.cleaned_data['locale.language']:
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, language)
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, language, max_age=request.session.get_expiry_age())
else:
response.delete_cookie(settings.LANGUAGE_COOKIE_NAME)

Expand Down
12 changes: 5 additions & 7 deletions netbox/netbox/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,15 @@ def __call__(self, request):
login_url = f'{settings.LOGIN_URL}?next={parse.quote(request.get_full_path_info())}'
return HttpResponseRedirect(login_url)

# If language cookie is not set, check if it should be set and redirect to requested page
if request.user.is_authenticated and not request.COOKIES.get(settings.LANGUAGE_COOKIE_NAME):
if language := request.user.config.get('locale.language'):
response = HttpResponseRedirect(request.path)
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, language)
return response

# 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:
if language := request.user.config.get('locale.language'):
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, language, max_age=request.session.get_expiry_age())

# Attach the unique request ID as an HTTP header.
response['X-Request-ID'] = request.id

Expand Down

0 comments on commit 8577ff1

Please sign in to comment.