2019-09-18 14:06:46 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
from django.db.models import Q
|
2022-09-19 09:00:03 +00:00
|
|
|
from rest_framework import filters
|
|
|
|
from rest_framework.compat import coreapi, coreschema
|
2019-09-18 14:06:46 +00:00
|
|
|
|
2022-11-11 07:04:31 +00:00
|
|
|
from assets.utils import get_node_from_request, is_query_node_all_assets
|
2019-09-18 14:06:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AssetByNodeFilterBackend(filters.BaseFilterBackend):
|
|
|
|
fields = ['node', 'all']
|
|
|
|
|
|
|
|
def get_schema_fields(self, view):
|
|
|
|
return [
|
|
|
|
coreapi.Field(
|
|
|
|
name=field, location='query', required=False,
|
2019-09-19 06:48:25 +00:00
|
|
|
type='string', example='', description='', schema=None,
|
2019-09-18 14:06:46 +00:00
|
|
|
)
|
|
|
|
for field in self.fields
|
|
|
|
]
|
|
|
|
|
2020-09-29 07:35:39 +00:00
|
|
|
def filter_node_related_all(self, queryset, node):
|
|
|
|
return queryset.filter(
|
|
|
|
Q(nodes__key__istartswith=f'{node.key}:') |
|
|
|
|
Q(nodes__key=node.key)
|
|
|
|
).distinct()
|
2019-09-18 14:06:46 +00:00
|
|
|
|
2020-09-29 07:35:39 +00:00
|
|
|
def filter_node_related_direct(self, queryset, node):
|
|
|
|
return queryset.filter(nodes__key=node.key).distinct()
|
2019-09-18 14:06:46 +00:00
|
|
|
|
2019-09-19 06:48:25 +00:00
|
|
|
def filter_queryset(self, request, queryset, view):
|
2022-08-22 10:32:33 +00:00
|
|
|
node = get_node_from_request(request)
|
2019-09-19 06:48:25 +00:00
|
|
|
if node is None:
|
2019-10-18 07:12:17 +00:00
|
|
|
return queryset
|
2020-09-29 07:35:39 +00:00
|
|
|
|
|
|
|
query_all = is_query_node_all_assets(request)
|
2019-09-18 14:06:46 +00:00
|
|
|
if query_all:
|
2020-09-29 07:35:39 +00:00
|
|
|
return self.filter_node_related_all(queryset, node)
|
2020-08-16 15:08:58 +00:00
|
|
|
else:
|
2020-09-29 07:35:39 +00:00
|
|
|
return self.filter_node_related_direct(queryset, node)
|
2020-08-16 15:08:58 +00:00
|
|
|
|
|
|
|
|
2022-08-22 10:32:33 +00:00
|
|
|
class NodeFilterBackend(filters.BaseFilterBackend):
|
2020-08-16 15:08:58 +00:00
|
|
|
"""
|
2022-08-22 10:32:33 +00:00
|
|
|
需要与 `assets.api.mixin.NodeFilterMixin` 配合使用
|
2020-08-16 15:08:58 +00:00
|
|
|
"""
|
|
|
|
fields = ['node', 'all']
|
|
|
|
|
|
|
|
def get_schema_fields(self, view):
|
|
|
|
return [
|
|
|
|
coreapi.Field(
|
|
|
|
name=field, location='query', required=False,
|
|
|
|
type='string', example='', description='', schema=None,
|
|
|
|
)
|
|
|
|
for field in self.fields
|
|
|
|
]
|
|
|
|
|
|
|
|
def filter_queryset(self, request, queryset, view):
|
2022-08-22 10:32:33 +00:00
|
|
|
node = get_node_from_request(request)
|
2020-08-16 15:08:58 +00:00
|
|
|
if node is None:
|
|
|
|
return queryset
|
2022-08-22 10:32:33 +00:00
|
|
|
|
|
|
|
query_all = is_query_node_all_assets(request)
|
2020-08-16 15:08:58 +00:00
|
|
|
if query_all:
|
|
|
|
return queryset.filter(
|
|
|
|
Q(nodes__key__istartswith=f'{node.key}:') |
|
|
|
|
Q(nodes__key=node.key)
|
|
|
|
).distinct()
|
2019-09-18 14:06:46 +00:00
|
|
|
else:
|
2022-04-28 04:47:39 +00:00
|
|
|
print("Query query origin: ", queryset.count())
|
2020-08-16 15:08:58 +00:00
|
|
|
return queryset.filter(nodes__key=node.key).distinct()
|
2019-09-18 14:06:46 +00:00
|
|
|
|
|
|
|
|
2020-07-06 03:14:20 +00:00
|
|
|
class IpInFilterBackend(filters.BaseFilterBackend):
|
|
|
|
def filter_queryset(self, request, queryset, view):
|
|
|
|
ips = request.query_params.get('ips')
|
|
|
|
if not ips:
|
|
|
|
return queryset
|
|
|
|
ip_list = [i.strip() for i in ips.split(',')]
|
2022-10-31 11:27:45 +00:00
|
|
|
queryset = queryset.filter(address__in=ip_list)
|
2020-07-06 03:14:20 +00:00
|
|
|
return queryset
|
|
|
|
|
|
|
|
def get_schema_fields(self, view):
|
|
|
|
return [
|
|
|
|
coreapi.Field(
|
|
|
|
name='ips', location='query', required=False, type='string',
|
|
|
|
schema=coreschema.String(
|
|
|
|
title='ips',
|
2022-09-21 03:17:14 +00:00
|
|
|
description='address in filter'
|
2020-07-06 03:14:20 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
]
|