forked from TencentBlueKing/bk-user
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bkuser_core, bkuser_core/api/web): 新增回收站展示视图
初始化全局配置(回收策略),新增回收站展示策略 feat TencentBlueKing#908
- Loading branch information
Showing
17 changed files
with
504 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
|
||
class RecycleBinBaseSerializer(serializers.ModelSerializer): | ||
expires = serializers.IntegerField() | ||
|
||
class Meta: | ||
model = RecycleBin | ||
exclude = ["create_time", "update_time", "object_type", "object_id", "status"] | ||
|
||
|
||
class RecycleBinCategoryOutputSlZ(RecycleBinBaseSerializer): | ||
category = serializers.SerializerMethodField() | ||
profile_count = serializers.IntegerField() | ||
department_count = serializers.IntegerField() | ||
|
||
def get_category(self, instance): | ||
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): | ||
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(), | ||
} | ||
|
||
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): | ||
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
), | ||
path( | ||
"department/", | ||
views.RecycleBinDepartmentListApi.as_view(), | ||
name="recycle_bin.list_department", | ||
), | ||
path( | ||
"profile/", | ||
views.RecycleBinProfileListApi.as_view(), | ||
name="recycle_bin.list_profile", | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): | ||
""" | ||
回收站展示基础类 | ||
""" | ||
|
||
object_type: str = "" | ||
search_fields: list = [] | ||
queryset = RecycleBin.objects.filter(status=RecycleBinObjectStatus.SOFT_DELETED.value) | ||
pagination_class = CustomPagination | ||
|
||
def _search_queryset(self, request): | ||
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 | ||
if validated_data: | ||
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
40
src/api/bkuser_core/recycle_bin/migrations/0001_initial.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')}, | ||
}, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
""" |
Oops, something went wrong.