Skip to content

Synthetic SearchIndex classes for custom objects #137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: feature
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions netbox_custom_objects/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,29 @@ class CustomObjectsPluginConfig(PluginConfig):
required_settings = []
template_extensions = "template_content.template_extensions"

def ready(self):
super().ready()
# Suppress warnings about database calls during model loading
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore", category=RuntimeWarning, message=".*database.*"
)
warnings.filterwarnings(
"ignore", category=UserWarning, message=".*database.*"
)

# Skip custom object type model loading if running during migration
if is_running_migration() or not check_custom_object_type_table_exists():
return

from .models import CustomObjectType
from .search import register_custom_object_search_index

custom_object_types = CustomObjectType.objects.all()
for custom_type in custom_object_types:
# Synthesize SearchIndex classes for all CustomObjectTypes
register_custom_object_search_index(custom_type)

def get_model(self, model_name, require_ready=True):
try:
# if the model is already loaded, return it
Expand Down Expand Up @@ -106,6 +129,7 @@ def get_models(self, include_auto_created=False, include_swapped=False):

custom_object_types = CustomObjectType.objects.all()
for custom_type in custom_object_types:

# Only yield already cached models during discovery
if CustomObjectType.is_model_cached(custom_type.id):
model = CustomObjectType.get_cached_model(custom_type.id)
Expand Down
36 changes: 36 additions & 0 deletions netbox_custom_objects/search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from netbox.registry import registry
from netbox.search import SearchIndex, register_search
from extras.choices import CustomFieldTypeChoices
from . import models, constants


@register_search
class CustomObjectTypeIndex(SearchIndex):
model = models.CustomObjectType
fields = (
('name', 100),
('description', 500),
('comments', 5000),
)
display_attrs = ('description', 'description')


def register_custom_object_search_index(custom_object_type):
fields = []
for field in custom_object_type.fields.all():
if field.primary or field.type == CustomFieldTypeChoices.TYPE_TEXT:
fields.append((field.name, 100))

model = custom_object_type.get_model()
attrs = {
"model": model,
"fields": tuple(fields),
"display_attrs": tuple(),
}
search_index = type(
f"{custom_object_type.name}SearchIndex",
(SearchIndex,),
attrs,
)
label = f"{constants.APP_LABEL}.{custom_object_type.get_table_model_name(custom_object_type.id).lower()}"
registry["search"][label] = search_index