Skip to content

Commit

Permalink
feat(web/personal_center): 个人中心展示 (#1245)
Browse files Browse the repository at this point in the history
  • Loading branch information
neronkl authored Oct 11, 2023
1 parent 0d0f935 commit d56fbd9
Show file tree
Hide file tree
Showing 21 changed files with 1,197 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@


class UserSearchInputSLZ(serializers.Serializer):
username = serializers.CharField(required=False, help_text="用户名", allow_blank=True)
username = serializers.CharField(help_text="用户名", required=False, allow_blank=True)


class DataSourceSearchDepartmentsOutputSLZ(serializers.Serializer):
Expand All @@ -39,7 +39,7 @@ class DataSourceSearchDepartmentsOutputSLZ(serializers.Serializer):
class UserSearchOutputSLZ(serializers.Serializer):
id = serializers.CharField(help_text="用户ID")
username = serializers.CharField(help_text="用户名")
full_name = serializers.CharField(help_text="全名")
full_name = serializers.CharField(help_text="姓名")
phone = serializers.CharField(help_text="手机号")
email = serializers.CharField(help_text="邮箱")
departments = serializers.SerializerMethodField(help_text="用户部门")
Expand Down Expand Up @@ -105,7 +105,7 @@ class LeaderSearchOutputSLZ(serializers.Serializer):


class DepartmentSearchInputSLZ(serializers.Serializer):
name = serializers.CharField(required=False, help_text="部门名称", allow_blank=True)
name = serializers.CharField(help_text="部门名称", required=False, allow_blank=True)


class DepartmentSearchOutputSLZ(serializers.Serializer):
Expand All @@ -125,7 +125,7 @@ class UserLeaderOutputSLZ(serializers.Serializer):

class UserRetrieveOutputSLZ(serializers.Serializer):
username = serializers.CharField(help_text="用户名")
full_name = serializers.CharField(help_text="全名")
full_name = serializers.CharField(help_text="姓名")
email = serializers.CharField(help_text="邮箱")
phone_country_code = serializers.CharField(help_text="手机区号")
phone = serializers.CharField(help_text="手机号")
Expand Down
3 changes: 3 additions & 0 deletions src/bk-user/bkuser/apis/web/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class CurrentUserTenantMixin:
request: Request

def get_current_tenant_id(self) -> str:
"""
获取当前登录用户所属租户的ID
"""
tenant_id = self.request.user.get_property("tenant_id")
if not tenant_id:
raise error_codes.GET_CURRENT_TENANT_FAILED
Expand Down
5 changes: 4 additions & 1 deletion src/bk-user/bkuser/apis/web/organization/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,12 @@ class TenantUserInfoOutputSLZ(serializers.Serializer):
phone_country_code = serializers.CharField(
help_text="手机号国际区号", required=False, default=settings.DEFAULT_PHONE_COUNTRY_CODE
)
account_expired_at = serializers.DateTimeField(help_text="账号过期时间")
account_expired_at = serializers.SerializerMethodField(help_text="账号过期时间")
departments = serializers.SerializerMethodField(help_text="用户所属部门")

def get_account_expired_at(self, instance: TenantUser) -> str:
return instance.account_expired_at_display


class TenantUserListOutputSLZ(TenantUserInfoOutputSLZ):
@swagger_serializer_method(serializer_or_field=TenantUserDepartmentOutputSLZ(many=True))
Expand Down
10 changes: 10 additions & 0 deletions src/bk-user/bkuser/apis/web/personal_center/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-用户管理(Bk-User) available.
Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
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.
"""
135 changes: 135 additions & 0 deletions src/bk-user/bkuser/apis/web/personal_center/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-用户管理(Bk-User) available.
Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
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, List

from django.conf import settings
from django.utils.translation import gettext_lazy as _
from drf_yasg.utils import swagger_serializer_method
from rest_framework import serializers

from bkuser.apis.web.organization.serializers import TenantUserDepartmentOutputSLZ, TenantUserLeaderOutputSLZ
from bkuser.apps.tenant.models import TenantUser
from bkuser.biz.tenant import TenantUserHandler
from bkuser.common.error_codes import error_codes
from bkuser.common.validators import validate_phone_with_country_code


class TenantBaseInfoOutputSLZ(serializers.Serializer):
id = serializers.CharField(help_text="租户ID")
name = serializers.CharField(help_text="租户名称")


class TenantUserBaseInfoOutputSLZ(serializers.Serializer):
id = serializers.CharField(help_text="租户用户ID")
username = serializers.CharField(help_text="用户名")
full_name = serializers.CharField(help_text="姓名")
tenant = TenantBaseInfoOutputSLZ(help_text="租户")


class NaturalUserWithTenantUserListOutputSLZ(serializers.Serializer):
id = serializers.CharField(help_text="自然人ID")
full_name = serializers.CharField(help_text="自然人姓名")
tenant_users = serializers.ListField(help_text="自然人关联的租户账号列表", child=TenantUserBaseInfoOutputSLZ())


class TenantUserRetrieveOutputSLZ(serializers.Serializer):
id = serializers.CharField(help_text="租户用户ID")
username = serializers.CharField(help_text="用户名", required=False)
full_name = serializers.CharField(help_text="用户姓名", required=False)
logo = serializers.CharField(help_text="头像", required=False)

# 邮箱信息
is_inherited_email = serializers.BooleanField(help_text="是否继承数据源邮箱")
email = serializers.EmailField(help_text="用户邮箱", required=False)
custom_email = serializers.EmailField(help_text="自定义用户邮箱")

# 手机号信息
is_inherited_phone = serializers.BooleanField(help_text="是否继承数据源手机号")
phone = serializers.CharField(help_text="用户手机号", required=False)
phone_country_code = serializers.CharField(
help_text="手机号国际区号", required=False, default=settings.DEFAULT_PHONE_COUNTRY_CODE
)
custom_phone = serializers.CharField(help_text="自定义用户手机号")
custom_phone_country_code = serializers.CharField(help_text="自定义用户手机国际区号")

account_expired_at = serializers.SerializerMethodField(help_text="账号过期时间")
departments = serializers.SerializerMethodField(help_text="用户所属部门")
leaders = serializers.SerializerMethodField(help_text="用户上级")

@swagger_serializer_method(serializer_or_field=TenantUserDepartmentOutputSLZ(many=True))
def get_departments(self, instance: TenantUser) -> List[Dict]:
tenant_user_departments = TenantUserHandler.get_tenant_user_departments_map_by_id([instance.id])
departments = tenant_user_departments.get(instance.id) or []
return [{"id": i.id, "name": i.name} for i in departments]

@swagger_serializer_method(serializer_or_field=TenantUserLeaderOutputSLZ(many=True))
def get_leaders(self, instance: TenantUser) -> List[Dict]:
leaders = TenantUserHandler.get_tenant_user_leaders_map_by_id([instance.id]).get(instance.id) or []
return [
{
"id": i.id,
"username": i.username,
"full_name": i.full_name,
}
for i in leaders
]

def get_account_expired_at(self, instance: TenantUser) -> str:
return instance.account_expired_at_display

def to_representation(self, instance: TenantUser) -> Dict:
data = super().to_representation(instance)
user = instance.data_source_user
if user is not None:
data.update(
{
"full_name": user.full_name,
"username": user.username,
"email": user.email,
"phone": user.phone,
"phone_country_code": user.phone_country_code,
"logo": user.logo or settings.DEFAULT_DATA_SOURCE_USER_LOGO,
}
)
return data


class TenantUserPhoneUpdateInputSLZ(serializers.Serializer):
is_inherited_phone = serializers.BooleanField(help_text="是否继承数据源手机号", required=True)
custom_phone = serializers.CharField(help_text="自定义用户手机号", required=False, allow_blank=True)
custom_phone_country_code = serializers.CharField(
help_text="自定义用户手机国际区号", required=False, default=settings.DEFAULT_PHONE_COUNTRY_CODE
)

def validate(self, attrs):
# custom_phone_country_code 具有默认值
# 不通过继承,则需校验手机号,custom_phone 必须存在
if not attrs["is_inherited_phone"]:
if not attrs.get("custom_phone"):
raise error_codes.VALIDATION_ERROR.f(_("自定义手机号码为必填项"))

validate_phone_with_country_code(
phone=attrs["custom_phone"], country_code=attrs["custom_phone_country_code"]
)

return attrs


class TenantUserEmailUpdateInputSLZ(serializers.Serializer):
is_inherited_email = serializers.BooleanField(help_text="是否继承数据源邮箱", required=True)
custom_email = serializers.EmailField(help_text="自定义用户邮箱", required=False, allow_blank=True)

def validate(self, attrs):
# 不通过继承,custom_email 必须存在
if not attrs["is_inherited_email"] and not attrs.get("custom_email"):
raise error_codes.VALIDATION_ERROR.f(_("自定义邮箱为必填项"))

return attrs
36 changes: 36 additions & 0 deletions src/bk-user/bkuser/apis/web/personal_center/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-用户管理(Bk-User) available.
Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
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 django.urls import path

from . import views

urlpatterns = [
# 关联用户列表
path(
"current-natural-user/",
views.NaturalUserTenantUserListApi.as_view(),
name="personal_center.current_natural_user",
),
# 租户用户详情
path(
"tenant-users/<str:id>/", views.TenantUserRetrieveApi.as_view(), name="personal_center.tenant_users.retrieve"
),
path(
"tenant-users/<str:id>/phone/",
views.TenantUserPhoneUpdateApi.as_view(),
name="personal_center.tenant_users.phone.update",
),
path(
"tenant-users/<str:id>/email/",
views.TenantUserEmailUpdateApi.as_view(),
name="personal_center.tenant_users.email.update",
),
]
Loading

0 comments on commit d56fbd9

Please sign in to comment.