From 7c694c6885a2acf9c55d72bbdc0c94c2beb91724 Mon Sep 17 00:00:00 2001 From: BaiJiangJie Date: Tue, 29 May 2018 19:19:35 +0800 Subject: [PATCH] =?UTF-8?q?[Update]=20=E5=90=88=E5=B9=B6Node=E7=9A=84paren?= =?UTF-8?q?t.setter=E6=96=B9=E6=B3=95=EF=BC=8C=E4=BF=AE=E6=94=B9node=5Ffak?= =?UTF-8?q?e=E7=9A=84parent=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/assets/api/node.py | 60 ++++++++++++++++++-------------------- apps/assets/models/node.py | 22 +++++++------- 2 files changed, 40 insertions(+), 42 deletions(-) diff --git a/apps/assets/api/node.py b/apps/assets/api/node.py index 11f45cc8d..bb8cc719a 100644 --- a/apps/assets/api/node.py +++ b/apps/assets/api/node.py @@ -31,7 +31,7 @@ from .. import serializers logger = get_logger(__file__) __all__ = [ 'NodeViewSet', 'NodeChildrenApi', - 'NodeAssetsApi', 'NodeWithAssetsApi', + 'NodeAssetsApi', 'NodeAddAssetsApi', 'NodeRemoveAssetsApi', 'NodeReplaceAssetsApi', 'NodeAddChildrenApi', 'RefreshNodeHardwareInfoApi', @@ -50,32 +50,32 @@ class NodeViewSet(BulkModelViewSet): serializer.save() -class NodeWithAssetsApi(generics.ListAPIView): - permission_classes = (IsSuperUser,) - serializers = serializers.NodeSerializer - - def get_node(self): - pk = self.kwargs.get('pk') or self.request.query_params.get('node') - if not pk: - node = Node.root() - else: - node = get_object_or_404(Node, pk) - return node - - def get_queryset(self): - queryset = [] - node = self.get_node() - children = node.get_children() - assets = node.get_assets() - queryset.extend(list(children)) - - for asset in assets: - node = Node() - node.id = asset.id - node.parent = node.id - node.value = asset.hostname - queryset.append(node) - return queryset +# class NodeWithAssetsApi(generics.ListAPIView): +# permission_classes = (IsSuperUser,) +# serializers = serializers.NodeSerializer +# +# def get_node(self): +# pk = self.kwargs.get('pk') or self.request.query_params.get('node') +# if not pk: +# node = Node.root() +# else: +# node = get_object_or_404(Node, pk) +# return node +# +# def get_queryset(self): +# queryset = [] +# node = self.get_node() +# children = node.get_children() +# assets = node.get_assets() +# queryset.extend(list(children)) +# +# for asset in assets: +# node = Node() +# node.id = asset.id +# node.parent = node.id +# node.value = asset.hostname +# queryset.append(node) +# return queryset class NodeChildrenApi(mixins.ListModelMixin, generics.CreateAPIView): @@ -139,9 +139,9 @@ class NodeChildrenApi(mixins.ListModelMixin, generics.CreateAPIView): for asset in assets: node_fake = Node() node_fake.id = asset.id + node_fake.is_node = False node_fake.parent = node node_fake.value = asset.hostname - node_fake.is_node = False queryset.append(node_fake) queryset = sorted(queryset, key=lambda x: x.is_node, reverse=True) return queryset @@ -177,9 +177,7 @@ class NodeAddChildrenApi(generics.UpdateAPIView): for node in children: if not node: continue - # node.parent = instance - # node.save() - node.set_parent(instance) + node.parent = instance return Response("OK") diff --git a/apps/assets/models/node.py b/apps/assets/models/node.py index 784c8ae1b..7c2fe7dcc 100644 --- a/apps/assets/models/node.py +++ b/apps/assets/models/node.py @@ -40,16 +40,6 @@ class Node(models.Model): def level(self): return len(self.key.split(':')) - def set_parent(self, instance): - children = self.get_all_children() - old_key = self.key - with transaction.atomic(): - self.parent = instance - for child in children: - child.key = child.key.replace(old_key, self.key, 1) - child.save() - self.save() - def get_next_child_key(self): mark = self.child_mark self.child_mark += 1 @@ -128,7 +118,17 @@ class Node(models.Model): @parent.setter def parent(self, parent): - self.key = parent.get_next_child_key() + if self.is_node: + children = self.get_all_children() + old_key = self.key + with transaction.atomic(): + self.key = parent.get_next_child_key() + for child in children: + child.key = child.key.replace(old_key, self.key, 1) + child.save() + self.save() + else: + self.key = parent.key+':fake' @property def ancestor(self):