File tree Expand file tree Collapse file tree 3 files changed +38
-0
lines changed Expand file tree Collapse file tree 3 files changed +38
-0
lines changed Original file line number Diff line number Diff line change
1
+ #
2
+ # Copyright (c) nexB Inc. and others. All rights reserved.
3
+ # FederatedCode is a trademark of nexB Inc.
4
+ # SPDX-License-Identifier: Apache-2.0
5
+ # See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
6
+ # See https://github.com/nexB/federatedcode for support or download.
7
+ # See https://aboutcode.org for more information about AboutCode.org OSS projects.
8
+ #
9
+
10
+ import zoneinfo
11
+
12
+ from django .utils import timezone
13
+
14
+
15
+ class TimezoneMiddleware :
16
+ def __init__ (self , get_response ):
17
+ self .get_response = get_response
18
+
19
+ def __call__ (self , request ):
20
+ try :
21
+ # Activate local timezone for user using cookies
22
+ tzname = request .COOKIES .get ("user_timezone" )
23
+ if tzname :
24
+ timezone .activate (zoneinfo .ZoneInfo (tzname ))
25
+ else :
26
+ timezone .deactivate ()
27
+ except Exception as e :
28
+ timezone .deactivate ()
29
+
30
+ return self .get_response (request )
Original file line number Diff line number Diff line change 28
28
</ div >
29
29
30
30
< script src ="{% static 'js/notification.js' %} " defer > </ script >
31
+ < script >
32
+ // Since django can not determine the client’s time zone automatically.
33
+ // Store client's timezone in cookies to enable localization.
34
+ // https://docs.djangoproject.com/en/5.1/topics/i18n/timezones/#selecting-the-current-time-zone
35
+ const currentTimeZone = Intl . DateTimeFormat ( ) . resolvedOptions ( ) . timeZone ;
36
+ document . cookie = "user_timezone=" + currentTimeZone ;
37
+ </ script >
31
38
{% block scripts %}
32
39
{% endblock %}
33
40
</ body >
Original file line number Diff line number Diff line change 90
90
"django.middleware.clickjacking.XFrameOptionsMiddleware" ,
91
91
# OAUTH
92
92
"oauth2_provider.middleware.OAuth2TokenMiddleware" ,
93
+ "fedcode.middleware.TimezoneMiddleware" ,
93
94
]
94
95
95
96
ROOT_URLCONF = "federatedcode.urls"
You can’t perform that action at this time.
0 commit comments