Skip to content

Commit

Permalink
feat(login/api): support iso_code for query and upsert
Browse files Browse the repository at this point in the history
  • Loading branch information
wklken committed Feb 10, 2023
1 parent 10d0c5b commit 02aafee
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/api/bkuser_core/api/login/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class LoginUpsertSerializer(serializers.Serializer):
staff_status = serializers.CharField(required=False)
wx_userid = serializers.CharField(required=False, allow_blank=True)

iso_code = serializers.CharField(required=False)


class LoginBatchQuerySerializer(serializers.Serializer):
username_list = serializers.ListField(child=serializers.CharField(), required=False)
Expand All @@ -56,6 +58,7 @@ class LoginBatchResponseSerializer(serializers.Serializer):
time_zone = serializers.CharField()
email = serializers.CharField()
role = serializers.IntegerField()
iso_code = serializers.CharField()

def get_username(self, data):
return get_username(
Expand Down Expand Up @@ -87,6 +90,7 @@ class Meta:
"language",
"domain",
"category_id",
"iso_code",
# NOTE: 这里缩减登陆成功之后的展示字段
# "position",
# "logo_url", => to logo?
Expand Down
24 changes: 23 additions & 1 deletion src/api/bkuser_core/api/login/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
from bkuser_core.common.error_codes import error_codes
from bkuser_core.profiles.constants import ProfileStatus
from bkuser_core.profiles.models import Profile, ProfileTokenHolder
from bkuser_core.profiles.utils import make_passwd_reset_url_by_token, parse_username_domain
from bkuser_core.profiles.utils import align_country_iso_code, make_passwd_reset_url_by_token, parse_username_domain
from bkuser_core.profiles.validators import validate_username
from bkuser_core.user_settings.loader import ConfigProvider

Expand Down Expand Up @@ -272,6 +272,10 @@ def upsert(self, request):
username = serializer.validated_data.pop("username")
domain = serializer.validated_data.pop("domain", None)

iso_code = None
if "iso_code" in validated_data:
iso_code = validated_data.pop("iso_code")

try:
category = ProfileCategory.objects.get(domain=domain)
# 当 domain 存在时,校验 username
Expand All @@ -296,6 +300,24 @@ def upsert(self, request):
if created:
logger.info("user<%s/%s> created by login", category.id, username)

if iso_code:
try:
# NOTE: 这里直接用iso_code设置country_code, 无视原来的country_code
# 原因: 目前产品只暴露iso_code, country_code是内部的
profile.country_code, profile.iso_code = align_country_iso_code(
country_code="",
iso_code=iso_code,
)
except ValueError:
profile.country_code = settings.DEFAULT_COUNTRY_CODE
profile.iso_code = settings.DEFAULT_IOS_CODE

try:
profile.save()
except Exception: # pylint: disable=broad-except
logger.exception("failed to update iso_code for profile %s", username)
# do nothing

return Response(data=ProfileSerializer(profile, context={"request": request}).data)

@method_decorator(cache_page(settings.GLOBAL_CACHES_TIMEOUT))
Expand Down

0 comments on commit 02aafee

Please sign in to comment.