From f0cbd773101d962830477c9e0b96ca435ba2bfaf Mon Sep 17 00:00:00 2001 From: wangruidong <940853815@qq.com> Date: Tue, 26 Mar 2024 10:21:44 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E8=B5=84=E4=BA=A7?= =?UTF-8?q?=E3=80=81=E7=BD=91=E5=9F=9F=E3=80=81=E7=BD=91=E5=85=B3=E7=9A=84?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=E4=BD=93=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/assets/api/asset/asset.py | 1 + apps/assets/api/domain.py | 4 ++++ apps/assets/serializers/domain.py | 22 ++++++++++++++-------- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/apps/assets/api/asset/asset.py b/apps/assets/api/asset/asset.py index 0f66d4b9a..1e5d2a0b6 100644 --- a/apps/assets/api/asset/asset.py +++ b/apps/assets/api/asset/asset.py @@ -32,6 +32,7 @@ __all__ = [ class AssetFilterSet(BaseFilterSet): platform = django_filters.CharFilter(method='filter_platform') + exclude_platform = django_filters.CharFilter(field_name="platform__name", lookup_expr='exact', exclude=True) domain = django_filters.CharFilter(method='filter_domain') type = django_filters.CharFilter(field_name="platform__type", lookup_expr="exact") category = django_filters.CharFilter(field_name="platform__category", lookup_expr="exact") diff --git a/apps/assets/api/domain.py b/apps/assets/api/domain.py index 46b586458..14979036a 100644 --- a/apps/assets/api/domain.py +++ b/apps/assets/api/domain.py @@ -30,6 +30,10 @@ class DomainViewSet(OrgBulkModelViewSet): return serializers.DomainWithGatewaySerializer return super().get_serializer_class() + def partial_update(self, request, *args, **kwargs): + kwargs['partial'] = True + return self.update(request, *args, **kwargs) + class GatewayViewSet(HostViewSet): perm_model = Gateway diff --git a/apps/assets/serializers/domain.py b/apps/assets/serializers/domain.py index 9138e23f9..4224e3983 100644 --- a/apps/assets/serializers/domain.py +++ b/apps/assets/serializers/domain.py @@ -1,12 +1,13 @@ # -*- coding: utf-8 -*- # -from django.db.models import Count +from django.db.models import Count, Q from django.utils.translation import gettext_lazy as _ from rest_framework import serializers from common.serializers import ResourceLabelsMixin from common.serializers.fields import ObjectRelatedField from orgs.mixins.serializers import BulkOrgResourceModelSerializer +from assets.models.gateway import Gateway from .gateway import GatewayWithAccountSecretSerializer from ..models import Domain @@ -15,7 +16,7 @@ __all__ = ['DomainSerializer', 'DomainWithGatewaySerializer', 'DomainListSeriali class DomainSerializer(ResourceLabelsMixin, BulkOrgResourceModelSerializer): gateways = ObjectRelatedField( - many=True, required=False, label=_('Gateway'), read_only=True, + many=True, required=False, label=_('Gateway'), queryset=Gateway.objects ) class Meta: @@ -38,12 +39,17 @@ class DomainSerializer(ResourceLabelsMixin, BulkOrgResourceModelSerializer): data['assets'] = [i for i in assets if str(i['id']) not in gateway_ids] return data - def update(self, instance, validated_data): + def create(self, validated_data): assets = validated_data.pop('assets', []) - assets = assets + list(instance.gateways) - validated_data['assets'] = assets - instance = super().update(instance, validated_data) - return instance + gateways = validated_data.pop('gateways', []) + validated_data['assets'] = assets + gateways + return super().create(validated_data) + + def update(self, instance, validated_data): + assets = validated_data.pop('assets', list(instance.assets.all())) + gateways = validated_data.pop('gateways', list(instance.gateways.all())) + validated_data['assets'] = assets + gateways + return super().update(instance, validated_data) @classmethod def setup_eager_loading(cls, queryset): @@ -61,7 +67,7 @@ class DomainListSerializer(DomainSerializer): @classmethod def setup_eager_loading(cls, queryset): queryset = queryset.annotate( - assets_amount=Count('assets', distinct=True), + assets_amount=Count('assets', filter=~Q(assets__platform__name='Gateway'), distinct=True), ) return queryset