Remove legacy search backend

This commit is contained in:
jeremystretch 2022-10-18 10:21:32 -04:00
parent 20e7ca204e
commit bc0fe05ec9
3 changed files with 7 additions and 83 deletions

View File

@ -1,5 +1,2 @@
# Prefix for nested serializers # Prefix for nested serializers
NESTED_SERIALIZER_PREFIX = 'Nested' NESTED_SERIALIZER_PREFIX = 'Nested'
# Max results per object type
SEARCH_MAX_RESULTS = 15

View File

@ -20,15 +20,6 @@ class LookupTypes:
ENDSWITH = 'iendswith' ENDSWITH = 'iendswith'
def get_registry():
r = {}
for app_label, models in registry['search'].items():
for model_name, idx in models.items():
r[f'{app_label}.{model_name}'] = idx
return r
class SearchIndex: class SearchIndex:
""" """
Base class for building search indexes. Base class for building search indexes.
@ -88,14 +79,14 @@ class SearchIndex:
return values return values
class SearchResult: def get_indexer(model):
""" """
Represents a single result returned by a search backend's search() method. Get the search indexer class for the given model.
""" """
def __init__(self, obj, field=None, value=None): app_label = model._meta.app_label
self.object = obj model_name = model._meta.model_name
self.field = field
self.value = value return registry['search'][app_label][model_name]
def register_search(): def register_search():

View File

@ -10,29 +10,13 @@ from django.db.models.signals import post_delete, post_save
from extras.models import CachedValue from extras.models import CachedValue
from extras.registry import registry from extras.registry import registry
from netbox.constants import SEARCH_MAX_RESULTS
from utilities.querysets import RestrictedPrefetch from utilities.querysets import RestrictedPrefetch
from utilities.templatetags.builtins.filters import bettertitle from utilities.templatetags.builtins.filters import bettertitle
from . import FieldTypes, LookupTypes, SearchResult, get_registry from . import FieldTypes, LookupTypes, get_indexer
# The cache for the initialized backend.
_backends_cache = {}
DEFAULT_LOOKUP_TYPE = LookupTypes.PARTIAL DEFAULT_LOOKUP_TYPE = LookupTypes.PARTIAL
def get_indexer(model):
app_label = model._meta.app_label
model_name = model._meta.model_name
return registry['search'][app_label][model_name]
class SearchEngineError(Exception):
"""Something went wrong with a search engine."""
pass
class SearchBackend: class SearchBackend:
"""A search engine capable of performing multi-table searches.""" """A search engine capable of performing multi-table searches."""
_search_choice_options = tuple() _search_choice_options = tuple()
@ -111,54 +95,6 @@ class SearchBackend:
raise NotImplementedError raise NotImplementedError
class FilterSetSearchBackend(SearchBackend):
"""
Legacy search backend. Performs a discrete database query for each registered object type, using the FilterSet
class specified by the index for each.
"""
def search(self, request, value, object_types=None, lookup=DEFAULT_LOOKUP_TYPE):
results = []
search_registry = get_registry()
if object_types is not None:
keys = [f'{ct.app_label}.{ct.name}' for ct in object_types]
else:
keys = search_registry.keys()
for obj_type in keys:
queryset = getattr(search_registry[obj_type], 'queryset', None)
if not queryset:
continue
# Restrict the queryset for the current user
if hasattr(queryset, 'restrict'):
queryset = queryset.restrict(request.user, 'view')
filterset = getattr(search_registry[obj_type], 'filterset', None)
if not filterset:
# This backend requires a FilterSet class for the model
continue
queryset = filterset({'q': value}, queryset=queryset).qs[:SEARCH_MAX_RESULTS]
results.extend([
SearchResult(obj) for obj in queryset
])
return results
@classmethod
def cache(cls, instance, data):
# This backend does not utilize a cache
pass
@classmethod
def remove(cls, instance):
# This backend does not utilize a cache
pass
class CachedValueSearchBackend(SearchBackend): class CachedValueSearchBackend(SearchBackend):
def search(self, request, value, object_types=None, lookup=None): def search(self, request, value, object_types=None, lookup=None):