generated from aboutcode-org/skeleton
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable datetime localization for client
- Detect and store client timezone in cookies - Add TimezoneMiddleware to activate localization based on cookies Signed-off-by: Keshav Priyadarshi <[email protected]>
- Loading branch information
1 parent
b867180
commit 1e32797
Showing
3 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# | ||
# Copyright (c) nexB Inc. and others. All rights reserved. | ||
# FederatedCode is a trademark of nexB Inc. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text. | ||
# See https://github.com/nexB/federatedcode for support or download. | ||
# See https://aboutcode.org for more information about AboutCode.org OSS projects. | ||
# | ||
|
||
import zoneinfo | ||
|
||
from django.utils import timezone | ||
|
||
|
||
class TimezoneMiddleware: | ||
def __init__(self, get_response): | ||
self.get_response = get_response | ||
|
||
def __call__(self, request): | ||
try: | ||
# Activate local timezone for user using cookies | ||
tzname = request.COOKIES.get("user_timezone") | ||
if tzname: | ||
timezone.activate(zoneinfo.ZoneInfo(tzname)) | ||
else: | ||
timezone.deactivate() | ||
except Exception as e: | ||
timezone.deactivate() | ||
|
||
return self.get_response(request) |
This file contains 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
This file contains 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