jumpserver/apps/assets/serializers/node.py

98 lines
2.6 KiB
Python
Raw Normal View History

2018-02-07 15:25:15 +00:00
# -*- 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
2018-04-24 03:07:09 +00:00
__all__ = [
'NodeSerializer', "NodeGrantedSerializer", "NodeAddChildrenSerializer",
"NodeAssetsSerializer",
2018-04-24 03:07:09 +00:00
]
2018-02-07 15:25:15 +00:00
class NodeGrantedSerializer(BulkSerializerMixin, serializers.ModelSerializer):
"""
授权资产组
"""
assets_granted = AssetGrantedSerializer(many=True, read_only=True)
assets_amount = serializers.SerializerMethodField()
parent = serializers.SerializerMethodField()
2018-02-25 10:08:00 +00:00
name = serializers.SerializerMethodField()
2018-02-07 15:25:15 +00:00
class Meta:
model = Node
fields = [
2018-02-25 10:08:00 +00:00
'id', 'key', 'name', 'value', 'parent',
'assets_granted', 'assets_amount', 'org_id',
2018-02-07 15:25:15 +00:00
]
@staticmethod
def get_assets_amount(obj):
return len(obj.assets_granted)
2018-02-25 10:08:00 +00:00
@staticmethod
def get_name(obj):
return obj.name
2018-02-07 15:25:15 +00:00
@staticmethod
def get_parent(obj):
return obj.parent.id
class NodeSerializer(serializers.ModelSerializer):
2018-02-25 14:36:42 +00:00
assets_amount = serializers.SerializerMethodField()
2018-08-06 09:16:52 +00:00
tree_id = serializers.SerializerMethodField()
tree_parent = serializers.SerializerMethodField()
2018-02-07 15:25:15 +00:00
class Meta:
model = Node
fields = [
2018-08-06 09:16:52 +00:00
'id', 'key', 'value', 'assets_amount',
'is_node', 'org_id', 'tree_id', 'tree_parent',
]
2018-02-07 15:25:15 +00:00
list_serializer_class = BulkListSerializer
def validate(self, data):
value = data.get('value')
2018-05-15 07:05:49 +00:00
instance = self.instance if self.instance else Node.root()
children = instance.parent.get_children().exclude(key=instance.key)
values = [child.value for child in children]
if value in values:
raise serializers.ValidationError(
'The same level node name cannot be the same'
)
return data
2018-02-07 15:25:15 +00:00
@staticmethod
2018-08-06 09:16:52 +00:00
def get_assets_amount(obj):
return obj.get_all_assets().count()
2018-02-07 15:25:15 +00:00
2018-02-25 14:36:42 +00:00
@staticmethod
2018-08-06 09:16:52 +00:00
def get_tree_id(obj):
return obj.key
@staticmethod
def get_tree_parent(obj):
return obj.parent_key
2018-02-25 14:36:42 +00:00
2018-02-07 15:25:15 +00:00
def get_fields(self):
fields = super().get_fields()
field = fields["key"]
field.required = False
return fields
class NodeAssetsSerializer(serializers.ModelSerializer):
2018-07-14 16:55:05 +00:00
assets = serializers.PrimaryKeyRelatedField(many=True, queryset = Asset.objects.all())
2018-02-07 15:25:15 +00:00
class Meta:
model = Node
2018-03-01 04:40:41 +00:00
fields = ['assets']
class NodeAddChildrenSerializer(serializers.Serializer):
nodes = serializers.ListField()