Skip to content

Commit

Permalink
Fixes #16758: Create language cookie if required (#16764)
Browse files Browse the repository at this point in the history
* Fixes #16758: Create language cookie if required

* Align language cookie with session lifetime
  • Loading branch information
m2martin authored Jul 9, 2024
1 parent 96ff796 commit 6a1245c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 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
5 changes: 5 additions & 0 deletions netbox/netbox/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ def __call__(self, 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 6a1245c

Please sign in to comment.