From 382112ee3354f701166273704539b8d0964be3af Mon Sep 17 00:00:00 2001 From: fit2bot <68588906+fit2bot@users.noreply.github.com> Date: Thu, 18 Aug 2022 11:48:58 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E6=89=B9=E9=87=8F=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E4=BC=98=E5=8C=96=20(#8772)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: feng626 <1304903146@qq.com> --- apps/audits/api.py | 8 ++------ apps/audits/filters.py | 27 +++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/apps/audits/api.py b/apps/audits/api.py index ca0e4d86a..ff1bb0309 100644 --- a/apps/audits/api.py +++ b/apps/audits/api.py @@ -12,6 +12,7 @@ from common.api import CommonGenericViewSet from orgs.mixins.api import OrgGenericViewSet, OrgBulkModelViewSet, OrgRelationMixin from orgs.utils import current_org from ops.models import CommandExecution +from . import filters from .models import FTPLog, UserLoginLog, OperateLog, PasswordChangeLog from .serializers import FTPLogSerializer, UserLoginLogSerializer, CommandExecutionSerializer from .serializers import OperateLogSerializer, PasswordChangeLogSerializer, CommandExecutionHostsRelationSerializer @@ -126,12 +127,7 @@ class CommandExecutionViewSet(ListModelMixin, OrgGenericViewSet): class CommandExecutionHostRelationViewSet(OrgRelationMixin, OrgBulkModelViewSet): serializer_class = CommandExecutionHostsRelationSerializer m2m_field = CommandExecution.hosts.field - filterset_fields = { - 'id': ['exact'], - 'asset': ['exact'], - 'asset__hostname': ['icontains'], - 'commandexecution': ['exact'], - } + filterset_class = filters.CommandExecutionFilter search_fields = ('asset__hostname', ) http_method_names = ['options', 'get'] rbac_perms = { diff --git a/apps/audits/filters.py b/apps/audits/filters.py index 470c2c4b5..b8bf466ec 100644 --- a/apps/audits/filters.py +++ b/apps/audits/filters.py @@ -1,10 +1,14 @@ +from django.db.models import F, Value +from django.db.models.functions import Concat +from django_filters.rest_framework import CharFilter from rest_framework import filters from rest_framework.compat import coreapi, coreschema from orgs.utils import current_org +from ops.models import CommandExecution +from common.drf.filters import BaseFilterSet - -__all__ = ['CurrentOrgMembersFilter'] +__all__ = ['CurrentOrgMembersFilter', 'CommandExecutionFilter'] class CurrentOrgMembersFilter(filters.BaseFilterBackend): @@ -30,3 +34,22 @@ class CurrentOrgMembersFilter(filters.BaseFilterBackend): else: queryset = queryset.filter(user__in=self._get_user_list()) return queryset + + +class CommandExecutionFilter(BaseFilterSet): + hostname_ip = CharFilter(method='filter_hostname_ip') + + class Meta: + model = CommandExecution.hosts.through + fields = ( + 'id', 'asset', 'commandexecution', 'hostname_ip' + ) + + def filter_hostname_ip(self, queryset, name, value): + queryset = queryset.annotate( + hostname_ip=Concat( + F('asset__hostname'), Value('('), + F('asset__ip'), Value(')') + ) + ).filter(hostname_ip__icontains=value) + return queryset