netbox-community / netbox-plugin-tutorial

A tutorial on building custom plugins for NetBox v3.2+
90 stars 48 forks source link

ImportError in api/urls.py #8

Closed dankotrajkovic closed 2 years ago

dankotrajkovic commented 2 years ago

Environment Python: 3.8.10 Code: v3.2-beta2 (got from sudo git clone -b v3.2-beta2 --depth 1 https://github.com/netbox-community/netbox.git . )

Cloning the netbox-plugin-tutorial doing a checkout on the last step to try out the functionality.

When applying the migrations with python netbox/manage.py migrate produces the following error.

  File "/home/network/netbox-plugin-demo/netbox_access_lists/api/urls.py", line 1, in <module>
    from netbox.api.routers import NetBoxRouter
ImportError: cannot import name 'NetBoxRouter' from 'netbox.api.routers' (/opt/netbox/netbox/netbox/api/routers.py)

Looking at the contents of netbox/netbox/netbox/api/routers.py in v3.2-beta2 it does not seem to have a NetBoxRouter class


from collections import OrderedDict

from rest_framework.routers import DefaultRouter

class OrderedDefaultRouter(DefaultRouter):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # Extend the list view mappings to support the DELETE operation
        self.routes[0].mapping.update({
            'put': 'bulk_update',
            'patch': 'bulk_partial_update',
            'delete': 'bulk_destroy',
        })

    def get_api_root_view(self, api_urls=None):
        """
        Wrap DRF's DefaultRouter to return an alphabetized list of endpoints.
        """
        api_root_dict = OrderedDict()
        list_name = self.routes[0].name
        for prefix, viewset, basename in sorted(self.registry, key=lambda x: x[0]):
            api_root_dict[prefix] = list_name.format(basename=basename)

        return self.APIRootView.as_view(api_root_dict=api_root_dict)```
jeremystretch commented 2 years ago

IIRC OrderedDefaultRouter was renamed to NetBoxRouter after the v3.2-beta2 release. (It would have been available in the feature branch.) Sorry for the confusion. It's a moot point now that v3.2.0 has been released.