jumpserver/apps/assets/serializers/node.py

49 lines
1.4 KiB
Python
Raw Normal View History

2018-02-07 15:25:15 +00:00
# -*- coding: utf-8 -*-
from rest_framework import serializers
2019-06-28 14:07:22 +00:00
from django.utils.translation import ugettext as _
2018-02-07 15:25:15 +00:00
from orgs.mixins import BulkOrgResourceModelSerializer
2018-02-07 15:25:15 +00:00
from ..models import Asset, Node
2018-04-24 03:07:09 +00:00
__all__ = [
'NodeSerializer', "NodeAddChildrenSerializer",
"NodeAssetsSerializer",
2018-04-24 03:07:09 +00:00
]
class NodeSerializer(BulkOrgResourceModelSerializer):
assets_amount = serializers.IntegerField(read_only=True)
name = serializers.ReadOnlyField(source='value')
2018-02-07 15:25:15 +00:00
class Meta:
model = Node
only_fields = ['id', 'key', 'value', 'org_id']
fields = only_fields + ['name', 'assets_amount']
read_only_fields = [
'key', 'name', 'assets_amount', 'org_id',
]
2018-02-07 15:25:15 +00:00
def validate_value(self, data):
2018-05-15 07:05:49 +00:00
instance = self.instance if self.instance else Node.root()
2019-06-28 14:07:22 +00:00
children = instance.parent.get_children()
children_values = [node.value for node in children if node != instance]
if data in children_values:
2018-05-15 07:05:49 +00:00
raise serializers.ValidationError(
2019-06-28 14:07:22 +00:00
_('The same level node name cannot be the same')
2018-05-15 07:05:49 +00:00
)
return data
2018-02-07 15:25:15 +00:00
class NodeAssetsSerializer(serializers.ModelSerializer):
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()