From 8b6526211c5890a7d383aec2148b5c3688b6a84b Mon Sep 17 00:00:00 2001 From: fit2bot <68588906+fit2bot@users.noreply.github.com> Date: Mon, 16 Oct 2023 16:40:21 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E5=B7=A5=E5=8D=95=E5=8A=A8=E4=BD=9C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=93=8D=E4=BD=9C=E6=97=A5=E5=BF=97=20(#1185?= =?UTF-8?q?7)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: feng <1304903146@qq.com> --- apps/audits/const.py | 6 ++++-- apps/tickets/api/ticket.py | 22 +++++++++++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/apps/audits/const.py b/apps/audits/const.py index b08595a2f..4418e9e7f 100644 --- a/apps/audits/const.py +++ b/apps/audits/const.py @@ -29,11 +29,13 @@ class ActionChoices(TextChoices): connect = "connect", _("Connect") login = "login", _("Login") change_auth = "change_password", _("Change password") - # acls action - reject = 'reject', _('Reject') + accept = 'accept', _('Accept') review = 'review', _('Review') notice = 'notice', _('Notifications') + reject = 'reject', _('Reject') + approve = 'approve', _('Approve') + close = 'close', _('Close') class LoginTypeChoices(TextChoices): diff --git a/apps/tickets/api/ticket.py b/apps/tickets/api/ticket.py index 1e44c3818..da60aefe8 100644 --- a/apps/tickets/api/ticket.py +++ b/apps/tickets/api/ticket.py @@ -6,9 +6,10 @@ from rest_framework.decorators import action from rest_framework.exceptions import MethodNotAllowed from rest_framework.response import Response +from audits.handler import create_or_update_operate_log from common.api import CommonApiMixin from common.const.http import POST, PUT, PATCH -from orgs.utils import tmp_to_root_org +from orgs.utils import tmp_to_root_org, tmp_to_org from rbac.permissions import RBACPermission from tickets import filters from tickets import serializers @@ -17,6 +18,7 @@ from tickets.models import ( ApplyLoginAssetTicket, ApplyCommandTicket ) from tickets.permissions.ticket import IsAssignee, IsApplicant +from ..const import TicketAction __all__ = [ 'TicketViewSet', 'ApplyAssetTicketViewSet', @@ -77,6 +79,21 @@ class TicketViewSet(CommonApiMixin, viewsets.ModelViewSet): with tmp_to_root_org(): return super().create(request, *args, **kwargs) + @staticmethod + def _record_operate_log(ticket, action): + with tmp_to_org(ticket.org_id): + after = { + 'ID': str(ticket.id), + str(_('Name')): ticket.title, + str(_('Applicant')): str(ticket.applicant), + } + object_name = ticket._meta.object_name + resource_type = ticket._meta.verbose_name + create_or_update_operate_log( + action, resource_type, resource=ticket, + after=after, object_name=object_name + ) + @action(detail=True, methods=[PUT, PATCH], permission_classes=[IsAssignee, ]) def approve(self, request, *args, **kwargs): self.ticket_not_allowed() @@ -88,18 +105,21 @@ class TicketViewSet(CommonApiMixin, viewsets.ModelViewSet): serializer.is_valid(raise_exception=True) instance = serializer.save() instance.approve(processor=request.user) + self._record_operate_log(instance, TicketAction.approve) return Response('ok') @action(detail=True, methods=[PUT], permission_classes=[IsAssignee, ]) def reject(self, request, *args, **kwargs): instance = self.get_object() instance.reject(processor=request.user) + self._record_operate_log(instance, TicketAction.reject) return Response('ok') @action(detail=True, methods=[PUT], permission_classes=[IsAssignee | IsApplicant, ]) def close(self, request, *args, **kwargs): instance = self.get_object() instance.close() + self._record_operate_log(instance, TicketAction.close) return Response('ok') @action(detail=False, methods=[PUT], permission_classes=[RBACPermission, ])