Unverified Commit 3986bbf7 authored by BaiJiangJie's avatar BaiJiangJie Committed by GitHub

Merge pull request #3323 from jumpserver/dev_nodes

[Update] 优化节点删除 API,返回删除失败原因(包含子节点或资产)
parents 4102dc68 7ff63918
......@@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from rest_framework import generics
from rest_framework import generics, status
from rest_framework.serializers import ValidationError
from rest_framework.views import APIView
from rest_framework.response import Response
......@@ -59,6 +59,13 @@ class NodeViewSet(OrgModelViewSet):
raise ValidationError({"error": msg})
return super().perform_update(serializer)
def destroy(self, request, *args, **kwargs):
node = self.get_object()
if node.has_children_or_contains_assets():
msg = _("Deletion failed and the node contains children or assets")
return Response(data={'msg': msg}, status=status.HTTP_403_FORBIDDEN)
return super().destroy(request, *args, **kwargs)
class NodeListAsTreeApi(generics.ListAPIView):
"""
......
......@@ -470,8 +470,13 @@ class Node(OrgModelMixin, SomeNodesMixin, TreeMixin, FamilyMixin, FullValueMixin
tree_node = TreeNode(**data)
return tree_node
def delete(self, using=None, keep_parents=False):
def has_children_or_contains_assets(self):
if self.children or self.get_assets():
return True
return False
def delete(self, using=None, keep_parents=False):
if self.has_children_or_contains_assets():
return
return super().delete(using=using, keep_parents=keep_parents)
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment