From a4667f33127d582ac5d2f128af070358edb974d1 Mon Sep 17 00:00:00 2001 From: xinwen Date: Mon, 23 Nov 2020 17:26:06 +0800 Subject: [PATCH] =?UTF-8?q?fix(Node):=20Node=20=E4=BF=9D=E5=AD=98=E7=9A=84?= =?UTF-8?q?=E6=97=B6=E5=80=99=EF=BC=8C=E5=9C=A8=E4=BF=A1=E5=8F=B7=E9=87=8C?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=20parent=5Fkey?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/assets/api/node.py | 4 +--- apps/assets/models/node.py | 2 +- apps/assets/signals_handler.py | 7 ++++++- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/apps/assets/api/node.py b/apps/assets/api/node.py index 273ac667f..924dcb788 100644 --- a/apps/assets/api/node.py +++ b/apps/assets/api/node.py @@ -201,10 +201,8 @@ class NodeAddChildrenApi(generics.UpdateAPIView): def put(self, request, *args, **kwargs): instance = self.get_object() nodes_id = request.data.get("nodes") - children = [get_object_or_none(Node, id=pk) for pk in nodes_id] + children = Node.objects.filter(id__in=nodes_id) for node in children: - if not node: - continue node.parent = instance return Response("OK") diff --git a/apps/assets/models/node.py b/apps/assets/models/node.py index e88afccdc..da8d07ebc 100644 --- a/apps/assets/models/node.py +++ b/apps/assets/models/node.py @@ -103,7 +103,7 @@ class FamilyMixin: if value is None: value = child_key child = self.__class__.objects.create( - id=_id, key=child_key, value=value, parent_key=self.key, + id=_id, key=child_key, value=value ) return child diff --git a/apps/assets/signals_handler.py b/apps/assets/signals_handler.py index 1c2813aa9..da7f6d397 100644 --- a/apps/assets/signals_handler.py +++ b/apps/assets/signals_handler.py @@ -4,7 +4,7 @@ from operator import add, sub from assets.utils import is_asset_exists_in_node from django.db.models.signals import ( - post_save, m2m_changed, pre_delete, post_delete + post_save, m2m_changed, pre_delete, post_delete, pre_save ) from django.db.models import Q, F from django.dispatch import receiver @@ -37,6 +37,11 @@ def test_asset_conn_on_created(asset): test_asset_connectivity_util.delay([asset]) +@receiver(pre_save, sender=Node) +def on_node_pre_save(sender, instance: Node, **kwargs): + instance.parent_key = instance.compute_parent_key() + + @receiver(post_save, sender=Asset) @on_transaction_commit def on_asset_created_or_update(sender, instance=None, created=False, **kwargs):