[Bugfix] 资产树节点Node Value值校验修改异常抛出为400

pull/1312/head
BaiJiangjie 2018-05-11 11:27:48 +08:00
parent 4c53eebdbe
commit cbc4a0a97b
2 changed files with 11 additions and 5 deletions

View File

@ -14,6 +14,7 @@
# limitations under the License. # limitations under the License.
from rest_framework import generics, mixins from rest_framework import generics, mixins
from rest_framework.serializers import ValidationError
from rest_framework.views import APIView from rest_framework.views import APIView
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework_bulk import BulkModelViewSet from rest_framework_bulk import BulkModelViewSet
@ -110,7 +111,9 @@ class NodeChildrenApi(mixins.ListModelMixin, generics.CreateAPIView):
value = request.data.get("value") value = request.data.get("value")
values = [child.value for child in instance.get_children()] values = [child.value for child in instance.get_children()]
if value in values: if value in values:
raise AssertionError('The same level node name cannot be the same') raise ValidationError(
'The same level node name cannot be the same'
)
node = instance.create_child(value=value) node = instance.create_child(value=value)
return Response( return Response(
{"id": node.id, "key": node.key, "value": node.value}, {"id": node.id, "key": node.key, "value": node.value},

View File

@ -51,14 +51,17 @@ class NodeSerializer(serializers.ModelSerializer):
fields = ['id', 'key', 'value', 'parent', 'assets_amount', 'is_node'] fields = ['id', 'key', 'value', 'parent', 'assets_amount', 'is_node']
list_serializer_class = BulkListSerializer list_serializer_class = BulkListSerializer
def update(self, instance, validated_data): def validate(self, data):
value = validated_data.get('value') value = data.get('value')
instance = self.instance
if not instance.is_root(): if not instance.is_root():
children = instance.parent.get_children().exclude(key=instance.key) children = instance.parent.get_children().exclude(key=instance.key)
values = [child.value for child in children] values = [child.value for child in children]
if value in values: if value in values:
raise AssertionError('The same level node name cannot be the same') raise serializers.ValidationError(
return super().update(instance, validated_data) 'The same level node name cannot be the same'
)
return data
@staticmethod @staticmethod
def get_parent(obj): def get_parent(obj):