perf: 修改脚本

pull/8873/head
ibuler 2 years ago
parent 11d9a0e9cc
commit 87df92ea92

@ -2,9 +2,8 @@
# #
from rest_framework import serializers from rest_framework import serializers
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from drf_writable_nested.serializers import WritableNestedModelSerializer
from common.drf.serializers import JMSWritableNestedModelSerializer
from orgs.mixins.serializers import OrgResourceModelSerializerMixin from orgs.mixins.serializers import OrgResourceModelSerializerMixin
from ...models import Asset, Node, Platform, Protocol, Label, Domain from ...models import Asset, Node, Platform, Protocol, Label, Domain
from ..mixin import CategoryDisplayMixin from ..mixin import CategoryDisplayMixin
@ -19,13 +18,13 @@ __all__ = [
class AssetProtocolsSerializer(serializers.ModelSerializer): class AssetProtocolsSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Protocol model = Protocol
fields = ['id', 'name', 'port'] fields = ['pk', 'name', 'port']
class AssetLabelSerializer(serializers.ModelSerializer): class AssetLabelSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Label model = Label
fields = ['id', 'name', 'value'] fields = ['pk', 'name', 'value']
extra_kwargs = { extra_kwargs = {
'value': {'required': False} 'value': {'required': False}
} }
@ -34,7 +33,7 @@ class AssetLabelSerializer(serializers.ModelSerializer):
class AssetPlatformSerializer(serializers.ModelSerializer): class AssetPlatformSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Platform model = Platform
fields = ['id', 'name'] fields = ['pk', 'name']
extra_kwargs = { extra_kwargs = {
'name': {'required': False} 'name': {'required': False}
} }
@ -43,32 +42,26 @@ class AssetPlatformSerializer(serializers.ModelSerializer):
class AssetDomainSerializer(serializers.ModelSerializer): class AssetDomainSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Domain model = Domain
fields = ['id', 'name'] fields = ['pk', 'name']
class AssetNodesSerializer(serializers.ModelSerializer): class AssetNodesSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Node model = Node
fields = ['id', 'value'] fields = ['pk', 'value']
extra_kwargs = { extra_kwargs = {
'value': {'required': False} 'value': {'required': False}
} }
class AssetSerializer(CategoryDisplayMixin, WritableNestedModelSerializer, OrgResourceModelSerializerMixin): class AssetSerializer(CategoryDisplayMixin,
JMSWritableNestedModelSerializer,
OrgResourceModelSerializerMixin):
domain = AssetDomainSerializer(required=False) domain = AssetDomainSerializer(required=False)
# nodes_display = serializers.ListField( nodes_display = serializers.ListField(
# child=serializers.CharField(), label=_('Nodes name'), required=False child=serializers.CharField(), label=_('Nodes name'), required=False
# ) )
labels = AssetLabelSerializer(many=True, required=False) labels = AssetLabelSerializer(many=True, required=False)
# labels_display = serializers.ListField(
# child=serializers.CharField(), label=_('Labels name'),
# required=False, read_only=True
# )
# labels = AssetLabelSerializer(many=True, required=False)
# platform_display = serializers.SlugField(
# source='platform.name', label=_("Platform display"), read_only=True
# )
nodes = AssetNodesSerializer(many=True, required=False) nodes = AssetNodesSerializer(many=True, required=False)
platform = AssetPlatformSerializer(required=False) platform = AssetPlatformSerializer(required=False)
accounts = AccountSerializer(many=True, required=False) accounts = AccountSerializer(many=True, required=False)
@ -88,11 +81,10 @@ class AssetSerializer(CategoryDisplayMixin, WritableNestedModelSerializer, OrgRe
] ]
fields_fk = [ fields_fk = [
'domain', 'platform', 'platform', 'domain', 'platform', 'platform',
# 'domain_display', 'platform_display',
] ]
fields_m2m = [ fields_m2m = [
'nodes', 'labels', 'accounts', 'protocols', 'nodes', 'labels', 'accounts', 'protocols',
# 'labels_display','nodes_display', 'nodes_display',
] ]
read_only_fields = [ read_only_fields = [
'category', 'category_display', 'type', 'type_display', 'category', 'category_display', 'type', 'type_display',

@ -1,20 +1,19 @@
import copy
from rest_framework import serializers from rest_framework import serializers
from rest_framework.serializers import Serializer from rest_framework.serializers import Serializer
from rest_framework.serializers import ModelSerializer from rest_framework.serializers import ModelSerializer
from rest_framework_bulk.serializers import BulkListSerializer from rest_framework_bulk.serializers import BulkListSerializer
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from django.utils.functional import cached_property from django.utils.functional import cached_property
from rest_framework.utils.serializer_helpers import BindingDict from drf_writable_nested.serializers import WritableNestedModelSerializer
from common.mixins import BulkListSerializerMixin from common.mixins import BulkListSerializerMixin
from common.mixins.serializers import BulkSerializerMixin from common.mixins.serializers import BulkSerializerMixin
from common.drf.fields import EncryptedField from common.drf.fields import EncryptedField
__all__ = [ __all__ = [
'MethodSerializer', 'MethodSerializer', 'EmptySerializer', 'BulkModelSerializer',
'EmptySerializer', 'BulkModelSerializer', 'AdaptedBulkListSerializer', 'CeleryTaskSerializer', 'AdaptedBulkListSerializer', 'CeleryTaskSerializer',
'SecretReadableMixin' 'SecretReadableMixin', 'JMSWritableNestedModelSerializer',
] ]
@ -112,3 +111,12 @@ class SecretReadableMixin(serializers.Serializer):
if 'write_only' not in field_extra_kwargs: if 'write_only' not in field_extra_kwargs:
continue continue
serializer_field.write_only = field_extra_kwargs['write_only'] serializer_field.write_only = field_extra_kwargs['write_only']
class JMSWritableNestedModelSerializer(WritableNestedModelSerializer):
def _get_related_pk(self, data, model_class):
pk = data.get('pk') or data.get('id') or data.get(model_class._meta.pk.attname)
if pk:
return str(pk)
return None

Loading…
Cancel
Save