mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-24 16:26:09 -06:00
8927 refactor search
This commit is contained in:
parent
499fcee2a1
commit
c31d746494
@ -6,27 +6,10 @@ from search.backends import default_search_engine
|
|||||||
from .base import *
|
from .base import *
|
||||||
|
|
||||||
|
|
||||||
def build_search_choices():
|
def build_options(choices):
|
||||||
result = list()
|
options = [{"label": choices[0][1], "items": []}]
|
||||||
result.append(('', 'All Objects'))
|
|
||||||
for category, items in default_search_engine.get_registry().items():
|
|
||||||
subcategories = list()
|
|
||||||
for slug, obj in items.items():
|
|
||||||
name = obj.queryset.model._meta.verbose_name_plural
|
|
||||||
name = name[0].upper() + name[1:]
|
|
||||||
subcategories.append((slug, name))
|
|
||||||
result.append((category, tuple(subcategories)))
|
|
||||||
|
|
||||||
return tuple(result)
|
for label, choices in choices[1:]:
|
||||||
|
|
||||||
|
|
||||||
OBJ_TYPE_CHOICES = build_search_choices()
|
|
||||||
|
|
||||||
|
|
||||||
def build_options():
|
|
||||||
options = [{"label": OBJ_TYPE_CHOICES[0][1], "items": []}]
|
|
||||||
|
|
||||||
for label, choices in OBJ_TYPE_CHOICES[1:]:
|
|
||||||
items = []
|
items = []
|
||||||
|
|
||||||
for value, choice_label in choices:
|
for value, choice_label in choices:
|
||||||
@ -38,5 +21,14 @@ def build_options():
|
|||||||
|
|
||||||
class SearchForm(BootstrapMixin, forms.Form):
|
class SearchForm(BootstrapMixin, forms.Form):
|
||||||
q = forms.CharField(label='Search')
|
q = forms.CharField(label='Search')
|
||||||
obj_type = forms.ChoiceField(choices=OBJ_TYPE_CHOICES, required=False, label='Type')
|
options = None
|
||||||
options = build_options()
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
self.fields["obj_type"] = forms.ChoiceField(choices=default_search_engine.get_search_choices(), required=False, label='Type')
|
||||||
|
|
||||||
|
def get_options(self):
|
||||||
|
if not self.options:
|
||||||
|
self.options = build_options(default_search_engine.get_search_choices())
|
||||||
|
|
||||||
|
return self.options
|
||||||
|
@ -319,6 +319,7 @@ INSTALLED_APPS = [
|
|||||||
'social_django',
|
'social_django',
|
||||||
'taggit',
|
'taggit',
|
||||||
'timezone_field',
|
'timezone_field',
|
||||||
|
'search',
|
||||||
'circuits',
|
'circuits',
|
||||||
'dcim',
|
'dcim',
|
||||||
'ipam',
|
'ipam',
|
||||||
@ -330,7 +331,6 @@ INSTALLED_APPS = [
|
|||||||
'wireless',
|
'wireless',
|
||||||
'django_rq', # Must come after extras to allow overriding management commands
|
'django_rq', # Must come after extras to allow overriding management commands
|
||||||
'drf_yasg',
|
'drf_yasg',
|
||||||
'search',
|
|
||||||
]
|
]
|
||||||
|
|
||||||
# Middleware
|
# Middleware
|
||||||
|
@ -20,6 +20,7 @@ class SearchBackend(object):
|
|||||||
"""A search engine capable of performing multi-table searches."""
|
"""A search engine capable of performing multi-table searches."""
|
||||||
|
|
||||||
_created_engines: dict = dict()
|
_created_engines: dict = dict()
|
||||||
|
_search_choices = tuple()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_created_engines(cls):
|
def get_created_engines(cls):
|
||||||
@ -66,6 +67,24 @@ class SearchBackend(object):
|
|||||||
|
|
||||||
# Signalling hooks.
|
# Signalling hooks.
|
||||||
|
|
||||||
|
def get_search_choices(self):
|
||||||
|
if self._search_choices:
|
||||||
|
return self._search_choices
|
||||||
|
|
||||||
|
result = list()
|
||||||
|
result.append(('', 'All Objects'))
|
||||||
|
for category, items in self.get_registry().items():
|
||||||
|
subcategories = list()
|
||||||
|
for slug, obj in items.items():
|
||||||
|
name = obj.queryset.model._meta.verbose_name_plural
|
||||||
|
name = name[0].upper() + name[1:]
|
||||||
|
subcategories.append((slug, name))
|
||||||
|
result.append((category, tuple(subcategories)))
|
||||||
|
|
||||||
|
self._search_choices = tuple(result)
|
||||||
|
print(self._search_choices)
|
||||||
|
return self._search_choices
|
||||||
|
|
||||||
def _use_hooks(self):
|
def _use_hooks(self):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
@ -4,13 +4,18 @@ from django import template
|
|||||||
|
|
||||||
register = template.Library()
|
register = template.Library()
|
||||||
|
|
||||||
search_form = SearchForm()
|
search_form = None
|
||||||
|
|
||||||
|
|
||||||
@register.inclusion_tag("search/searchbar.html")
|
@register.inclusion_tag("search/searchbar.html")
|
||||||
def search_options(request) -> Dict:
|
def search_options(request) -> Dict:
|
||||||
|
global search_form
|
||||||
|
|
||||||
|
if not search_form:
|
||||||
|
search_form = SearchForm()
|
||||||
|
|
||||||
"""Provide search options to template."""
|
"""Provide search options to template."""
|
||||||
return {
|
return {
|
||||||
'options': search_form.options,
|
'options': search_form.get_options(),
|
||||||
'request': request,
|
'request': request,
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user