From 02aafeeba0e96fb13b48d12c069cb2c8645e1b5d Mon Sep 17 00:00:00 2001 From: wklken Date: Fri, 10 Feb 2023 14:45:25 +0800 Subject: [PATCH] feat(login/api): support iso_code for query and upsert #887 --- src/api/bkuser_core/api/login/serializers.py | 4 ++++ src/api/bkuser_core/api/login/views.py | 24 +++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/api/bkuser_core/api/login/serializers.py b/src/api/bkuser_core/api/login/serializers.py index ace11971f..ea2bd75bf 100644 --- a/src/api/bkuser_core/api/login/serializers.py +++ b/src/api/bkuser_core/api/login/serializers.py @@ -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) @@ -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( @@ -87,6 +90,7 @@ class Meta: "language", "domain", "category_id", + "iso_code", # NOTE: 这里缩减登陆成功之后的展示字段 # "position", # "logo_url", => to logo? diff --git a/src/api/bkuser_core/api/login/views.py b/src/api/bkuser_core/api/login/views.py index 98a64288d..68cb071aa 100644 --- a/src/api/bkuser_core/api/login/views.py +++ b/src/api/bkuser_core/api/login/views.py @@ -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 @@ -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 @@ -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))