fix(xpack): GatheredUser 点击资产树报错

pull/4739/head
xinwen 2020-09-29 15:35:39 +08:00 committed by 老广
parent 1915224063
commit d52ed2ffb9
3 changed files with 47 additions and 51 deletions

View File

@ -3,6 +3,7 @@ from typing import List
from assets.models import Node, Asset from assets.models import Node, Asset
from assets.pagination import AssetLimitOffsetPagination from assets.pagination import AssetLimitOffsetPagination
from common.utils import lazyproperty, dict_get_any, is_uuid, get_object_or_none from common.utils import lazyproperty, dict_get_any, is_uuid, get_object_or_none
from assets.utils import get_node, is_query_node_all_assets
class SerializeToTreeNodeMixin: class SerializeToTreeNodeMixin:
@ -80,21 +81,8 @@ class FilterAssetByNodeMixin:
@lazyproperty @lazyproperty
def is_query_node_all_assets(self): def is_query_node_all_assets(self):
request = self.request return is_query_node_all_assets(self.request)
query_all_arg = request.query_params.get('all')
show_current_asset_arg = request.query_params.get('show_current_asset')
if show_current_asset_arg is not None:
return show_current_asset_arg != '1'
return query_all_arg != '0'
@lazyproperty @lazyproperty
def node(self): def node(self):
node_id = dict_get_any(self.request.query_params, ['node', 'node_id']) return get_node(self.request)
if not node_id:
return None
if is_uuid(node_id):
node = get_object_or_none(Node, id=node_id)
else:
node = get_object_or_none(Node, key=node_id)
return node

View File

@ -5,9 +5,8 @@ from rest_framework.compat import coreapi, coreschema
from rest_framework import filters from rest_framework import filters
from django.db.models import Q from django.db.models import Q
from common.utils import dict_get_any, is_uuid, get_object_or_none
from .models import Label from .models import Label
from assets.models import Node from assets.utils import is_query_node_all_assets, get_node
class AssetByNodeFilterBackend(filters.BaseFilterBackend): class AssetByNodeFilterBackend(filters.BaseFilterBackend):
@ -22,43 +21,25 @@ class AssetByNodeFilterBackend(filters.BaseFilterBackend):
for field in self.fields for field in self.fields
] ]
@staticmethod def filter_node_related_all(self, queryset, node):
def is_query_all(request): return queryset.filter(
query_all_arg = request.query_params.get('all') Q(nodes__key__istartswith=f'{node.key}:') |
show_current_asset_arg = request.query_params.get('show_current_asset') Q(nodes__key=node.key)
).distinct()
query_all = query_all_arg == '1' def filter_node_related_direct(self, queryset, node):
if show_current_asset_arg is not None: return queryset.filter(nodes__key=node.key).distinct()
query_all = show_current_asset_arg != '1'
return query_all
@staticmethod
def get_query_node(request):
node_id = dict_get_any(request.query_params, ['node', 'node_id'])
if not node_id:
return None, False
if is_uuid(node_id):
node = get_object_or_none(Node, id=node_id)
else:
node = get_object_or_none(Node, key=node_id)
return node, True
def filter_queryset(self, request, queryset, view): def filter_queryset(self, request, queryset, view):
node, has_query_arg = self.get_query_node(request) node = get_node(request)
if not has_query_arg:
return queryset
if node is None: if node is None:
return queryset return queryset
query_all = self.is_query_all(request)
query_all = is_query_node_all_assets(request)
if query_all: if query_all:
return queryset.filter( return self.filter_node_related_all(queryset, node)
Q(nodes__key__istartswith=f'{node.key}:') |
Q(nodes__key=node.key)
).distinct()
else: else:
return queryset.filter(nodes__key=node.key).distinct() return self.filter_node_related_direct(queryset, node)
class FilterAssetByNodeFilterBackend(filters.BaseFilterBackend): class FilterAssetByNodeFilterBackend(filters.BaseFilterBackend):
@ -139,9 +120,14 @@ class LabelFilterBackend(filters.BaseFilterBackend):
class AssetRelatedByNodeFilterBackend(AssetByNodeFilterBackend): class AssetRelatedByNodeFilterBackend(AssetByNodeFilterBackend):
@staticmethod def filter_node_related_all(self, queryset, node):
def perform_query(pattern, queryset): return queryset.filter(
return queryset.filter(asset__nodes__key__regex=pattern).distinct() Q(asset__nodes__key__istartswith=f'{node.key}:') |
Q(asset__nodes__key=node.key)
).distinct()
def filter_node_related_direct(self, queryset, node):
return queryset.filter(asset__nodes__key=node.key).distinct()
class IpInFilterBackend(filters.BaseFilterBackend): class IpInFilterBackend(filters.BaseFilterBackend):

View File

@ -2,7 +2,8 @@
# #
from django.db.models import Q from django.db.models import Q
from common.utils import get_logger from common.utils import get_logger, dict_get_any, is_uuid, get_object_or_none
from common.http import is_true
from .models import Asset, Node from .models import Asset, Node
@ -28,3 +29,24 @@ def is_asset_exists_in_node(asset_pk, node_key):
).filter( ).filter(
Q(nodes__key__istartswith=f'{node_key}:') | Q(nodes__key=node_key) Q(nodes__key__istartswith=f'{node_key}:') | Q(nodes__key=node_key)
).exists() ).exists()
def is_query_node_all_assets(request):
request = request
query_all_arg = request.query_params.get('all')
show_current_asset_arg = request.query_params.get('show_current_asset')
if show_current_asset_arg is not None:
return not is_true(show_current_asset_arg)
return is_true(query_all_arg)
def get_node(request):
node_id = dict_get_any(request.query_params, ['node', 'node_id'])
if not node_id:
return None
if is_uuid(node_id):
node = get_object_or_none(Node, id=node_id)
else:
node = get_object_or_none(Node, key=node_id)
return node