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",
|
2020-03-12 08:24:38 +00:00
|
|
|
"NodeAssetsSerializer", "NodeTaskSerializer",
|
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")
|
|
|
|
)
|
2022-01-12 12:09:56 +00:00
|
|
|
full_value = serializers.CharField(
|
|
|
|
required=False, allow_blank=True, allow_null=True, label=_("Full 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):
|
2020-10-30 02:16:49 +00:00
|
|
|
if '/' in data:
|
|
|
|
error = _("Can't contains: " + "/")
|
|
|
|
raise serializers.ValidationError(error)
|
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
|
|
|
|
2022-01-12 12:09:56 +00:00
|
|
|
def create(self, validated_data):
|
|
|
|
full_value = validated_data.get('full_value')
|
|
|
|
|
|
|
|
# 直接多层级创建
|
|
|
|
if full_value:
|
|
|
|
node = Node.create_node_by_full_value(full_value)
|
|
|
|
# 根据 value 在 root 下创建
|
|
|
|
else:
|
|
|
|
key = Node.org_root().get_next_child_key()
|
2022-03-02 07:18:43 +00:00
|
|
|
validated_data['key'] = key
|
|
|
|
node = Node.objects.create(**validated_data)
|
2022-01-12 12:09:56 +00:00
|
|
|
return node
|
|
|
|
|
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
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
|
|
|
|
class NodeTaskSerializer(serializers.Serializer):
|
|
|
|
ACTION_CHOICES = (
|
|
|
|
('refresh', 'refresh'),
|
|
|
|
('test', 'test'),
|
|
|
|
('refresh_cache', 'refresh_cache'),
|
|
|
|
)
|
|
|
|
task = serializers.CharField(read_only=True)
|
|
|
|
action = serializers.ChoiceField(choices=ACTION_CHOICES, write_only=True)
|