Skip to content

feat: add UserTimezoneMiddleware#255

Open
xogas wants to merge 3 commits intoTencentBlueKing:masterfrom
xogas:feat/add-middleware
Open

feat: add UserTimezoneMiddleware#255
xogas wants to merge 3 commits intoTencentBlueKing:masterfrom
xogas:feat/add-middleware

Conversation

@xogas
Copy link
Contributor

@xogas xogas commented Feb 2, 2026

No description provided.

@jamesgetx jamesgetx requested review from jamesgetx and piglei February 3, 2026 02:08
return

# Fallback to default timezone when time_zone is empty or invalid
dj_timezone.activate(dj_timezone.get_default_timezone())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这一行需要手动调用吗?Django 默认是不是已经激活这个

Copy link
Contributor Author

@xogas xogas Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

当打开 USE_TZ = True 时, 会使用 settings.TIME_ZONE 的时区, 但这里显式设置是为了明确这一点

],
ids=["invalid_timezone", "empty_string", "none_value", "no_attr"],
)
def test_fallback_to_default_timezone(self, rf, authenticated_user, time_zone_value, has_attr):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:以上测试均使用 mock 去验证以 activate 作为切入点,是否被 call,call 参数如何。但是考虑到测试已经构建起一个有效 Django App 的条件下,是否采用 override settings 去直接去除所有 mock,调用 middleware 然后获取当前 timezone 是否被改变是否更好?更符合“测试验证行为而不是实现”(假如实现复杂很多的情况下另说)。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改

@xogas xogas requested a review from piglei February 3, 2026 08:40
Copy link
Collaborator

@piglei piglei left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments