diff --git a/dbm-ui/backend/ticket/views.py b/dbm-ui/backend/ticket/views.py index 59ed2d03df..0dfe59f095 100644 --- a/dbm-ui/backend/ticket/views.py +++ b/dbm-ui/backend/ticket/views.py @@ -8,9 +8,7 @@ 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 operator from collections import Counter -from functools import reduce from typing import Dict, List from django.db import transaction @@ -27,7 +25,6 @@ from backend.bk_web.pagination import AuditedLimitOffsetPagination from backend.bk_web.swagger import PaginatedResponseSwaggerAutoSchema, common_swagger_auto_schema from backend.configuration.constants import DBType -from backend.configuration.models import DBAdministrator from backend.db_services.ipchooser.query.resource import ResourceQueryHelper from backend.iam_app.dataclass import ResourceEnum from backend.iam_app.dataclass.actions import ActionEnum @@ -149,17 +146,12 @@ def _get_self_manage_tickets(cls, user): # 超级管理员返回所有单据 if user.username in env.ADMIN_USERS or user.is_superuser: return Ticket.objects.all() - # 获取user管理的单据合集 - manage_filters = [ - Q(group=manage.db_type) & Q(bk_biz_id=manage.bk_biz_id) if manage.bk_biz_id else Q(group=manage.db_type) - for manage in DBAdministrator.objects.filter(users__contains=user.username) - ] - # 除了user管理的单据合集,处理人及协助人也能管理自己的单据 + # 过滤user为处理人、协助人的单据 todo_filters = Q( Q(todo_of_ticket__operators__contains=user.username) | Q(todo_of_ticket__helpers__contains=user.username) ) - ticket_filter = Q(creator=user.username) | todo_filters | reduce(operator.or_, manage_filters or [Q()]) - return Ticket.objects.filter(ticket_filter).prefetch_related("todo_of_ticket") + ticket_filter = Q(creator=user.username) | todo_filters + return Ticket.objects.filter(ticket_filter).distinct() def filter_queryset(self, queryset): """filter_class可能导致预取的todo失效,这里重新取一次""" @@ -168,22 +160,11 @@ def filter_queryset(self, queryset): def get_queryset(self): """ - 单据queryset规则--针对list: - 1. self_manage=0 只返回自己管理的单据 - 2. self_manage=1,则返回自己管理组件的单据,如果是管理员则返回所有单据 + 获取ticket的queryset """ - if self.action != "list" or "self_manage" not in self.request.query_params: + if self.action != "list": return super().get_queryset() - - username = self.request.user.username - self_manage = int(self.request.query_params["self_manage"]) - # 只返回自己创建的单据 - if self_manage == 0: - qs = Ticket.objects.filter(creator=username) - # 返回自己管理的组件单据 - else: - qs = self._get_self_manage_tickets(self.request.user) - return qs + return self._get_self_manage_tickets(self.request.user) def get_serializer_context(self): context = super(TicketViewSet, self).get_serializer_context() @@ -278,9 +259,6 @@ def list(self, request, *args, **kwargs): resp = super().list(request, *args, **kwargs) # 补充单据关联信息 resp.data["results"] = TicketHandler.add_related_object(resp.data["results"]) - # 如果是查询自身单据(self_manage),则不进行鉴权 - skip_iam = "self_manage" in request.query_params - resp.data["results"] = [{"skip_iam": skip_iam, **t} for t in resp.data["results"]] return resp @common_swagger_auto_schema(