feat: node api 支持使用 full_value 递归创建

pull/7486/head
ibuler 2022-01-12 20:09:56 +08:00 committed by Jiangjie.Bai
parent b0932e5137
commit dde7559f47
2 changed files with 18 additions and 8 deletions

View File

@ -17,7 +17,7 @@ from common.mixins.api import SuggestionMixin
from assets.models import Asset
from common.utils import get_logger, get_object_or_none
from common.tree import TreeNodeSerializer
from orgs.mixins.api import OrgModelViewSet
from orgs.mixins.api import OrgBulkModelViewSet
from orgs.mixins import generics
from orgs.utils import current_org
from ..hands import IsOrgAdmin
@ -42,19 +42,13 @@ __all__ = [
]
class NodeViewSet(SuggestionMixin, OrgModelViewSet):
class NodeViewSet(SuggestionMixin, OrgBulkModelViewSet):
model = Node
filterset_fields = ('value', 'key', 'id')
search_fields = ('value', )
permission_classes = (IsOrgAdmin,)
serializer_class = serializers.NodeSerializer
# 仅支持根节点指直接创建子节点下的节点需要通过children接口创建
def perform_create(self, serializer):
child_key = Node.org_root().get_next_child_key()
serializer.validated_data["key"] = child_key
serializer.save()
@action(methods=[POST], detail=False, url_path='check_assets_amount_task')
def check_assets_amount_task(self, request):
task = check_node_assets_amount_task.delay(current_org.id)

View File

@ -17,6 +17,9 @@ class NodeSerializer(BulkOrgResourceModelSerializer):
value = serializers.CharField(
required=False, allow_blank=True, allow_null=True, label=_("value")
)
full_value = serializers.CharField(
required=False, allow_blank=True, allow_null=True, label=_("Full value")
)
class Meta:
model = Node
@ -40,6 +43,19 @@ class NodeSerializer(BulkOrgResourceModelSerializer):
)
return data
def create(self, validated_data):
full_value = validated_data.get('full_value')
value = validated_data.get('value')
# 直接多层级创建
if full_value:
node = Node.create_node_by_full_value(full_value)
# 根据 value 在 root 下创建
else:
key = Node.org_root().get_next_child_key()
node = Node.objects.create(key=key, value=value)
return node
class NodeAssetsSerializer(BulkOrgResourceModelSerializer):
assets = serializers.PrimaryKeyRelatedField(