mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-25 00:36:11 -06:00
Move search app to a module under netbox/
This commit is contained in:
parent
777e883c73
commit
5797ba0fd7
@ -2,7 +2,7 @@ import circuits.filtersets
|
|||||||
import circuits.tables
|
import circuits.tables
|
||||||
from circuits.models import Circuit, Provider, ProviderNetwork
|
from circuits.models import Circuit, Provider, ProviderNetwork
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from search.models import SearchMixin
|
from netbox.search.models import SearchMixin
|
||||||
from utilities.utils import count_related
|
from utilities.utils import count_related
|
||||||
|
|
||||||
|
|
||||||
|
@ -4,7 +4,6 @@ from dcim.models import (
|
|||||||
Cable,
|
Cable,
|
||||||
Device,
|
Device,
|
||||||
DeviceType,
|
DeviceType,
|
||||||
Interface,
|
|
||||||
Location,
|
Location,
|
||||||
Module,
|
Module,
|
||||||
ModuleType,
|
ModuleType,
|
||||||
@ -14,8 +13,7 @@ from dcim.models import (
|
|||||||
Site,
|
Site,
|
||||||
VirtualChassis,
|
VirtualChassis,
|
||||||
)
|
)
|
||||||
from django.db import models
|
from netbox.search.models import SearchMixin
|
||||||
from search.models import SearchMixin
|
|
||||||
from utilities.utils import count_related
|
from utilities.utils import count_related
|
||||||
|
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@ import extras.filtersets
|
|||||||
import extras.tables
|
import extras.tables
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from extras.models import JournalEntry
|
from extras.models import JournalEntry
|
||||||
from search.models import SearchMixin
|
from netbox.search.models import SearchMixin
|
||||||
from utilities.utils import count_related
|
from utilities.utils import count_related
|
||||||
|
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@ import ipam.filtersets
|
|||||||
import ipam.tables
|
import ipam.tables
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from ipam.models import ASN, VLAN, VRF, Aggregate, IPAddress, Prefix, Service
|
from ipam.models import ASN, VLAN, VRF, Aggregate, IPAddress, Prefix, Service
|
||||||
from search.models import SearchMixin
|
from netbox.search.models import SearchMixin
|
||||||
from utilities.utils import count_related
|
from utilities.utils import count_related
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
from django import forms
|
from django import forms
|
||||||
from utilities.forms import BootstrapMixin
|
|
||||||
|
|
||||||
from search.backends import default_search_engine
|
from netbox.search.backends import default_search_engine
|
||||||
|
from utilities.forms import BootstrapMixin
|
||||||
|
|
||||||
from .base import *
|
from .base import *
|
||||||
|
|
||||||
@ -25,7 +25,11 @@ class SearchForm(BootstrapMixin, forms.Form):
|
|||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self.fields["obj_type"] = forms.ChoiceField(choices=default_search_engine.get_search_choices(), required=False, label='Type')
|
self.fields["obj_type"] = forms.ChoiceField(
|
||||||
|
choices=default_search_engine.get_search_choices(),
|
||||||
|
required=False,
|
||||||
|
label='Type'
|
||||||
|
)
|
||||||
|
|
||||||
def get_options(self):
|
def get_options(self):
|
||||||
if not self.options:
|
if not self.options:
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
from abc import ABC
|
|
||||||
from importlib import import_module
|
from importlib import import_module
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.exceptions import ImproperlyConfigured
|
from django.core.exceptions import ImproperlyConfigured
|
||||||
from django.db import models
|
|
||||||
from django.db.models.signals import post_save, pre_delete
|
from django.db.models.signals import post_save, pre_delete
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from netbox.constants import SEARCH_MAX_RESULTS
|
from netbox.constants import SEARCH_MAX_RESULTS
|
||||||
@ -146,7 +144,7 @@ def get_backend(backend_name=None):
|
|||||||
"""Initializes and returns the search backend."""
|
"""Initializes and returns the search backend."""
|
||||||
global _backends_cache
|
global _backends_cache
|
||||||
if not backend_name:
|
if not backend_name:
|
||||||
backend_name = getattr(settings, "SEARCH_BACKEND", "search.backends.PostgresIcontainsSearchBackend")
|
backend_name = getattr(settings, "SEARCH_BACKEND", "netbox.search.backends.PostgresIcontainsSearchBackend")
|
||||||
|
|
||||||
# Try to use the cached backend.
|
# Try to use the cached backend.
|
||||||
if backend_name in _backends_cache:
|
if backend_name in _backends_cache:
|
@ -1,7 +1,3 @@
|
|||||||
from django.core.exceptions import ImproperlyConfigured
|
|
||||||
from django.db import models
|
|
||||||
|
|
||||||
|
|
||||||
class SearchMixin(object):
|
class SearchMixin(object):
|
||||||
"""
|
"""
|
||||||
Base class for building search indexes.
|
Base class for building search indexes.
|
39
netbox/netbox/search/register.py
Normal file
39
netbox/netbox/search/register.py
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
import importlib
|
||||||
|
import inspect
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from django.apps import apps
|
||||||
|
from django.conf import settings
|
||||||
|
from django.utils.module_loading import module_has_submodule
|
||||||
|
|
||||||
|
from .backends import default_search_engine
|
||||||
|
from .hierarchy import SEARCH_TYPES
|
||||||
|
|
||||||
|
|
||||||
|
def get_app_modules():
|
||||||
|
"""
|
||||||
|
Returns all app modules (installed apps) - yields tuples of (app_name, module)
|
||||||
|
"""
|
||||||
|
for app in apps.get_app_configs():
|
||||||
|
yield app.name, app.module
|
||||||
|
|
||||||
|
|
||||||
|
def register():
|
||||||
|
for name, module in SEARCH_TYPES.items():
|
||||||
|
default_search_engine.register(name, module)
|
||||||
|
|
||||||
|
for name, module in get_app_modules():
|
||||||
|
submodule_name = "search_indexes"
|
||||||
|
if module_has_submodule(module, submodule_name):
|
||||||
|
module_name = f"{name}.{submodule_name}"
|
||||||
|
if name in settings.PLUGINS:
|
||||||
|
search_module = importlib.import_module(module_name)
|
||||||
|
else:
|
||||||
|
search_module = sys.modules[module_name]
|
||||||
|
|
||||||
|
cls_objects = inspect.getmembers(search_module, predicate=inspect.isclass)
|
||||||
|
for cls_name, cls_obj in inspect.getmembers(search_module, predicate=inspect.isclass):
|
||||||
|
if getattr(cls_obj, "search_index", False) and getattr(cls_obj, "model", None):
|
||||||
|
cls_name = cls_obj.model.__name__.lower()
|
||||||
|
if not default_search_engine.is_registered(cls_name, cls_obj):
|
||||||
|
default_search_engine.register(cls_name, cls_obj)
|
@ -319,7 +319,6 @@ INSTALLED_APPS = [
|
|||||||
'social_django',
|
'social_django',
|
||||||
'taggit',
|
'taggit',
|
||||||
'timezone_field',
|
'timezone_field',
|
||||||
'search',
|
|
||||||
'circuits',
|
'circuits',
|
||||||
'dcim',
|
'dcim',
|
||||||
'ipam',
|
'ipam',
|
||||||
|
@ -23,7 +23,7 @@ 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.constants import SEARCH_MAX_RESULTS
|
||||||
from netbox.forms import SearchForm
|
from netbox.forms import SearchForm
|
||||||
from search.backends import default_search_engine
|
from netbox.search.backends import default_search_engine
|
||||||
from tenancy.models import Tenant
|
from tenancy.models import Tenant
|
||||||
from virtualization.models import Cluster, VirtualMachine
|
from virtualization.models import Cluster, VirtualMachine
|
||||||
from wireless.models import WirelessLAN, WirelessLink
|
from wireless.models import WirelessLAN, WirelessLink
|
||||||
|
@ -1,45 +0,0 @@
|
|||||||
import importlib
|
|
||||||
import inspect
|
|
||||||
import sys
|
|
||||||
from django.apps import AppConfig
|
|
||||||
|
|
||||||
from django.apps import apps
|
|
||||||
from django.conf import settings
|
|
||||||
from django.utils.module_loading import module_has_submodule
|
|
||||||
from netbox import denormalized
|
|
||||||
|
|
||||||
|
|
||||||
def get_app_modules():
|
|
||||||
"""
|
|
||||||
Returns all app modules (installed apps) - yields tuples of (app_name, module)
|
|
||||||
"""
|
|
||||||
for app in apps.get_app_configs():
|
|
||||||
yield app.name, app.module
|
|
||||||
|
|
||||||
|
|
||||||
class SearchConfig(AppConfig):
|
|
||||||
name = "search"
|
|
||||||
verbose_name = "search"
|
|
||||||
|
|
||||||
def ready(self):
|
|
||||||
from .backends import default_search_engine
|
|
||||||
from .hierarchy import SEARCH_TYPES
|
|
||||||
|
|
||||||
for name, module in SEARCH_TYPES.items():
|
|
||||||
default_search_engine.register(name, module)
|
|
||||||
|
|
||||||
for name, module in get_app_modules():
|
|
||||||
submodule_name = "search_indexes"
|
|
||||||
if module_has_submodule(module, submodule_name):
|
|
||||||
module_name = f"{name}.{submodule_name}"
|
|
||||||
if name in settings.PLUGINS:
|
|
||||||
search_module = importlib.import_module(module_name)
|
|
||||||
else:
|
|
||||||
search_module = sys.modules[module_name]
|
|
||||||
|
|
||||||
cls_objects = inspect.getmembers(search_module, predicate=inspect.isclass)
|
|
||||||
for cls_name, cls_obj in inspect.getmembers(search_module, predicate=inspect.isclass):
|
|
||||||
if getattr(cls_obj, "search_index", False) and getattr(cls_obj, "model", None):
|
|
||||||
cls_name = cls_obj.model.__name__.lower()
|
|
||||||
if not default_search_engine.is_registered(cls_name, cls_obj):
|
|
||||||
default_search_engine.register(cls_name, cls_obj)
|
|
@ -1,7 +1,7 @@
|
|||||||
import tenancy.filtersets
|
import tenancy.filtersets
|
||||||
import tenancy.tables
|
import tenancy.tables
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from search.models import SearchMixin
|
from netbox.search.models import SearchMixin
|
||||||
from tenancy.models import Contact, ContactAssignment, Tenant
|
from tenancy.models import Contact, ContactAssignment, Tenant
|
||||||
from utilities.utils import count_related
|
from utilities.utils import count_related
|
||||||
|
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
from typing import Dict
|
from typing import Dict
|
||||||
from netbox.forms import SearchForm
|
|
||||||
from django import template
|
from django import template
|
||||||
|
|
||||||
register = template.Library()
|
from netbox.forms import SearchForm
|
||||||
|
|
||||||
|
register = template.Library()
|
||||||
search_form = None
|
search_form = None
|
||||||
|
|
||||||
|
|
||||||
@ -14,7 +15,7 @@ def search_options(request) -> Dict:
|
|||||||
if not search_form:
|
if not search_form:
|
||||||
search_form = SearchForm()
|
search_form = SearchForm()
|
||||||
|
|
||||||
"""Provide search options to template."""
|
# Provide search options to template.
|
||||||
return {
|
return {
|
||||||
'options': search_form.get_options(),
|
'options': search_form.get_options(),
|
||||||
'request': request,
|
'request': request,
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import virtualization.filtersets
|
import virtualization.filtersets
|
||||||
import virtualization.tables
|
import virtualization.tables
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from search.models import SearchMixin
|
from netbox.search.models import SearchMixin
|
||||||
from utilities.utils import count_related
|
from utilities.utils import count_related
|
||||||
from virtualization.models import Cluster, Device, VirtualMachine
|
from virtualization.models import Cluster, Device, VirtualMachine
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@ import wireless.filtersets
|
|||||||
import wireless.tables
|
import wireless.tables
|
||||||
from dcim.models import Interface
|
from dcim.models import Interface
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from search.models import SearchMixin
|
from netbox.search.models import SearchMixin
|
||||||
from utilities.utils import count_related
|
from utilities.utils import count_related
|
||||||
from wireless.models import WirelessLAN, WirelessLink
|
from wireless.models import WirelessLAN, WirelessLink
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user