Browse Source

perf: 修改脚本

pull/8873/head
ibuler 2 years ago
parent
commit
87df92ea92
  1. 34
      apps/assets/serializers/asset/common.py
  2. 18
      apps/common/drf/serializers.py

34
apps/assets/serializers/asset/common.py

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

18
apps/common/drf/serializers.py

@ -1,20 +1,19 @@
import copy
from rest_framework import serializers
from rest_framework.serializers import Serializer
from rest_framework.serializers import ModelSerializer
from rest_framework_bulk.serializers import BulkListSerializer
from django.utils.translation import gettext_lazy as _
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.serializers import BulkSerializerMixin
from common.drf.fields import EncryptedField
__all__ = [
'MethodSerializer',
'EmptySerializer', 'BulkModelSerializer', 'AdaptedBulkListSerializer', 'CeleryTaskSerializer',
'SecretReadableMixin'
'MethodSerializer', 'EmptySerializer', 'BulkModelSerializer',
'AdaptedBulkListSerializer', 'CeleryTaskSerializer',
'SecretReadableMixin', 'JMSWritableNestedModelSerializer',
]
@ -112,3 +111,12 @@ class SecretReadableMixin(serializers.Serializer):
if 'write_only' not in field_extra_kwargs:
continue
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