Logo
  • NAV documentation
  • Overview
  • Installing NAV
  • Getting started with NAV
  • Getting organized
  • Howtos
  • FAQ
  • Reference material
  • Network Administration Visualized release notes
  • Glossary
  • Hacking NAV
    • Hacker’s guide to NAV
    • Hacking with PyCharm
    • Using NAV with Docker for development
    • Javascript hacking
    • Checklist for releasing a new NAV version
    • Writing a new servicemon plugin
    • Adding support for a new environment probe device to NAV
    • The NAV API
    • Extending NAV locally
    • How to customize parts of NAV’s web interface
    • NAVbar search providers
      • A simple implementation example
      • The SearchProvider base class
        • SearchProvider
      • The SearchResult namedtuple
        • SearchResult
    • Establishing SNMP tunnels using socat/SSH
Logo
  • Hacking NAV
  • NAVbar search providers
Previous Next

NAVbar search providers

NAV’s search bar (the one seen at the top of every NAV web page) provides a pluggable architecture for search implementations. Any class that implements the interface of nav.web.info.searchproviders.SearchProvider can be used as a provider of search results for the NAVbar.

The list of SearchProvider implementations that are used to respond to NAVbar search is configured in NAV’s Django settings. The default list, as of NAV 4.8, looks like this:

# Classes that implement a search engine for the web navbar
SEARCHPROVIDERS = [
    'nav.web.info.searchproviders.RoomSearchProvider',
    'nav.web.info.searchproviders.LocationSearchProvider',
    'nav.web.info.searchproviders.NetboxSearchProvider',
    'nav.web.info.searchproviders.InterfaceSearchProvider',
    'nav.web.info.searchproviders.VlanSearchProvider',
    'nav.web.info.searchproviders.PrefixSearchProvider',
    'nav.web.info.searchproviders.DevicegroupSearchProvider',
    'nav.web.info.searchproviders.UnrecognizedNeighborSearchProvider',
]

If you want to hook in your own SearchProvider, local to your installation, you can do this by manipulating this list in your local_settings.py file.

A simple implementation example

This is the current implementation of the Room search provider; it will do a substring search among room IDs, and then return search results which link back to each matched room’s details page.

class RoomSearchProvider(SearchProvider):
    """Searchprovider for rooms"""
    name = "Rooms"
    headers = [
        ('Roomid', 'id'),
        ('Description', 'description')
    ]
    link = 'Roomid'

    def fetch_results(self):
        results = Room.objects.filter(id__icontains=self.query).order_by("id")
        for result in results:
            self.results.append(SearchResult(
                reverse('room-info', kwargs={'roomid': result.id}),
                result)
            )

The actual work of the implementation is accomplished within the fetch_results() method, which must return a list of nav.web.info.searchproviders.SearchResult namedtuples.

The headers class attribute defines how to extract columnar information from the returned SearchResult’s instance objects. In this case, the search result tab for Rooms will contain two columns: One captioned Roomid, where the cell values come from the Room objects’ id attribute, and one captioned Description, where the cell values come from the Room objects’ description attributes.

The SearchProvider base class

class nav.web.info.searchproviders.SearchProvider(query='')

Search provider interface.

To implement this interface, all that is needed is to inherit from this class, set some class attributes and override the fetch_results() method. The query, as entered by the end-user will be available in the self.query variable.

__init__(query='')
Parameters:

query – The search query string, as entered by the user.

fetch_results()

Fetches and returns results based on self.query.

Returns:

A list of nav.web.info.searchproviders.SearchResult instances.

headers = (('Column title', 'result instance attribute name'),)

Defines the result table columns; what the column titles should be, and which attribute from the SearchResult.inst object to extract the values for this column from.

link = 'id'

The title of the result column to put hyperlinks in

name = 'SearchProvider'

Used as the caption of results from this provider

The SearchResult namedtuple

A SearchResult namedtuple consist of the href and inst attributes. href is a URL used as a hyperlink on the search result line. inst is normally some kind of instance object, typically a Django model, which represent the search result itself.

class nav.web.info.searchproviders.SearchResult(href, inst)
href

Alias for field number 0

inst

Alias for field number 1

Previous Next

© Copyright 2012-2025 Uninett AS, 2022-2025 Sikt.

Built with Sphinx using a theme provided by Read the Docs.