From dde7559f47107d6626584c42ae2c6d7bf59fd53a Mon Sep 17 00:00:00 2001 From: ibuler Date: Wed, 12 Jan 2022 20:09:56 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20node=20api=20=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=20full=5Fvalue=20=E9=80=92=E5=BD=92=E5=88=9B?= =?UTF-8?q?=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/assets/api/node.py | 10 ++-------- apps/assets/serializers/node.py | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/apps/assets/api/node.py b/apps/assets/api/node.py index 7e4ff61aa..c7f32b306 100644 --- a/apps/assets/api/node.py +++ b/apps/assets/api/node.py @@ -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) diff --git a/apps/assets/serializers/node.py b/apps/assets/serializers/node.py index fd04486fe..bb09e7580 100644 --- a/apps/assets/serializers/node.py +++ b/apps/assets/serializers/node.py @@ -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(