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-08-21 12:27:21 +00:00
|
|
|
from orgs.mixins.serializers 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):
|
2019-07-22 03:38:09 +00:00
|
|
|
name = serializers.ReadOnlyField(source='value')
|
2019-08-28 03:43:55 +00:00
|
|
|
value = serializers.CharField(
|
|
|
|
required=False, allow_blank=True, allow_null=True, label=_("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-08-21 12:27:21 +00:00
|
|
|
fields = only_fields + ['name', 'full_value']
|
|
|
|
read_only_fields = ['key', 'org_id']
|
2018-02-07 15:25:15 +00:00
|
|
|
|
2018-12-17 10:20:44 +00:00
|
|
|
def validate_value(self, data):
|
2019-10-08 03:57:22 +00:00
|
|
|
if self.instance:
|
|
|
|
instance = self.instance
|
|
|
|
siblings = instance.get_siblings()
|
|
|
|
else:
|
|
|
|
instance = Node.org_root()
|
|
|
|
siblings = instance.get_children()
|
2019-08-21 12:27:21 +00:00
|
|
|
if siblings.filter(value=data):
|
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
|
|
|
|
2019-10-18 07:05:45 +00:00
|
|
|
class NodeAssetsSerializer(BulkOrgResourceModelSerializer):
|
|
|
|
assets = serializers.PrimaryKeyRelatedField(
|
|
|
|
many=True, queryset=Asset.objects
|
|
|
|
)
|
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
|
|
|
|