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