Reset active language and timezone on requests without an org - #260
Open
norkans7 wants to merge 3 commits into
Open
Reset active language and timezone on requests without an org#260norkans7 wants to merge 3 commits into
norkans7 wants to merge 3 commits into
Conversation
Previously SetOrgMiddleware activated an org's language and timezone in thread-local state but never reset them, so org-less requests handled by the same thread could render with a previous request's locale settings.
Instead of activating state in process_request and resetting to defaults on org-less requests, wrap request handling in translation.override and timezone.override when an org supplies a language/timezone. Prior state is snapshotted and restored, so nothing leaks between requests on the same thread and org-less requests keep whatever language was negotiated earlier in the middleware chain (e.g. by LocaleMiddleware).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
SetOrgMiddleware activated an org's language and timezone in thread-local state but never restored them, so requests without an org handled by the same worker thread could render with a previous request's locale settings.
This reworks the middleware to save and restore instead of activate and reset:
__call__now wraps request handling intranslation.override(...)andtimezone.override(...)context managers when the org supplies a language/timezone, falling back tosettings.DEFAULT_LANGUAGEfor orgs without a configured language. These snapshot the prior thread-local state and restore it after the response is generated, so no state leaks between requests — including the response phase.LocaleMiddleware) rather than being forced tosettings.LANGUAGE_CODE. This matters because org-less pages (login, org chooser, account management) are exactly the ones where a user's negotiated language would otherwise be stomped.set_language/set_timezonehook methods that mutated global state are removed.Tests cover: org state being restored after the response (no leak into subsequent org-less requests on the same thread), a pre-negotiated language surviving an org-less request unchanged, and the default language fallback for orgs without a configured language.