2018-02-07 15:25:15 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from rest_framework import serializers
|
|
|
|
|
|
|
|
from ..models import Asset, Node
|
|
|
|
|
|
|
|
|
2018-04-24 03:07:09 +00:00
|
|
|
__all__ = [
|
2018-12-17 10:20:44 +00:00
|
|
|
'NodeSerializer', "NodeAddChildrenSerializer",
|
2018-05-29 03:50:09 +00:00
|
|
|
"NodeAssetsSerializer",
|
2018-04-24 03:07:09 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2018-02-07 15:25:15 +00:00
|
|
|
class NodeSerializer(serializers.ModelSerializer):
|
2018-12-17 10:20:44 +00:00
|
|
|
assets_amount = serializers.IntegerField(read_only=True)
|
2018-02-07 15:25:15 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Node
|
2018-07-27 03:59:39 +00:00
|
|
|
fields = [
|
2018-12-17 10:20:44 +00:00
|
|
|
'id', 'key', 'value', 'assets_amount', 'org_id',
|
2018-07-27 03:59:39 +00:00
|
|
|
]
|
2018-12-17 03:49:57 +00:00
|
|
|
read_only_fields = [
|
2019-03-19 11:09:09 +00:00
|
|
|
'key', 'assets_amount', 'org_id',
|
2018-12-17 03:49:57 +00:00
|
|
|
]
|
2018-02-07 15:25:15 +00:00
|
|
|
|
2018-12-17 10:20:44 +00:00
|
|
|
def validate_value(self, data):
|
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]
|
2018-12-17 10:20:44 +00:00
|
|
|
if data in values:
|
2018-05-15 07:05:49 +00:00
|
|
|
raise serializers.ValidationError(
|
|
|
|
'The same level node name cannot be the same'
|
|
|
|
)
|
2018-05-11 03:27:48 +00:00
|
|
|
return data
|
2018-05-10 12:48:16 +00:00
|
|
|
|
2018-02-07 15:25:15 +00:00
|
|
|
|
|
|
|
class NodeAssetsSerializer(serializers.ModelSerializer):
|
2018-12-10 02:11:54 +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()
|
2018-12-17 10:20:44 +00:00
|
|
|
|