perf: 优化资产、网域、网关的操作体验

pull/12887/head
wangruidong 2024-03-26 10:21:44 +08:00 committed by Bryan
parent f11852c60d
commit f0cbd77310
3 changed files with 19 additions and 8 deletions

View File

@ -32,6 +32,7 @@ __all__ = [
class AssetFilterSet(BaseFilterSet): class AssetFilterSet(BaseFilterSet):
platform = django_filters.CharFilter(method='filter_platform') 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') domain = django_filters.CharFilter(method='filter_domain')
type = django_filters.CharFilter(field_name="platform__type", lookup_expr="exact") type = django_filters.CharFilter(field_name="platform__type", lookup_expr="exact")
category = django_filters.CharFilter(field_name="platform__category", lookup_expr="exact") category = django_filters.CharFilter(field_name="platform__category", lookup_expr="exact")

View File

@ -30,6 +30,10 @@ class DomainViewSet(OrgBulkModelViewSet):
return serializers.DomainWithGatewaySerializer return serializers.DomainWithGatewaySerializer
return super().get_serializer_class() return super().get_serializer_class()
def partial_update(self, request, *args, **kwargs):
kwargs['partial'] = True
return self.update(request, *args, **kwargs)
class GatewayViewSet(HostViewSet): class GatewayViewSet(HostViewSet):
perm_model = Gateway perm_model = Gateway

View File

@ -1,12 +1,13 @@
# -*- coding: utf-8 -*- # -*- 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 django.utils.translation import gettext_lazy as _
from rest_framework import serializers from rest_framework import serializers
from common.serializers import ResourceLabelsMixin from common.serializers import ResourceLabelsMixin
from common.serializers.fields import ObjectRelatedField from common.serializers.fields import ObjectRelatedField
from orgs.mixins.serializers import BulkOrgResourceModelSerializer from orgs.mixins.serializers import BulkOrgResourceModelSerializer
from assets.models.gateway import Gateway
from .gateway import GatewayWithAccountSecretSerializer from .gateway import GatewayWithAccountSecretSerializer
from ..models import Domain from ..models import Domain
@ -15,7 +16,7 @@ __all__ = ['DomainSerializer', 'DomainWithGatewaySerializer', 'DomainListSeriali
class DomainSerializer(ResourceLabelsMixin, BulkOrgResourceModelSerializer): class DomainSerializer(ResourceLabelsMixin, BulkOrgResourceModelSerializer):
gateways = ObjectRelatedField( gateways = ObjectRelatedField(
many=True, required=False, label=_('Gateway'), read_only=True, many=True, required=False, label=_('Gateway'), queryset=Gateway.objects
) )
class Meta: 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] data['assets'] = [i for i in assets if str(i['id']) not in gateway_ids]
return data return data
def update(self, instance, validated_data): def create(self, validated_data):
assets = validated_data.pop('assets', []) assets = validated_data.pop('assets', [])
assets = assets + list(instance.gateways) gateways = validated_data.pop('gateways', [])
validated_data['assets'] = assets validated_data['assets'] = assets + gateways
instance = super().update(instance, validated_data) return super().create(validated_data)
return instance
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 @classmethod
def setup_eager_loading(cls, queryset): def setup_eager_loading(cls, queryset):
@ -61,7 +67,7 @@ class DomainListSerializer(DomainSerializer):
@classmethod @classmethod
def setup_eager_loading(cls, queryset): def setup_eager_loading(cls, queryset):
queryset = queryset.annotate( queryset = queryset.annotate(
assets_amount=Count('assets', distinct=True), assets_amount=Count('assets', filter=~Q(assets__platform__name='Gateway'), distinct=True),
) )
return queryset return queryset