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
|
|
|
|
2019-06-19 08:45:14 +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__ = [
|
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
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-06-19 08:45:14 +00:00
|
|
|
class NodeSerializer(BulkOrgResourceModelSerializer):
|
2018-12-17 10:20:44 +00:00
|
|
|
assets_amount = serializers.IntegerField(read_only=True)
|
2019-07-22 03:38:09 +00:00
|
|
|
name = serializers.ReadOnlyField(source='value')
|
2018-02-07 15:25:15 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Node
|
2019-07-11 10:12:14 +00:00
|
|
|
only_fields = ['id', 'key', 'value', 'org_id']
|
2019-07-22 03:38:09 +00:00
|
|
|
fields = only_fields + ['name', 'assets_amount']
|
2018-12-17 03:49:57 +00:00
|
|
|
read_only_fields = [
|
2019-07-22 03:38:09 +00:00
|
|
|
'key', 'name', '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()
|
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
|
|
|
)
|
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
|
|
|
|