Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api/profile): leader has no domain in username #535

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/api/bkuser_core/profiles/v2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ def list(self, request, *args, **kwargs):
# 直接在 DB 中拼接 username & domain,比在 serializer 中快很多
if "username" in fields:
default_domain = ProfileCategory.objects.get_default().domain
# 这里拼装的 username@domain, 没有走到serializer中的get_username
queryset = queryset.extra(
select={"username": "if(`domain`= %s, username, CONCAT(username, '@', domain))"},
select_params=(default_domain,),
Expand All @@ -206,7 +207,9 @@ def list(self, request, *args, **kwargs):
page = self.paginate_queryset(queryset)
# page may be empty list
if page is not None:
serializer = serializer_class(page, fields=fields, many=True)
# BUG: 这里必须显式传递 context给到slz, 下层self.context.get("request") 用到, 判断拼接 username@domain
# 坑, 修改或重构需要注意; 不要通过这种方式来决定字段格式, 非常容易遗漏
serializer = serializer_class(page, fields=fields, many=True, context=self.get_serializer_context())
return self.get_paginated_response(serializer.data)

fields = [x for x in fields if x in self._get_model_field_names()]
Expand Down