mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-25 00:36:11 -06:00
Build search form options from registry
This commit is contained in:
parent
e67965ae22
commit
cefb96fa1d
@ -1,3 +1,4 @@
|
|||||||
|
from collections import defaultdict
|
||||||
from importlib import import_module
|
from importlib import import_module
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
@ -86,17 +87,22 @@ class SearchBackend(object):
|
|||||||
if self._search_choice_options:
|
if self._search_choice_options:
|
||||||
return self._search_choice_options
|
return self._search_choice_options
|
||||||
|
|
||||||
result = list()
|
# Organize choices by category
|
||||||
result.append(('', 'All Objects'))
|
categories = defaultdict(dict)
|
||||||
for category, items in self._search_choices.items():
|
for app_label, models in registry['search'].items():
|
||||||
subcategories = list()
|
for name, cls in models.items():
|
||||||
for slug, verbose_name in items.items():
|
model = cls.queryset.model
|
||||||
name = verbose_name
|
title = model._meta.verbose_name.title()
|
||||||
name = name[0].upper() + name[1:]
|
categories[cls.choice_header][name] = title
|
||||||
subcategories.append((slug, name))
|
|
||||||
result.append((category, tuple(subcategories)))
|
# Compile a nested tuple of choices for form rendering
|
||||||
|
results = (
|
||||||
|
('', 'All Objects'),
|
||||||
|
*[(category, choices.items()) for category, choices in categories.items()]
|
||||||
|
)
|
||||||
|
|
||||||
|
self._search_choice_options = results
|
||||||
|
|
||||||
self._search_choice_options = tuple(result)
|
|
||||||
return self._search_choice_options
|
return self._search_choice_options
|
||||||
|
|
||||||
def _use_hooks(self):
|
def _use_hooks(self):
|
||||||
|
@ -5,15 +5,11 @@ from django import template
|
|||||||
from netbox.forms import SearchForm
|
from netbox.forms import SearchForm
|
||||||
|
|
||||||
register = template.Library()
|
register = template.Library()
|
||||||
search_form = None
|
search_form = SearchForm()
|
||||||
|
|
||||||
|
|
||||||
@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 {
|
||||||
|
Loading…
Reference in New Issue
Block a user