From 1b8feb8e1e04b07fbcaaaaf3baa6066360e9654b Mon Sep 17 00:00:00 2001 From: ycggyao Date: Tue, 14 Jan 2025 14:35:28 +0800 Subject: [PATCH] =?UTF-8?q?fix(backend):=20=E5=8D=95=E6=8D=AE=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E5=8E=BB=E9=99=A4self=5Fmanage=E9=80=BB=E8=BE=91=20#9?= =?UTF-8?q?051=20#=20Reviewed,=20transaction=20id:=2029184?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dbm-ui/backend/ticket/filters.py | 4 +- dbm-ui/backend/ticket/views.py | 64 ++++++++------------------------ 2 files changed, 18 insertions(+), 50 deletions(-) diff --git a/dbm-ui/backend/ticket/filters.py b/dbm-ui/backend/ticket/filters.py index 7a15945a23..6919d419ed 100644 --- a/dbm-ui/backend/ticket/filters.py +++ b/dbm-ui/backend/ticket/filters.py @@ -52,8 +52,10 @@ def filter_todo(self, queryset, name, value): Q(todo_of_ticket__operators__contains=user) | Q(todo_of_ticket__helpers__contains=user), todo_of_ticket__status__in=TODO_RUNNING_STATUS, ) - else: + elif value == "done": todo_filter = Q(todo_of_ticket__done_by=user) + else: + todo_filter = Q(creator=user) return queryset.filter(todo_filter).distinct() def filter_is_assist(self, queryset, name, value): diff --git a/dbm-ui/backend/ticket/views.py b/dbm-ui/backend/ticket/views.py index 59ed2d03df..220142f6b7 100644 --- a/dbm-ui/backend/ticket/views.py +++ b/dbm-ui/backend/ticket/views.py @@ -8,13 +8,10 @@ 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 -from django.db.models import Q from django.utils.translation import ugettext_lazy as _ from drf_yasg.utils import swagger_auto_schema from rest_framework import serializers, status @@ -22,12 +19,10 @@ from rest_framework.permissions import AllowAny from rest_framework.response import Response -from backend import env from backend.bk_web import viewsets 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 @@ -144,47 +139,24 @@ def _get_login_exempt_view_func(cls): # 需要豁免的接口方法与名字 return {"post": [cls.callback.__name__], "put": [], "get": [], "delete": []} - @classmethod - 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管理的单据合集,处理人及协助人也能管理自己的单据 - 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") + # @classmethod + # def _get_self_manage_tickets(cls, user): + # # 超级管理员返回所有单据 + # if user.username in env.ADMIN_USERS or user.is_superuser: + # return Ticket.objects.all() + # return Ticket.objects.all() + # # # 过滤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 + # # return Ticket.objects.filter(ticket_filter).distinct() def filter_queryset(self, queryset): """filter_class可能导致预取的todo失效,这里重新取一次""" queryset = super().filter_queryset(queryset) return queryset.prefetch_related("todo_of_ticket") - def get_queryset(self): - """ - 单据queryset规则--针对list: - 1. self_manage=0 只返回自己管理的单据 - 2. self_manage=1,则返回自己管理组件的单据,如果是管理员则返回所有单据 - """ - if self.action != "list" or "self_manage" not in self.request.query_params: - 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 - def get_serializer_context(self): context = super(TicketViewSet, self).get_serializer_context() if self.action == "retrieve": @@ -278,9 +250,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( @@ -444,12 +413,11 @@ def get_tickets_count(self, request, *args, **kwargs): """ 获取单据的数量,目前需要获取 - 我的申请 - - (代办)待我审批、待我确认,待我补货 + - (代办:待我处理、待我协助)待我审批、待我确认,待我补货 - 我的已办 - - 我负责的业务 """ user = request.user.username - tickets = self._get_self_manage_tickets(request.user) + tickets = self.get_queryset() exclude_values = {"MY_APPROVE", "SELF_MANAGE", "DONE"} # 初始化 count_map @@ -479,12 +447,10 @@ def calculate_status_count(field_name, relation_name): results["pending"] = calculate_status_count("operators", "todo_of_ticket") # 计算我的协助 results["to_help"] = calculate_status_count("helpers", "todo_of_ticket") - # 我负责的业务 - results[CountType.SELF_MANAGE] = tickets.count() # 我的申请 results[CountType.MY_APPROVE] = tickets.filter(creator=user).count() # 我的已办 - results[CountType.DONE] = tickets.filter(todo_of_ticket__done_by=user).count() + results[CountType.DONE] = tickets.filter(todo_of_ticket__done_by=user).distinct().count() return Response(results)