From e23b4b53577ae33d910e0e86f1ea82311615dfec Mon Sep 17 00:00:00 2001 From: jeremystretch Date: Wed, 12 Oct 2022 15:23:08 -0400 Subject: [PATCH] Move get_registry() out of SearchBackend --- netbox/netbox/search/__init__.py | 8 ++++++++ netbox/netbox/search/backends.py | 11 ++--------- netbox/netbox/views/__init__.py | 4 ++-- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/netbox/netbox/search/__init__.py b/netbox/netbox/search/__init__.py index ed47cf80b..8989c0c77 100644 --- a/netbox/netbox/search/__init__.py +++ b/netbox/netbox/search/__init__.py @@ -20,6 +20,14 @@ class LookupTypes: ENDSWITH = 'iendswith' +def get_registry(): + r = {} + for app_label, models in registry['search'].items(): + r.update(**models) + + return r + + class SearchIndex: """ Base class for building search indexes. diff --git a/netbox/netbox/search/backends.py b/netbox/netbox/search/backends.py index f50623cb5..2fd3e40b1 100644 --- a/netbox/netbox/search/backends.py +++ b/netbox/netbox/search/backends.py @@ -11,7 +11,7 @@ from django.db.models.signals import post_delete, post_save from extras.models import CachedValue from extras.registry import registry 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. _backends_cache = {} @@ -41,13 +41,6 @@ class SearchBackend: post_save.connect(self.caching_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): """Return the set of choices for individual object types, organized by category.""" if not self._search_choice_options: @@ -118,7 +111,7 @@ class FilterSetSearchBackend(SearchBackend): def search(self, request, value, lookup=DEFAULT_LOOKUP_TYPE): results = [] - search_registry = self.get_registry() + search_registry = get_registry() for obj_type in search_registry.keys(): queryset = getattr(search_registry[obj_type], 'queryset', None) diff --git a/netbox/netbox/views/__init__.py b/netbox/netbox/views/__init__.py index ebc3c28bb..e0948c3ce 100644 --- a/netbox/netbox/views/__init__.py +++ b/netbox/netbox/views/__init__.py @@ -21,8 +21,8 @@ from dcim.models import ( from extras.models import ObjectChange from extras.tables import ObjectChangeTable from ipam.models import Aggregate, IPAddress, IPRange, Prefix, VLAN, VRF -from netbox.constants import SEARCH_MAX_RESULTS from netbox.forms import SearchForm +from netbox.search import get_registry from netbox.search.backends import search_backend from tenancy.models import Tenant from virtualization.models import Cluster, VirtualMachine @@ -153,9 +153,9 @@ class SearchView(View): results = [] 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 form.cleaned_data['obj_type']: + search_registry = get_registry() object_type = form.cleaned_data['obj_type'] url = reverse(search_registry[object_type].url) return redirect(f"{url}?q={form.cleaned_data['q']}")