From 411d199e18c50340a50f1e3f2bb838843acba45b Mon Sep 17 00:00:00 2001 From: ibuler Date: Tue, 27 Dec 2022 16:53:23 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BF=AE=E6=94=B9=20asset=20serializer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/assets/api/asset/asset.py | 1 + apps/assets/serializers/asset/common.py | 70 ++++++++++++------------- 2 files changed, 36 insertions(+), 35 deletions(-) diff --git a/apps/assets/api/asset/asset.py b/apps/assets/api/asset/asset.py index cd4166390..1394acbb7 100644 --- a/apps/assets/api/asset/asset.py +++ b/apps/assets/api/asset/asset.py @@ -60,6 +60,7 @@ class AssetViewSet(SuggestionMixin, NodeFilterMixin, OrgBulkModelViewSet): ordering = ("name",) serializer_classes = ( ("default", serializers.AssetSerializer), + ("retrieve", serializers.AssetDetailSerializer), ("suggestion", serializers.MiniAssetSerializer), ("platform", serializers.PlatformSerializer), ("gateways", serializers.GatewaySerializer), diff --git a/apps/assets/serializers/asset/common.py b/apps/assets/serializers/asset/common.py index e34af1f5f..36bb66803 100644 --- a/apps/assets/serializers/asset/common.py +++ b/apps/assets/serializers/asset/common.py @@ -1,21 +1,22 @@ # -*- coding: utf-8 -*- # -from rest_framework import serializers -from django.utils.translation import ugettext_lazy as _ -from django.db.transaction import atomic from django.db.models import F +from django.db.transaction import atomic +from django.utils.translation import ugettext_lazy as _ +from rest_framework import serializers -from common.drf.serializers import WritableNestedModelSerializer from common.drf.fields import LabeledChoiceField, ObjectRelatedField +from common.drf.serializers import WritableNestedModelSerializer from orgs.mixins.serializers import BulkOrgResourceSerializerMixin from ..account import AccountSerializer -from ...models import Asset, Node, Platform, Label, Domain, Account, Protocol from ...const import Category, AllTypes +from ...models import Asset, Node, Platform, Label, Domain, Account, Protocol __all__ = [ 'AssetSerializer', 'AssetSimpleSerializer', 'MiniAssetSerializer', 'AssetTaskSerializer', 'AssetsTaskSerializer', 'AssetProtocolsSerializer', + 'AssetDetailSerializer', ] @@ -66,21 +67,19 @@ class AssetSerializer(BulkOrgResourceSerializerMixin, WritableNestedModelSeriali nodes = ObjectRelatedField(many=True, required=False, queryset=Node.objects, label=_('Nodes')) labels = AssetLabelSerializer(many=True, required=False, label=_('Labels')) protocols = AssetProtocolsSerializer(many=True, required=False, label=_('Protocols')) - accounts = AssetAccountSerializer(many=True, required=False, label=_('Accounts')) - enabled_info = serializers.SerializerMethodField() class Meta: model = Asset - fields_mini = ['id', 'name', 'address', 'enabled_info'] + fields_mini = ['id', 'name', 'address'] fields_small = fields_mini + ['is_active', 'comment'] fields_fk = ['domain', 'platform', 'platform'] fields_m2m = [ 'nodes', 'labels', 'protocols', 'accounts', 'nodes_display', ] read_only_fields = [ - 'category', 'type', 'specific', 'info', - 'connectivity', 'date_verified', 'created_by', - 'date_created' + 'category', 'type', 'info', + 'connectivity', 'date_verified', + 'created_by', 'date_created' ] fields = fields_small + fields_fk + fields_m2m + read_only_fields extra_kwargs = { @@ -88,36 +87,13 @@ class AssetSerializer(BulkOrgResourceSerializerMixin, WritableNestedModelSeriali 'address': {'label': _('Address')}, } - def get_field_names(self, declared_fields, info): - names = super().get_field_names(declared_fields, info) - if self.__class__.__name__ != 'AssetSerializer': - names.remove('specific') - return names - - @staticmethod - def get_enabled_info(obj): - platform = obj.platform - automation = platform.automation - return { - 'su_enabled': platform.su_enabled, - 'ping_enabled': automation.ping_enabled, - 'domain_enabled': platform.domain_enabled, - 'ansible_enabled': automation.ansible_enabled, - 'protocols_enabled': platform.protocols_enabled, - 'gather_facts_enabled': automation.gather_facts_enabled, - 'push_account_enabled': automation.push_account_enabled, - 'change_secret_enabled': automation.change_secret_enabled, - 'verify_account_enabled': automation.verify_account_enabled, - 'gather_accounts_enabled': automation.gather_accounts_enabled, - } - @classmethod def setup_eager_loading(cls, queryset): """ Perform necessary eager loading of data. """ queryset = queryset.prefetch_related('domain', 'platform', 'protocols') \ .annotate(category=F("platform__category")) \ .annotate(type=F("platform__type")) - queryset = queryset.prefetch_related('nodes', 'labels') + queryset = queryset.prefetch_related('nodes', 'labels', 'accounts') return queryset def perform_nodes_display_create(self, instance, nodes_display): @@ -188,6 +164,30 @@ class AssetSerializer(BulkOrgResourceSerializerMixin, WritableNestedModelSeriali return instance +class AssetDetailSerializer(AssetSerializer): + accounts = AssetAccountSerializer(many=True, required=False, label=_('Accounts')) + enabled_info = serializers.SerializerMethodField() + + class Meta(AssetSerializer.Meta): + fields = AssetSerializer.Meta.fields + ['accounts', 'enabled_info', 'info', 'specific'] + + @staticmethod + def get_enabled_info(obj): + platform = obj.platform + automation = platform.automation + return { + 'su_enabled': platform.su_enabled, + 'ping_enabled': automation.ping_enabled, + 'domain_enabled': platform.domain_enabled, + 'ansible_enabled': automation.ansible_enabled, + 'protocols_enabled': platform.protocols_enabled, + 'gather_facts_enabled': automation.gather_facts_enabled, + 'change_secret_enabled': automation.change_secret_enabled, + 'verify_account_enabled': automation.verify_account_enabled, + 'gather_accounts_enabled': automation.gather_accounts_enabled, + } + + class MiniAssetSerializer(serializers.ModelSerializer): class Meta: model = Asset