8927 refactor search

This commit is contained in:
Arthur 2022-09-27 10:33:34 -07:00 committed by jeremystretch
parent 499fcee2a1
commit c31d746494
4 changed files with 41 additions and 25 deletions

View File

@ -6,27 +6,10 @@ from search.backends import default_search_engine
from .base import *
def build_search_choices():
result = list()
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)))
def build_options(choices):
options = [{"label": choices[0][1], "items": []}]
return tuple(result)
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:]:
for label, choices in choices[1:]:
items = []
for value, choice_label in choices:
@ -38,5 +21,14 @@ def build_options():
class SearchForm(BootstrapMixin, forms.Form):
q = forms.CharField(label='Search')
obj_type = forms.ChoiceField(choices=OBJ_TYPE_CHOICES, required=False, label='Type')
options = build_options()
options = None
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

View File

@ -319,6 +319,7 @@ INSTALLED_APPS = [
'social_django',
'taggit',
'timezone_field',
'search',
'circuits',
'dcim',
'ipam',
@ -330,7 +331,6 @@ INSTALLED_APPS = [
'wireless',
'django_rq', # Must come after extras to allow overriding management commands
'drf_yasg',
'search',
]
# Middleware

View File

@ -20,6 +20,7 @@ class SearchBackend(object):
"""A search engine capable of performing multi-table searches."""
_created_engines: dict = dict()
_search_choices = tuple()
@classmethod
def get_created_engines(cls):
@ -66,6 +67,24 @@ class SearchBackend(object):
# 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):
raise NotImplementedError

View File

@ -4,13 +4,18 @@ from django import template
register = template.Library()
search_form = SearchForm()
search_form = None
@register.inclusion_tag("search/searchbar.html")
def search_options(request) -> Dict:
global search_form
if not search_form:
search_form = SearchForm()
"""Provide search options to template."""
return {
'options': search_form.options,
'options': search_form.get_options(),
'request': request,
}