Skip to content

Commit

Permalink
feat(locale): add time zone adaptation according to tenant user setti…
Browse files Browse the repository at this point in the history
  • Loading branch information
Qi-Cui authored Aug 9, 2024
1 parent 2f4290b commit 38ce5a9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/bk-user/bkuser/apis/web/personal_center/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""

from typing import Dict

from django.utils.translation import gettext_lazy as _
Expand Down
18 changes: 18 additions & 0 deletions src/bk-user/bkuser/common/middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""

import pytz
from django.utils import timezone

from .local import local


Expand All @@ -29,3 +33,17 @@ def __call__(self, request):
local.release()

return response


class TimeZoneMiddleware:
def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request):
# 如果时区非空,则激活当前时区。如果时区为空,则使用默认时区
if request.user and request.user.is_authenticated and (tzname := request.user.get_property("time_zone")):
timezone.activate(pytz.timezone(tzname))
else:
timezone.deactivate()

return self.get_response(request)
1 change: 1 addition & 0 deletions src/bk-user/bkuser/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware",
"bkuser.auth.middlewares.LoginMiddleware",
"bkuser.common.middlewares.TimeZoneMiddleware",
"django_prometheus.middleware.PrometheusAfterMiddleware",
]

Expand Down

0 comments on commit 38ce5a9

Please sign in to comment.