mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Initial multitenancy implementation
This commit is contained in:
0
netbox/tenancy/api/__init__.py
Normal file
0
netbox/tenancy/api/__init__.py
Normal file
38
netbox/tenancy/api/serializers.py
Normal file
38
netbox/tenancy/api/serializers.py
Normal file
@@ -0,0 +1,38 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
from tenancy.models import Tenant, TenantGroup
|
||||
|
||||
|
||||
#
|
||||
# Tenant groups
|
||||
#
|
||||
|
||||
class TenantGroupSerializer(serializers.ModelSerializer):
|
||||
|
||||
class Meta:
|
||||
model = TenantGroup
|
||||
fields = ['id', 'name', 'slug']
|
||||
|
||||
|
||||
class TenantGroupNestedSerializer(TenantGroupSerializer):
|
||||
|
||||
class Meta(TenantGroupSerializer.Meta):
|
||||
pass
|
||||
|
||||
|
||||
#
|
||||
# Tenants
|
||||
#
|
||||
|
||||
class TenantSerializer(serializers.ModelSerializer):
|
||||
group = TenantGroupNestedSerializer()
|
||||
|
||||
class Meta:
|
||||
model = Tenant
|
||||
fields = ['id', 'name', 'slug', 'group', 'comments']
|
||||
|
||||
|
||||
class TenantNestedSerializer(TenantSerializer):
|
||||
|
||||
class Meta(TenantSerializer.Meta):
|
||||
fields = ['id', 'name', 'slug']
|
||||
16
netbox/tenancy/api/urls.py
Normal file
16
netbox/tenancy/api/urls.py
Normal file
@@ -0,0 +1,16 @@
|
||||
from django.conf.urls import url
|
||||
|
||||
from .views import *
|
||||
|
||||
|
||||
urlpatterns = [
|
||||
|
||||
# Tenant groups
|
||||
url(r'^tenant-groups/$', TenantGroupListView.as_view(), name='tenantgroup_list'),
|
||||
url(r'^tenant-groups/(?P<pk>\d+)/$', TenantGroupDetailView.as_view(), name='tenantgroup_detail'),
|
||||
|
||||
# Tenants
|
||||
url(r'^tenants/$', TenantListView.as_view(), name='tenant_list'),
|
||||
url(r'^tenants/(?P<pk>\d+)/$', TenantDetailView.as_view(), name='tenant_detail'),
|
||||
|
||||
]
|
||||
39
netbox/tenancy/api/views.py
Normal file
39
netbox/tenancy/api/views.py
Normal file
@@ -0,0 +1,39 @@
|
||||
from rest_framework import generics
|
||||
|
||||
from tenancy.models import Tenant, TenantGroup
|
||||
from tenancy.filters import TenantFilter
|
||||
|
||||
from . import serializers
|
||||
|
||||
|
||||
class TenantGroupListView(generics.ListAPIView):
|
||||
"""
|
||||
List all tenant groups
|
||||
"""
|
||||
queryset = TenantGroup.objects.all()
|
||||
serializer_class = serializers.TenantGroupSerializer
|
||||
|
||||
|
||||
class TenantGroupDetailView(generics.RetrieveAPIView):
|
||||
"""
|
||||
Retrieve a single circuit type
|
||||
"""
|
||||
queryset = TenantGroup.objects.all()
|
||||
serializer_class = serializers.TenantGroupSerializer
|
||||
|
||||
|
||||
class TenantListView(generics.ListAPIView):
|
||||
"""
|
||||
List tenants (filterable)
|
||||
"""
|
||||
queryset = Tenant.objects.select_related('group')
|
||||
serializer_class = serializers.TenantSerializer
|
||||
filter_class = TenantFilter
|
||||
|
||||
|
||||
class TenantDetailView(generics.RetrieveAPIView):
|
||||
"""
|
||||
Retrieve a single tenant
|
||||
"""
|
||||
queryset = Tenant.objects.select_related('group')
|
||||
serializer_class = serializers.TenantSerializer
|
||||
Reference in New Issue
Block a user