2022-12-15 08:02:34 +00:00
|
|
|
|
# ~*~ coding: utf-8 ~*~
|
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
|
from django.db.models import Q
|
2023-09-13 09:05:01 +00:00
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
2022-12-15 08:02:34 +00:00
|
|
|
|
from rest_framework.generics import get_object_or_404
|
|
|
|
|
from rest_framework.response import Response
|
|
|
|
|
|
|
|
|
|
from assets.locks import NodeAddChildrenLock
|
2023-09-13 09:05:01 +00:00
|
|
|
|
from common.exceptions import JMSException
|
2022-12-15 08:02:34 +00:00
|
|
|
|
from common.tree import TreeNodeSerializer
|
|
|
|
|
from common.utils import get_logger
|
|
|
|
|
from orgs.mixins import generics
|
|
|
|
|
from orgs.utils import current_org
|
|
|
|
|
from .mixin import SerializeToTreeNodeMixin
|
|
|
|
|
from .. import serializers
|
|
|
|
|
from ..const import AllTypes
|
2022-12-22 03:34:18 +00:00
|
|
|
|
from ..models import Node, Platform, Asset
|
2022-12-15 08:02:34 +00:00
|
|
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
__all__ = [
|
|
|
|
|
'NodeChildrenApi',
|
|
|
|
|
'NodeChildrenAsTreeApi',
|
|
|
|
|
'CategoryTreeApi',
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
2022-12-22 03:34:18 +00:00
|
|
|
|
class NodeChildrenApi(generics.ListCreateAPIView):
|
2022-12-15 08:02:34 +00:00
|
|
|
|
"""
|
2022-12-22 03:34:18 +00:00
|
|
|
|
节点的增删改查
|
2022-12-15 08:02:34 +00:00
|
|
|
|
"""
|
|
|
|
|
serializer_class = serializers.NodeSerializer
|
|
|
|
|
search_fields = ('value',)
|
|
|
|
|
|
|
|
|
|
instance = None
|
|
|
|
|
is_initial = False
|
|
|
|
|
|
|
|
|
|
def initial(self, request, *args, **kwargs):
|
2023-09-13 09:05:01 +00:00
|
|
|
|
super().initial(request, *args, **kwargs)
|
2022-12-15 08:02:34 +00:00
|
|
|
|
self.instance = self.get_object()
|
|
|
|
|
|
|
|
|
|
def perform_create(self, serializer):
|
|
|
|
|
with NodeAddChildrenLock(self.instance):
|
|
|
|
|
data = serializer.validated_data
|
|
|
|
|
_id = data.get("id")
|
|
|
|
|
value = data.get("value")
|
2023-06-01 08:22:17 +00:00
|
|
|
|
if value:
|
|
|
|
|
children = self.instance.get_children()
|
|
|
|
|
if children.filter(value=value).exists():
|
|
|
|
|
raise JMSException(_('The same level node name cannot be the same'))
|
|
|
|
|
else:
|
2022-12-15 08:02:34 +00:00
|
|
|
|
value = self.instance.get_next_child_preset_name()
|
|
|
|
|
node = self.instance.create_child(value=value, _id=_id)
|
|
|
|
|
# 避免查询 full value
|
|
|
|
|
node._full_value = node.value
|
|
|
|
|
serializer.instance = node
|
|
|
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
|
pk = self.kwargs.get('pk') or self.request.query_params.get('id')
|
|
|
|
|
key = self.request.query_params.get("key")
|
|
|
|
|
|
|
|
|
|
if not pk and not key:
|
|
|
|
|
self.is_initial = True
|
|
|
|
|
if current_org.is_root():
|
|
|
|
|
node = None
|
|
|
|
|
else:
|
|
|
|
|
node = Node.org_root()
|
|
|
|
|
return node
|
|
|
|
|
if pk:
|
|
|
|
|
node = get_object_or_404(Node, pk=pk)
|
|
|
|
|
else:
|
|
|
|
|
node = get_object_or_404(Node, key=key)
|
|
|
|
|
return node
|
|
|
|
|
|
|
|
|
|
def get_org_root_queryset(self, query_all):
|
|
|
|
|
if query_all:
|
|
|
|
|
return Node.objects.all()
|
|
|
|
|
else:
|
|
|
|
|
return Node.org_root_nodes()
|
|
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
|
query_all = self.request.query_params.get("all", "0") == "all"
|
|
|
|
|
|
|
|
|
|
if self.is_initial and current_org.is_root():
|
|
|
|
|
return self.get_org_root_queryset(query_all)
|
|
|
|
|
|
|
|
|
|
if self.is_initial:
|
|
|
|
|
with_self = True
|
|
|
|
|
else:
|
|
|
|
|
with_self = False
|
|
|
|
|
|
|
|
|
|
if not self.instance:
|
|
|
|
|
return Node.objects.none()
|
|
|
|
|
|
|
|
|
|
if query_all:
|
|
|
|
|
queryset = self.instance.get_all_children(with_self=with_self)
|
|
|
|
|
else:
|
|
|
|
|
queryset = self.instance.get_children(with_self=with_self)
|
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NodeChildrenAsTreeApi(SerializeToTreeNodeMixin, NodeChildrenApi):
|
|
|
|
|
"""
|
|
|
|
|
节点子节点作为树返回,
|
|
|
|
|
[
|
|
|
|
|
{
|
|
|
|
|
"id": "",
|
|
|
|
|
"name": "",
|
|
|
|
|
"pId": "",
|
|
|
|
|
"meta": ""
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
model = Node
|
|
|
|
|
|
|
|
|
|
def filter_queryset(self, queryset):
|
2023-01-16 11:02:09 +00:00
|
|
|
|
""" queryset is Node queryset """
|
2022-12-15 08:02:34 +00:00
|
|
|
|
if not self.request.GET.get('search'):
|
|
|
|
|
return queryset
|
|
|
|
|
queryset = super().filter_queryset(queryset)
|
|
|
|
|
queryset = self.model.get_ancestor_queryset(queryset)
|
|
|
|
|
return queryset
|
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
|
def get_queryset_for_assets(self):
|
|
|
|
|
query_all = self.request.query_params.get("all", "0") == "all"
|
|
|
|
|
include_assets = self.request.query_params.get('assets', '0') == '1'
|
|
|
|
|
if not self.instance or not include_assets:
|
2023-02-02 02:41:03 +00:00
|
|
|
|
return Asset.objects.none()
|
2024-01-02 08:11:56 +00:00
|
|
|
|
if self.instance.is_org_root():
|
|
|
|
|
return Asset.objects.none()
|
2023-01-16 11:02:09 +00:00
|
|
|
|
if query_all:
|
2023-06-27 02:24:47 +00:00
|
|
|
|
assets = self.instance.get_all_assets()
|
2023-01-16 11:02:09 +00:00
|
|
|
|
else:
|
2023-06-27 02:24:47 +00:00
|
|
|
|
assets = self.instance.get_assets()
|
|
|
|
|
return assets.only(
|
|
|
|
|
"id", "name", "address", "platform_id",
|
|
|
|
|
"org_id", "is_active", 'comment'
|
|
|
|
|
).prefetch_related('platform')
|
2023-01-16 11:02:09 +00:00
|
|
|
|
|
|
|
|
|
def filter_queryset_for_assets(self, assets):
|
|
|
|
|
search = self.request.query_params.get('search')
|
|
|
|
|
if search:
|
|
|
|
|
q = Q(name__icontains=search) | Q(address__icontains=search)
|
|
|
|
|
assets = assets.filter(q)
|
|
|
|
|
return assets
|
|
|
|
|
|
2022-12-15 08:02:34 +00:00
|
|
|
|
def list(self, request, *args, **kwargs):
|
|
|
|
|
nodes = self.filter_queryset(self.get_queryset()).order_by('value')
|
|
|
|
|
nodes = self.serialize_nodes(nodes, with_asset_amount=True)
|
2023-01-16 11:02:09 +00:00
|
|
|
|
assets = self.filter_queryset_for_assets(self.get_queryset_for_assets())
|
2023-01-17 05:40:51 +00:00
|
|
|
|
node_key = self.instance.key if self.instance else None
|
|
|
|
|
assets = self.serialize_assets(assets, node_key=node_key)
|
2022-12-15 08:02:34 +00:00
|
|
|
|
data = [*nodes, *assets]
|
|
|
|
|
return Response(data=data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CategoryTreeApi(SerializeToTreeNodeMixin, generics.ListAPIView):
|
|
|
|
|
serializer_class = TreeNodeSerializer
|
2022-12-22 03:34:18 +00:00
|
|
|
|
rbac_perms = {
|
|
|
|
|
'GET': 'assets.view_asset',
|
|
|
|
|
'list': 'assets.view_asset',
|
|
|
|
|
}
|
2022-12-15 08:02:34 +00:00
|
|
|
|
|
2022-12-22 03:34:18 +00:00
|
|
|
|
def get_assets(self):
|
|
|
|
|
key = self.request.query_params.get('key')
|
|
|
|
|
platform = Platform.objects.filter(id=key).first()
|
|
|
|
|
if not platform:
|
|
|
|
|
return []
|
|
|
|
|
assets = Asset.objects.filter(platform=platform).prefetch_related('platform')
|
|
|
|
|
return self.serialize_assets(assets, key)
|
2022-12-15 08:02:34 +00:00
|
|
|
|
|
|
|
|
|
def list(self, request, *args, **kwargs):
|
2022-12-27 05:20:18 +00:00
|
|
|
|
include_asset = self.request.query_params.get('assets', '0') == '1'
|
2023-01-16 11:02:09 +00:00
|
|
|
|
# 资源数量统计可选项 (asset, account)
|
|
|
|
|
count_resource = self.request.query_params.get('count_resource', 'asset')
|
2022-12-22 03:34:18 +00:00
|
|
|
|
|
2023-05-09 11:46:34 +00:00
|
|
|
|
if not self.request.query_params.get('key'):
|
|
|
|
|
nodes = AllTypes.to_tree_nodes(include_asset, count_resource=count_resource)
|
|
|
|
|
elif include_asset:
|
2022-12-22 03:34:18 +00:00
|
|
|
|
nodes = self.get_assets()
|
2022-12-15 08:02:34 +00:00
|
|
|
|
else:
|
2023-05-09 11:46:34 +00:00
|
|
|
|
nodes = []
|
2022-12-22 03:34:18 +00:00
|
|
|
|
return Response(data=nodes)
|