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

feat(bkuser_core, bkuser_core/api/web): 新增回收站展示视图 #941

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 10 additions & 0 deletions src/api/bkuser_core/api/web/recycle_bin/__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.
"""
20 changes: 20 additions & 0 deletions src/api/bkuser_core/api/web/recycle_bin/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- 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 bkuser_core.categories.models import ProfileCategory
from bkuser_core.departments.models import Department
from bkuser_core.profiles.models import Profile
from bkuser_core.recycle_bin.constants import RecycleBinObjectType

RECYCLE_BIN_OBJECT_MAP = {
RecycleBinObjectType.CATEGORY.value: ProfileCategory,
RecycleBinObjectType.DEPARTMENT.value: Department,
RecycleBinObjectType.PROFILE.value: Profile,
}
79 changes: 79 additions & 0 deletions src/api/bkuser_core/api/web/recycle_bin/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# -*- 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 rest_framework import serializers

from bkuser_core.api.web.utils import get_category_display_name_map
from bkuser_core.recycle_bin.models import RecycleBin


class RecycleBinSearchInputSlZ(serializers.Serializer):
keyword = serializers.CharField(required=False)
neronkl marked this conversation as resolved.
Show resolved Hide resolved


class RecycleBinBaseSerializer(serializers.ModelSerializer):
expires = serializers.IntegerField()
neronkl marked this conversation as resolved.
Show resolved Hide resolved
neronkl marked this conversation as resolved.
Show resolved Hide resolved

class Meta:
model = RecycleBin
exclude = ["create_time", "update_time", "object_type", "object_id", "status"]
neronkl marked this conversation as resolved.
Show resolved Hide resolved


class RecycleBinCategoryOutputSlZ(RecycleBinBaseSerializer):
category = serializers.SerializerMethodField()
profile_count = serializers.IntegerField()
department_count = serializers.IntegerField()
neronkl marked this conversation as resolved.
Show resolved Hide resolved

def get_category(self, instance):
neronkl marked this conversation as resolved.
Show resolved Hide resolved
category = instance.get_map_object()
return {
"id": category.id,
"display_name": category.display_name,
"domain": category.domain,
"type": category.type,
}


class RecycleBinDepartmentOutputSlZ(RecycleBinBaseSerializer):
category_display_name = serializers.SerializerMethodField()
profile_count = serializers.IntegerField()
department = serializers.SerializerMethodField()

def get_department(self, instance):
neronkl marked this conversation as resolved.
Show resolved Hide resolved
department = instance.get_map_object()
return {
"id": department.id,
"name": department.name,
"ancestor": department.parent.name if department.parent else "",
"children": department.children.all().count(),
neronkl marked this conversation as resolved.
Show resolved Hide resolved
}

def get_category_display_name(self, instance):
department = instance.get_map_object()
return get_category_display_name_map()[department.category_id]


class RecycleBinProfileOutputSlZ(RecycleBinBaseSerializer):
category_display_name = serializers.SerializerMethodField()
profile = serializers.SerializerMethodField()

def get_profile(self, instance):
neronkl marked this conversation as resolved.
Show resolved Hide resolved
profile = instance.get_map_object()
return {
"id": profile.id,
"username": f"{profile.username}@{profile.domain}",
"display_name": profile.display_name,
"department": profile.departments.values("id", "name"),
}

def get_category_display_name(self, instance):
profile = instance.get_map_object()
return get_category_display_name_map()[profile.category_id]
neronkl marked this conversation as resolved.
Show resolved Hide resolved
33 changes: 33 additions & 0 deletions src/api/bkuser_core/api/web/recycle_bin/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# -*- 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.conf import path

from . import views

urlpatterns = [
path(
"category/",
views.RecycleBinCategoryListApi.as_view(),
name="recycle_bin.list_category",
neronkl marked this conversation as resolved.
Show resolved Hide resolved
),
path(
"department/",
views.RecycleBinDepartmentListApi.as_view(),
name="recycle_bin.list_department",
neronkl marked this conversation as resolved.
Show resolved Hide resolved
),
path(
"profile/",
views.RecycleBinProfileListApi.as_view(),
name="recycle_bin.list_profile",
neronkl marked this conversation as resolved.
Show resolved Hide resolved
),
neronkl marked this conversation as resolved.
Show resolved Hide resolved
]
74 changes: 74 additions & 0 deletions src/api/bkuser_core/api/web/recycle_bin/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# -*- 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.
"""
import functools
from operator import or_

from django.db.models import Q
from rest_framework import generics

from bkuser_core.api.web.recycle_bin.constants import RECYCLE_BIN_OBJECT_MAP
from bkuser_core.api.web.recycle_bin.serializers import (
RecycleBinCategoryOutputSlZ,
RecycleBinDepartmentOutputSlZ,
RecycleBinProfileOutputSlZ,
RecycleBinSearchInputSlZ,
)
from bkuser_core.api.web.viewset import CustomPagination
from bkuser_core.recycle_bin.constants import RecycleBinObjectStatus, RecycleBinObjectType
from bkuser_core.recycle_bin.models import RecycleBin


class RecycleBinBaseListApi(generics.ListAPIView):
neronkl marked this conversation as resolved.
Show resolved Hide resolved
"""
回收站展示基础类
"""

object_type: str = ""
search_fields: list = []
queryset = RecycleBin.objects.filter(status=RecycleBinObjectStatus.SOFT_DELETED.value)
pagination_class = CustomPagination

def _search_queryset(self, request):
neronkl marked this conversation as resolved.
Show resolved Hide resolved
self.queryset = self.queryset.filter(object_type=self.object_type)
input_slz = RecycleBinSearchInputSlZ(data=request.query_params)
input_slz.is_valid(raise_exception=True)
validated_data = input_slz.validated_data
neronkl marked this conversation as resolved.
Show resolved Hide resolved
if validated_data:
neronkl marked this conversation as resolved.
Show resolved Hide resolved
keyword = validated_data["keyword"]
# Q 连接对应search_filed 和 keyword
condition_combos = [Q(**{"{}__icontains".format(filed): keyword}) for filed in self.search_fields]
query = functools.reduce(or_, condition_combos)
object_ids = RECYCLE_BIN_OBJECT_MAP[self.object_type].objects.filter(query).values_list("id", flat=True)
self.queryset = self.queryset.filter(object_id__in=object_ids)

def list(self, request, *args, **kwargs):
input_slz = RecycleBinSearchInputSlZ(data=request.query_params)
input_slz.is_valid(raise_exception=True)
self._search_queryset(request)
return super(RecycleBinBaseListApi, self).list(request, *args, **kwargs)


class RecycleBinCategoryListApi(RecycleBinBaseListApi):
object_type = RecycleBinObjectType.CATEGORY.value
serializer_class = RecycleBinCategoryOutputSlZ
search_fields = ["domain", "display_name"]


class RecycleBinDepartmentListApi(RecycleBinBaseListApi):
object_type = RecycleBinObjectType.DEPARTMENT.value
serializer_class = RecycleBinDepartmentOutputSlZ
search_fields = ["name"]


class RecycleBinProfileListApi(RecycleBinBaseListApi):
object_type = RecycleBinObjectType.PROFILE.value
serializer_class = RecycleBinProfileOutputSlZ
search_fields = ["username", "display_name"]
1 change: 1 addition & 0 deletions src/api/bkuser_core/api/web/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
url(r"^site/", include("bkuser_core.api.web.site.urls")),
url(r"^departments/", include("bkuser_core.api.web.department.urls")),
url(r"^passwords/", include("bkuser_core.api.web.password.urls")),
url(r"^recycle_bin/", include("bkuser_core.api.web.recycle_bin.urls")),
# 通用检索
url(r"^search/", include("bkuser_core.api.web.search.urls")),
# 首页
Expand Down
1 change: 1 addition & 0 deletions src/api/bkuser_core/config/common/django_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"bkuser_core.audit",
"bkuser_core.categories",
"bkuser_core.bkiam",
"bkuser_core.recycle_bin",
# 数据库字段翻译,需要后置于需要翻译的 Django App
"modeltranslation",
# apigateway sdk
Expand Down
10 changes: 10 additions & 0 deletions src/api/bkuser_core/recycle_bin/__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.
"""
17 changes: 17 additions & 0 deletions src/api/bkuser_core/recycle_bin/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# -*- 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.apps import AppConfig


class ProfileConfig(AppConfig):
name = "bkuser_core.recycle_bin"
29 changes: 29 additions & 0 deletions src/api/bkuser_core/recycle_bin/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# -*- 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 enum import auto

from bkuser_core.common.enum import AutoNameEnum


class RecycleBinObjectType(AutoNameEnum):
CATEGORY = auto()
DEPARTMENT = auto()
PROFILE = auto()

_choices_labels = ((CATEGORY, "用户目录"), (DEPARTMENT, "部门组织"), (PROFILE, "人员"))


class RecycleBinObjectStatus(AutoNameEnum):
SOFT_DELETED = auto()
HARD_DELETED = auto()
REVERTING = auto()

_choices_labels = ((SOFT_DELETED, "软删除"), (HARD_DELETED, "硬删除"), (REVERTING, "还原"))
40 changes: 40 additions & 0 deletions src/api/bkuser_core/recycle_bin/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# -*- 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.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='RecycleBin',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('create_time', models.DateTimeField(auto_now_add=True)),
('update_time', models.DateTimeField(auto_now=True)),
('object_type', models.CharField(choices=[('CATEGORY', '用户目录'), ('DEPARTMENT', '部门组织'), ('PROFILE', '人员')], default='', max_length=64, verbose_name='对象类型')),
('object_id', models.IntegerField(verbose_name='对象id')),
('operator', models.CharField(default='', max_length=255, verbose_name='操作人')),
('status', models.CharField(choices=[('SOFT_DELETED', '软删除'), ('HARD_DELETED', '硬删除'), ('REVERTING', '还原')], default='SOFT_DELETED', max_length=64, verbose_name='数据状态')),
],
options={
'verbose_name': '回收站信息',
'verbose_name_plural': '回收站信息',
'ordering': ['-create_time'],
'index_together': {('object_type', 'object_id')},
},
),
]
10 changes: 10 additions & 0 deletions src/api/bkuser_core/recycle_bin/migrations/__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.
"""
Loading