Move get_registry() out of SearchBackend

This commit is contained in:
jeremystretch 2022-10-12 15:23:08 -04:00
parent 7f86cffff6
commit e23b4b5357
3 changed files with 12 additions and 11 deletions

View File

@ -20,6 +20,14 @@ class LookupTypes:
ENDSWITH = 'iendswith' ENDSWITH = 'iendswith'
def get_registry():
r = {}
for app_label, models in registry['search'].items():
r.update(**models)
return r
class SearchIndex: class SearchIndex:
""" """
Base class for building search indexes. Base class for building search indexes.

View File

@ -11,7 +11,7 @@ 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 netbox.constants import SEARCH_MAX_RESULTS
from . import FieldTypes, LookupTypes, SearchResult from . import FieldTypes, LookupTypes, SearchResult, get_registry
# The cache for the initialized backend. # The cache for the initialized backend.
_backends_cache = {} _backends_cache = {}
@ -41,13 +41,6 @@ class SearchBackend:
post_save.connect(self.caching_handler) post_save.connect(self.caching_handler)
post_delete.connect(self.removal_handler) post_delete.connect(self.removal_handler)
def get_registry(self):
r = {}
for app_label, models in registry['search'].items():
r.update(**models)
return r
def get_search_choices(self): def get_search_choices(self):
"""Return the set of choices for individual object types, organized by category.""" """Return the set of choices for individual object types, organized by category."""
if not self._search_choice_options: if not self._search_choice_options:
@ -118,7 +111,7 @@ class FilterSetSearchBackend(SearchBackend):
def search(self, request, value, lookup=DEFAULT_LOOKUP_TYPE): def search(self, request, value, lookup=DEFAULT_LOOKUP_TYPE):
results = [] results = []
search_registry = self.get_registry() search_registry = get_registry()
for obj_type in search_registry.keys(): for obj_type in search_registry.keys():
queryset = getattr(search_registry[obj_type], 'queryset', None) queryset = getattr(search_registry[obj_type], 'queryset', None)

View File

@ -21,8 +21,8 @@ from dcim.models import (
from extras.models import ObjectChange from extras.models import ObjectChange
from extras.tables import ObjectChangeTable from extras.tables import ObjectChangeTable
from ipam.models import Aggregate, IPAddress, IPRange, Prefix, VLAN, VRF from ipam.models import Aggregate, IPAddress, IPRange, Prefix, VLAN, VRF
from netbox.constants import SEARCH_MAX_RESULTS
from netbox.forms import SearchForm from netbox.forms import SearchForm
from netbox.search import get_registry
from netbox.search.backends import search_backend from netbox.search.backends import search_backend
from tenancy.models import Tenant from tenancy.models import Tenant
from virtualization.models import Cluster, VirtualMachine from virtualization.models import Cluster, VirtualMachine
@ -153,9 +153,9 @@ class SearchView(View):
results = [] results = []
if form.is_valid(): if form.is_valid():
search_registry = search_backend.get_registry()
# If an object type has been specified, redirect to the dedicated view for it # If an object type has been specified, redirect to the dedicated view for it
if form.cleaned_data['obj_type']: if form.cleaned_data['obj_type']:
search_registry = get_registry()
object_type = form.cleaned_data['obj_type'] object_type = form.cleaned_data['obj_type']
url = reverse(search_registry[object_type].url) url = reverse(search_registry[object_type].url)
return redirect(f"{url}?q={form.cleaned_data['q']}") return redirect(f"{url}?q={form.cleaned_data['q']}")