mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Rename OrderedDefaultRouter to NetBoxRouter & document for plugins
This commit is contained in:
@ -70,10 +70,10 @@ Routers should be exposed in `api/urls.py`. This file **must** define a variable
|
|||||||
|
|
||||||
```python
|
```python
|
||||||
# api/urls.py
|
# api/urls.py
|
||||||
from rest_framework import routers
|
from netbox.api.routers import NetBoxRouter
|
||||||
from .views import MyModelViewSet
|
from .views import MyModelViewSet
|
||||||
|
|
||||||
router = routers.DefaultRouter()
|
router = NetBoxRouter()
|
||||||
router.register('my-model', MyModelViewSet)
|
router.register('my-model', MyModelViewSet)
|
||||||
urlpatterns = router.urls
|
urlpatterns = router.urls
|
||||||
```
|
```
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
from netbox.api import OrderedDefaultRouter
|
from netbox.api import NetBoxRouter
|
||||||
from . import views
|
from . import views
|
||||||
|
|
||||||
|
|
||||||
router = OrderedDefaultRouter()
|
router = NetBoxRouter()
|
||||||
router.APIRootView = views.CircuitsRootView
|
router.APIRootView = views.CircuitsRootView
|
||||||
|
|
||||||
# Providers
|
# Providers
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
from netbox.api import OrderedDefaultRouter
|
from netbox.api import NetBoxRouter
|
||||||
from . import views
|
from . import views
|
||||||
|
|
||||||
|
|
||||||
router = OrderedDefaultRouter()
|
router = NetBoxRouter()
|
||||||
router.APIRootView = views.DCIMRootView
|
router.APIRootView = views.DCIMRootView
|
||||||
|
|
||||||
# Sites
|
# Sites
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
from netbox.api import OrderedDefaultRouter
|
from netbox.api import NetBoxRouter
|
||||||
from . import views
|
from . import views
|
||||||
|
|
||||||
|
|
||||||
router = OrderedDefaultRouter()
|
router = NetBoxRouter()
|
||||||
router.APIRootView = views.ExtrasRootView
|
router.APIRootView = views.ExtrasRootView
|
||||||
|
|
||||||
# Webhooks
|
# Webhooks
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
from django.urls import path
|
from django.urls import path
|
||||||
|
|
||||||
from netbox.api import OrderedDefaultRouter
|
from netbox.api import NetBoxRouter
|
||||||
from ipam.models import IPRange, Prefix
|
from ipam.models import IPRange, Prefix
|
||||||
from . import views
|
from . import views
|
||||||
|
|
||||||
|
|
||||||
router = OrderedDefaultRouter()
|
router = NetBoxRouter()
|
||||||
router.APIRootView = views.IPAMRootView
|
router.APIRootView = views.IPAMRootView
|
||||||
|
|
||||||
# ASNs
|
# ASNs
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
from .fields import ChoiceField, ContentTypeField, SerializedPKRelatedField
|
from .fields import ChoiceField, ContentTypeField, SerializedPKRelatedField
|
||||||
from .routers import OrderedDefaultRouter
|
from .routers import NetBoxRouter
|
||||||
from .serializers import BulkOperationSerializer, ValidatedModelSerializer, WritableNestedSerializer
|
from .serializers import BulkOperationSerializer, ValidatedModelSerializer, WritableNestedSerializer
|
||||||
|
|
||||||
|
|
||||||
@ -7,7 +7,7 @@ __all__ = (
|
|||||||
'BulkOperationSerializer',
|
'BulkOperationSerializer',
|
||||||
'ChoiceField',
|
'ChoiceField',
|
||||||
'ContentTypeField',
|
'ContentTypeField',
|
||||||
'OrderedDefaultRouter',
|
'NetBoxRouter',
|
||||||
'SerializedPKRelatedField',
|
'SerializedPKRelatedField',
|
||||||
'ValidatedModelSerializer',
|
'ValidatedModelSerializer',
|
||||||
'WritableNestedSerializer',
|
'WritableNestedSerializer',
|
||||||
|
@ -1,14 +1,16 @@
|
|||||||
from collections import OrderedDict
|
|
||||||
|
|
||||||
from rest_framework.routers import DefaultRouter
|
from rest_framework.routers import DefaultRouter
|
||||||
|
|
||||||
|
|
||||||
class OrderedDefaultRouter(DefaultRouter):
|
class NetBoxRouter(DefaultRouter):
|
||||||
|
"""
|
||||||
|
Extend DRF's built-in DefaultRouter to:
|
||||||
|
1. Support bulk operations
|
||||||
|
2. Alphabetically order endpoints under the root view
|
||||||
|
"""
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
# Extend the list view mappings to support the DELETE operation
|
# Update the list view mappings to support bulk operations
|
||||||
self.routes[0].mapping.update({
|
self.routes[0].mapping.update({
|
||||||
'put': 'bulk_update',
|
'put': 'bulk_update',
|
||||||
'patch': 'bulk_partial_update',
|
'patch': 'bulk_partial_update',
|
||||||
@ -19,7 +21,7 @@ class OrderedDefaultRouter(DefaultRouter):
|
|||||||
"""
|
"""
|
||||||
Wrap DRF's DefaultRouter to return an alphabetized list of endpoints.
|
Wrap DRF's DefaultRouter to return an alphabetized list of endpoints.
|
||||||
"""
|
"""
|
||||||
api_root_dict = OrderedDict()
|
api_root_dict = {}
|
||||||
list_name = self.routes[0].name
|
list_name = self.routes[0].name
|
||||||
for prefix, viewset, basename in sorted(self.registry, key=lambda x: x[0]):
|
for prefix, viewset, basename in sorted(self.registry, key=lambda x: x[0]):
|
||||||
api_root_dict[prefix] = list_name.format(basename=basename)
|
api_root_dict[prefix] = list_name.format(basename=basename)
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
from netbox.api import OrderedDefaultRouter
|
from netbox.api import NetBoxRouter
|
||||||
from . import views
|
from . import views
|
||||||
|
|
||||||
|
|
||||||
router = OrderedDefaultRouter()
|
router = NetBoxRouter()
|
||||||
router.APIRootView = views.TenancyRootView
|
router.APIRootView = views.TenancyRootView
|
||||||
|
|
||||||
# Tenants
|
# Tenants
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
from django.urls import include, path
|
from django.urls import include, path
|
||||||
|
|
||||||
from netbox.api import OrderedDefaultRouter
|
from netbox.api import NetBoxRouter
|
||||||
from . import views
|
from . import views
|
||||||
|
|
||||||
|
|
||||||
router = OrderedDefaultRouter()
|
router = NetBoxRouter()
|
||||||
router.APIRootView = views.UsersRootView
|
router.APIRootView = views.UsersRootView
|
||||||
|
|
||||||
# Users and groups
|
# Users and groups
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
from netbox.api import OrderedDefaultRouter
|
from netbox.api import NetBoxRouter
|
||||||
from . import views
|
from . import views
|
||||||
|
|
||||||
|
|
||||||
router = OrderedDefaultRouter()
|
router = NetBoxRouter()
|
||||||
router.APIRootView = views.VirtualizationRootView
|
router.APIRootView = views.VirtualizationRootView
|
||||||
|
|
||||||
# Clusters
|
# Clusters
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
from netbox.api import OrderedDefaultRouter
|
from netbox.api import NetBoxRouter
|
||||||
from . import views
|
from . import views
|
||||||
|
|
||||||
|
|
||||||
router = OrderedDefaultRouter()
|
router = NetBoxRouter()
|
||||||
router.APIRootView = views.WirelessRootView
|
router.APIRootView = views.WirelessRootView
|
||||||
|
|
||||||
router.register('wireless-lan-groups', views.WirelessLANGroupViewSet)
|
router.register('wireless-lan-groups', views.WirelessLANGroupViewSet)
|
||||||
|
Reference in New Issue
Block a user