mirror of https://github.com/jumpserver/jumpserver
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.5 KiB
58 lines
1.5 KiB
# -*- coding: utf-8 -*-
|
|
from rest_framework import serializers
|
|
from rest_framework_bulk.serializers import BulkListSerializer
|
|
|
|
from common.mixins import BulkSerializerMixin
|
|
from ..models import Asset, Node
|
|
from .asset import AssetGrantedSerializer
|
|
|
|
|
|
class NodeGrantedSerializer(BulkSerializerMixin, serializers.ModelSerializer):
|
|
"""
|
|
授权资产组
|
|
"""
|
|
assets_granted = AssetGrantedSerializer(many=True, read_only=True)
|
|
assets_amount = serializers.SerializerMethodField()
|
|
parent = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = Node
|
|
fields = [
|
|
'id', 'key', 'value', 'parent',
|
|
'assets_granted', 'assets_amount',
|
|
]
|
|
|
|
@staticmethod
|
|
def get_assets_amount(obj):
|
|
return len(obj.assets_granted)
|
|
|
|
@staticmethod
|
|
def get_parent(obj):
|
|
return obj.parent.id
|
|
|
|
|
|
class NodeSerializer(serializers.ModelSerializer):
|
|
parent = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = Node
|
|
fields = ['id', 'key', 'value', 'parent']
|
|
list_serializer_class = BulkListSerializer
|
|
|
|
@staticmethod
|
|
def get_parent(obj):
|
|
return obj.parent.id
|
|
|
|
def get_fields(self):
|
|
fields = super().get_fields()
|
|
field = fields["key"]
|
|
field.required = False
|
|
return fields
|
|
|
|
|
|
class NodeAssetsSerializer(serializers.ModelSerializer):
|
|
assets = serializers.PrimaryKeyRelatedField(many=True, queryset=Asset.objects.all())
|
|
|
|
class Meta:
|
|
model = Node
|
|
fields = ['assets'] |