mirror of https://github.com/jumpserver/jumpserver
perf: 修改 asset serializer
parent
3336f2ba67
commit
411d199e18
|
@ -60,6 +60,7 @@ class AssetViewSet(SuggestionMixin, NodeFilterMixin, OrgBulkModelViewSet):
|
||||||
ordering = ("name",)
|
ordering = ("name",)
|
||||||
serializer_classes = (
|
serializer_classes = (
|
||||||
("default", serializers.AssetSerializer),
|
("default", serializers.AssetSerializer),
|
||||||
|
("retrieve", serializers.AssetDetailSerializer),
|
||||||
("suggestion", serializers.MiniAssetSerializer),
|
("suggestion", serializers.MiniAssetSerializer),
|
||||||
("platform", serializers.PlatformSerializer),
|
("platform", serializers.PlatformSerializer),
|
||||||
("gateways", serializers.GatewaySerializer),
|
("gateways", serializers.GatewaySerializer),
|
||||||
|
|
|
@ -1,21 +1,22 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- 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.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.fields import LabeledChoiceField, ObjectRelatedField
|
||||||
|
from common.drf.serializers import WritableNestedModelSerializer
|
||||||
from orgs.mixins.serializers import BulkOrgResourceSerializerMixin
|
from orgs.mixins.serializers import BulkOrgResourceSerializerMixin
|
||||||
from ..account import AccountSerializer
|
from ..account import AccountSerializer
|
||||||
from ...models import Asset, Node, Platform, Label, Domain, Account, Protocol
|
|
||||||
from ...const import Category, AllTypes
|
from ...const import Category, AllTypes
|
||||||
|
from ...models import Asset, Node, Platform, Label, Domain, Account, Protocol
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
'AssetSerializer', 'AssetSimpleSerializer', 'MiniAssetSerializer',
|
'AssetSerializer', 'AssetSimpleSerializer', 'MiniAssetSerializer',
|
||||||
'AssetTaskSerializer', 'AssetsTaskSerializer', 'AssetProtocolsSerializer',
|
'AssetTaskSerializer', 'AssetsTaskSerializer', 'AssetProtocolsSerializer',
|
||||||
|
'AssetDetailSerializer',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@ -66,21 +67,19 @@ class AssetSerializer(BulkOrgResourceSerializerMixin, WritableNestedModelSeriali
|
||||||
nodes = ObjectRelatedField(many=True, required=False, queryset=Node.objects, label=_('Nodes'))
|
nodes = ObjectRelatedField(many=True, required=False, queryset=Node.objects, label=_('Nodes'))
|
||||||
labels = AssetLabelSerializer(many=True, required=False, label=_('Labels'))
|
labels = AssetLabelSerializer(many=True, required=False, label=_('Labels'))
|
||||||
protocols = AssetProtocolsSerializer(many=True, required=False, label=_('Protocols'))
|
protocols = AssetProtocolsSerializer(many=True, required=False, label=_('Protocols'))
|
||||||
accounts = AssetAccountSerializer(many=True, required=False, label=_('Accounts'))
|
|
||||||
enabled_info = serializers.SerializerMethodField()
|
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Asset
|
model = Asset
|
||||||
fields_mini = ['id', 'name', 'address', 'enabled_info']
|
fields_mini = ['id', 'name', 'address']
|
||||||
fields_small = fields_mini + ['is_active', 'comment']
|
fields_small = fields_mini + ['is_active', 'comment']
|
||||||
fields_fk = ['domain', 'platform', 'platform']
|
fields_fk = ['domain', 'platform', 'platform']
|
||||||
fields_m2m = [
|
fields_m2m = [
|
||||||
'nodes', 'labels', 'protocols', 'accounts', 'nodes_display',
|
'nodes', 'labels', 'protocols', 'accounts', 'nodes_display',
|
||||||
]
|
]
|
||||||
read_only_fields = [
|
read_only_fields = [
|
||||||
'category', 'type', 'specific', 'info',
|
'category', 'type', 'info',
|
||||||
'connectivity', 'date_verified', 'created_by',
|
'connectivity', 'date_verified',
|
||||||
'date_created'
|
'created_by', 'date_created'
|
||||||
]
|
]
|
||||||
fields = fields_small + fields_fk + fields_m2m + read_only_fields
|
fields = fields_small + fields_fk + fields_m2m + read_only_fields
|
||||||
extra_kwargs = {
|
extra_kwargs = {
|
||||||
|
@ -88,36 +87,13 @@ class AssetSerializer(BulkOrgResourceSerializerMixin, WritableNestedModelSeriali
|
||||||
'address': {'label': _('Address')},
|
'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
|
@classmethod
|
||||||
def setup_eager_loading(cls, queryset):
|
def setup_eager_loading(cls, queryset):
|
||||||
""" Perform necessary eager loading of data. """
|
""" Perform necessary eager loading of data. """
|
||||||
queryset = queryset.prefetch_related('domain', 'platform', 'protocols') \
|
queryset = queryset.prefetch_related('domain', 'platform', 'protocols') \
|
||||||
.annotate(category=F("platform__category")) \
|
.annotate(category=F("platform__category")) \
|
||||||
.annotate(type=F("platform__type"))
|
.annotate(type=F("platform__type"))
|
||||||
queryset = queryset.prefetch_related('nodes', 'labels')
|
queryset = queryset.prefetch_related('nodes', 'labels', 'accounts')
|
||||||
return queryset
|
return queryset
|
||||||
|
|
||||||
def perform_nodes_display_create(self, instance, nodes_display):
|
def perform_nodes_display_create(self, instance, nodes_display):
|
||||||
|
@ -188,6 +164,30 @@ class AssetSerializer(BulkOrgResourceSerializerMixin, WritableNestedModelSeriali
|
||||||
return instance
|
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 MiniAssetSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Asset
|
model = Asset
|
||||||
|
|
Loading…
Reference in New Issue