mirror of https://github.com/jumpserver/jumpserver
perf: 修改 metadata
parent
81219e1e7c
commit
7ae395f7e8
|
@ -6,7 +6,7 @@ from django.db.transaction import atomic
|
||||||
from django.db.models import F
|
from django.db.models import F
|
||||||
|
|
||||||
from common.drf.serializers import JMSWritableNestedModelSerializer
|
from common.drf.serializers import JMSWritableNestedModelSerializer
|
||||||
from common.drf.fields import LabeledChoiceField, ObjectedRelatedField
|
from common.drf.fields import LabeledChoiceField, ObjectRelatedField
|
||||||
from ..account import AccountSerializer
|
from ..account import AccountSerializer
|
||||||
from ...models import Asset, Node, Platform, Protocol, Label, Domain
|
from ...models import Asset, Node, Platform, Protocol, Label, Domain
|
||||||
from ...const import Category, AllTypes
|
from ...const import Category, AllTypes
|
||||||
|
@ -45,9 +45,9 @@ class AssetPlatformSerializer(serializers.ModelSerializer):
|
||||||
class AssetSerializer(JMSWritableNestedModelSerializer):
|
class AssetSerializer(JMSWritableNestedModelSerializer):
|
||||||
category = LabeledChoiceField(choices=Category.choices, read_only=True, label=_('Category'))
|
category = LabeledChoiceField(choices=Category.choices, read_only=True, label=_('Category'))
|
||||||
type = LabeledChoiceField(choices=AllTypes.choices, read_only=True, label=_('Type'))
|
type = LabeledChoiceField(choices=AllTypes.choices, read_only=True, label=_('Type'))
|
||||||
domain = ObjectedRelatedField(required=False, queryset=Domain.objects, label=_('Domain'))
|
domain = ObjectRelatedField(required=False, queryset=Domain.objects, label=_('Domain'))
|
||||||
platform = ObjectedRelatedField(required=False, queryset=Platform.objects, label=_('Platform'))
|
platform = ObjectRelatedField(required=False, queryset=Platform.objects, label=_('Platform'))
|
||||||
nodes = ObjectedRelatedField(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'))
|
||||||
accounts = AccountSerializer(many=True, required=False, label=_('Accounts'))
|
accounts = AccountSerializer(many=True, required=False, label=_('Accounts'))
|
||||||
protocols = AssetProtocolsSerializer(many=True, required=False, label=_('Protocols'))
|
protocols = AssetProtocolsSerializer(many=True, required=False, label=_('Protocols'))
|
||||||
|
|
|
@ -10,7 +10,7 @@ from common.utils import decrypt_password
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
'ReadableHiddenField', 'EncryptedField', 'LabeledChoiceField',
|
'ReadableHiddenField', 'EncryptedField', 'LabeledChoiceField',
|
||||||
'ObjectedRelatedField',
|
'ObjectRelatedField',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@ -63,9 +63,10 @@ class LabeledChoiceField(ChoiceField):
|
||||||
return super(LabeledChoiceField, self).to_internal_value(data)
|
return super(LabeledChoiceField, self).to_internal_value(data)
|
||||||
|
|
||||||
|
|
||||||
class ObjectedRelatedField(serializers.RelatedField):
|
class ObjectRelatedField(serializers.RelatedField):
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
self.attrs = kwargs.pop('attrs', None) or ('id', 'name')
|
self.attrs = kwargs.pop('attrs', None) or ('id', 'name')
|
||||||
|
self.many = kwargs.get('many', False)
|
||||||
super().__init__(**kwargs)
|
super().__init__(**kwargs)
|
||||||
|
|
||||||
def to_representation(self, value):
|
def to_representation(self, value):
|
||||||
|
|
|
@ -90,8 +90,19 @@ class SimpleMetadataWithFilters(SimpleMetadata):
|
||||||
for choice_value, choice_name in dict(field.choices).items()
|
for choice_value, choice_name in dict(field.choices).items()
|
||||||
]
|
]
|
||||||
|
|
||||||
if field.__class__.__name__ == 'LabeledChoiceField':
|
class_name = field.__class__.__name__
|
||||||
|
if class_name == 'LabeledChoiceField':
|
||||||
field_info['type'] = 'labeled_choice'
|
field_info['type'] = 'labeled_choice'
|
||||||
|
elif class_name == 'ObjectRelatedField':
|
||||||
|
field_info['type'] = 'object_related_field'
|
||||||
|
elif class_name == 'ManyRelatedField':
|
||||||
|
child_relation_class_name = field.child_relation.__class__.__name__
|
||||||
|
if child_relation_class_name == 'ObjectRelatedField':
|
||||||
|
field_info['type'] = 'm2m_related_field'
|
||||||
|
|
||||||
|
# if field.label == '系统平台':
|
||||||
|
# print("Field: ", class_name, field, field_info)
|
||||||
|
|
||||||
return field_info
|
return field_info
|
||||||
|
|
||||||
def get_filters_fields(self, request, view):
|
def get_filters_fields(self, request, view):
|
||||||
|
|
Loading…
Reference in New Issue