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",)
|
||||
serializer_classes = (
|
||||
("default", serializers.AssetSerializer),
|
||||
("retrieve", serializers.AssetDetailSerializer),
|
||||
("suggestion", serializers.MiniAssetSerializer),
|
||||
("platform", serializers.PlatformSerializer),
|
||||
("gateways", serializers.GatewaySerializer),
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue