Skip to content

Commit

Permalink
Closes #14134: Display additional object attributes in global search …
Browse files Browse the repository at this point in the history
…results (#14154)

* WIP

* Add display_attrs for all indexers

* Linkify object attributes

* Clean up prefetch logic

* Use tooltips for display attributes

* Simplify template code

* Introduce get_indexer() utility function

* Add  to examples in docs

* Use tooltips to display long strings
  • Loading branch information
jeremystretch authored Nov 9, 2023
1 parent 2562c87 commit 3d20276
Show file tree
Hide file tree
Showing 15 changed files with 165 additions and 7 deletions.
1 change: 1 addition & 0 deletions docs/development/search.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class MyModelIndex(SearchIndex):
('description', 500),
('comments', 5000),
)
display_attrs = ('site', 'device', 'status', 'description')
```

A SearchIndex subclass defines both its model and a list of two-tuples specifying which model fields to be indexed and the weight (precedence) associated with each. Guidance on weight assignment for fields is provided below.
Expand Down
3 changes: 3 additions & 0 deletions docs/plugins/development/search.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ class MyModelIndex(SearchIndex):
('description', 500),
('comments', 5000),
)
display_attrs = ('site', 'device', 'status', 'description')
```

Fields listed in `display_attrs` will not be cached for search, but will be displayed alongside the object when it appears in global search results. This is helpful for conveying to the user additional information about an object.

To register one or more indexes with NetBox, define a list named `indexes` at the end of this file:

```python
Expand Down
6 changes: 6 additions & 0 deletions netbox/circuits/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class CircuitIndex(SearchIndex):
('description', 500),
('comments', 5000),
)
display_attrs = ('provider', 'provider_account', 'type', 'status', 'tenant', 'description')


@register_search
Expand All @@ -22,6 +23,7 @@ class CircuitTerminationIndex(SearchIndex):
('port_speed', 2000),
('upstream_speed', 2000),
)
display_attrs = ('circuit', 'site', 'provider_network', 'description')


@register_search
Expand All @@ -32,6 +34,7 @@ class CircuitTypeIndex(SearchIndex):
('slug', 110),
('description', 500),
)
display_attrs = ('description',)


@register_search
Expand All @@ -42,6 +45,7 @@ class ProviderIndex(SearchIndex):
('description', 500),
('comments', 5000),
)
display_attrs = ('description',)


class ProviderAccountIndex(SearchIndex):
Expand All @@ -51,6 +55,7 @@ class ProviderAccountIndex(SearchIndex):
('account', 200),
('comments', 5000),
)
display_attrs = ('provider', 'account', 'description')


@register_search
Expand All @@ -62,3 +67,4 @@ class ProviderNetworkIndex(SearchIndex):
('description', 500),
('comments', 5000),
)
display_attrs = ('provider', 'service_id', 'description')
1 change: 1 addition & 0 deletions netbox/core/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class DataSourceIndex(SearchIndex):
('description', 500),
('comments', 5000),
)
display_attrs = ('type', 'status', 'description')


@register_search
Expand Down
31 changes: 31 additions & 0 deletions netbox/dcim/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class CableIndex(SearchIndex):
('description', 500),
('comments', 5000),
)
display_attrs = ('type', 'status', 'tenant', 'label', 'description')


@register_search
Expand All @@ -21,6 +22,7 @@ class ConsolePortIndex(SearchIndex):
('description', 500),
('speed', 2000),
)
display_attrs = ('device', 'label', 'description')


@register_search
Expand All @@ -32,6 +34,7 @@ class ConsoleServerPortIndex(SearchIndex):
('description', 500),
('speed', 2000),
)
display_attrs = ('device', 'label', 'description')


@register_search
Expand All @@ -44,6 +47,9 @@ class DeviceIndex(SearchIndex):
('description', 500),
('comments', 5000),
)
display_attrs = (
'site', 'location', 'rack', 'device_type', 'role', 'tenant', 'platform', 'serial', 'asset_tag', 'description',
)


@register_search
Expand All @@ -54,6 +60,7 @@ class DeviceBayIndex(SearchIndex):
('label', 200),
('description', 500),
)
display_attrs = ('device', 'label', 'description')


@register_search
Expand All @@ -64,6 +71,7 @@ class DeviceRoleIndex(SearchIndex):
('slug', 110),
('description', 500),
)
display_attrs = ('description',)


@register_search
Expand All @@ -75,6 +83,7 @@ class DeviceTypeIndex(SearchIndex):
('description', 500),
('comments', 5000),
)
display_attrs = ('manufacturer', 'part_number', 'description')


@register_search
Expand All @@ -85,6 +94,7 @@ class FrontPortIndex(SearchIndex):
('label', 200),
('description', 500),
)
display_attrs = ('device', 'label', 'description')


@register_search
Expand All @@ -99,6 +109,7 @@ class InterfaceIndex(SearchIndex):
('mtu', 2000),
('speed', 2000),
)
display_attrs = ('device', 'label', 'description')


@register_search
Expand All @@ -112,6 +123,7 @@ class InventoryItemIndex(SearchIndex):
('description', 500),
('part_id', 2000),
)
display_attrs = ('device', 'manufacturer', 'part_id', 'serial', 'asset_tag', 'description')


@register_search
Expand All @@ -122,6 +134,7 @@ class LocationIndex(SearchIndex):
('slug', 110),
('description', 500),
)
display_attrs = ('site', 'status', 'tenant', 'description')


@register_search
Expand All @@ -132,6 +145,7 @@ class ManufacturerIndex(SearchIndex):
('slug', 110),
('description', 500),
)
display_attrs = ('description',)


@register_search
Expand All @@ -143,6 +157,7 @@ class ModuleIndex(SearchIndex):
('description', 500),
('comments', 5000),
)
display_attrs = ('device', 'module_bay', 'module_type', 'status', 'serial', 'asset_tag', 'description')


@register_search
Expand All @@ -153,6 +168,7 @@ class ModuleBayIndex(SearchIndex):
('label', 200),
('description', 500),
)
display_attrs = ('device', 'label', 'position', 'description')


@register_search
Expand All @@ -164,6 +180,7 @@ class ModuleTypeIndex(SearchIndex):
('description', 500),
('comments', 5000),
)
display_attrs = ('manufacturer', 'model', 'part_number', 'description')


@register_search
Expand All @@ -174,6 +191,7 @@ class PlatformIndex(SearchIndex):
('slug', 110),
('description', 500),
)
display_attrs = ('manufacturer', 'description')


@register_search
Expand All @@ -184,6 +202,7 @@ class PowerFeedIndex(SearchIndex):
('description', 500),
('comments', 5000),
)
display_attrs = ('power_panel', 'rack', 'status', 'description')


@register_search
Expand All @@ -194,6 +213,7 @@ class PowerOutletIndex(SearchIndex):
('label', 200),
('description', 500),
)
display_attrs = ('device', 'label', 'description')


@register_search
Expand All @@ -204,6 +224,7 @@ class PowerPanelIndex(SearchIndex):
('description', 500),
('comments', 5000),
)
display_attrs = ('site', 'location', 'description')


@register_search
Expand All @@ -216,6 +237,7 @@ class PowerPortIndex(SearchIndex):
('maximum_draw', 2000),
('allocated_draw', 2000),
)
display_attrs = ('device', 'label', 'description')


@register_search
Expand All @@ -229,6 +251,7 @@ class RackIndex(SearchIndex):
('description', 500),
('comments', 5000),
)
display_attrs = ('site', 'location', 'facility_id', 'tenant', 'status', 'role', 'description')


@register_search
Expand All @@ -238,6 +261,7 @@ class RackReservationIndex(SearchIndex):
('description', 500),
('comments', 5000),
)
display_attrs = ('rack', 'tenant', 'user', 'description')


@register_search
Expand All @@ -248,6 +272,7 @@ class RackRoleIndex(SearchIndex):
('slug', 110),
('description', 500),
)
display_attrs = ('device', 'label', 'description',)


@register_search
Expand All @@ -258,6 +283,7 @@ class RearPortIndex(SearchIndex):
('label', 200),
('description', 500),
)
display_attrs = ('device', 'label', 'description')


@register_search
Expand All @@ -268,6 +294,7 @@ class RegionIndex(SearchIndex):
('slug', 110),
('description', 500),
)
display_attrs = ('parent', 'description')


@register_search
Expand All @@ -282,6 +309,7 @@ class SiteIndex(SearchIndex):
('shipping_address', 2000),
('comments', 5000),
)
display_attrs = ('region', 'group', 'status', 'description')


@register_search
Expand All @@ -292,6 +320,7 @@ class SiteGroupIndex(SearchIndex):
('slug', 110),
('description', 500),
)
display_attrs = ('parent', 'description')


@register_search
Expand All @@ -303,6 +332,7 @@ class VirtualChassisIndex(SearchIndex):
('description', 500),
('comments', 5000),
)
display_attrs = ('master', 'domain', 'description')


@register_search
Expand All @@ -314,3 +344,4 @@ class VirtualDeviceContextIndex(SearchIndex):
('description', 500),
('comments', 5000),
)
display_attrs = ('device', 'status', 'identifier', 'description')
19 changes: 19 additions & 0 deletions netbox/extras/models/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
from django.db import models
from django.utils.translation import gettext_lazy as _

from netbox.search.utils import get_indexer
from netbox.registry import registry
from utilities.fields import RestrictedGenericForeignKey
from utilities.utils import content_type_identifier
from ..fields import CachedValueField

__all__ = (
Expand Down Expand Up @@ -58,3 +61,19 @@ class Meta:

def __str__(self):
return f'{self.object_type} {self.object_id}: {self.field}={self.value}'

@property
def display_attrs(self):
"""
Render any display attributes associated with this search result.
"""
indexer = get_indexer(self.object_type)
attrs = {}
for attr in indexer.display_attrs:
name = self.object._meta.get_field(attr).verbose_name
if value := getattr(self.object, attr):
if display_func := getattr(self.object, f'get_{attr}_display', None):
attrs[name] = display_func()
else:
attrs[name] = value
return attrs
Loading

0 comments on commit 3d20276

Please sign in to comment.