1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00
Jeremy Stretch 7abb2b2ab5 Closes #15131: Dynamic queryset annotations for REST API endpoints (#15152)
* Introduce RelatedObjectCountField

* Introduce get_annotations_for_serializer() and enable dynamic annotations

* Add RelatedObjectCountFields to serializers; remove static annotations from querysets

* Remove annotations cleanup logic from BriefModeMixin

* Annotate type for RelatedObjectCountField

* Remove redundant field on TagSerializer

* Add missing reverse relationship for power feeds to rack

* Refactor RelatedObjectCountField to take a single relationship name
2024-02-15 14:49:27 -05:00

71 lines
1.8 KiB
Python

from rest_framework.routers import APIRootView
from netbox.api.viewsets import NetBoxModelViewSet, MPTTLockedMixin
from tenancy import filtersets
from tenancy.models import *
from . import serializers
class TenancyRootView(APIRootView):
"""
Tenancy API root view
"""
def get_view_name(self):
return 'Tenancy'
#
# Tenants
#
class TenantGroupViewSet(MPTTLockedMixin, NetBoxModelViewSet):
queryset = TenantGroup.objects.add_related_count(
TenantGroup.objects.all(),
Tenant,
'group',
'tenant_count',
cumulative=True
)
serializer_class = serializers.TenantGroupSerializer
filterset_class = filtersets.TenantGroupFilterSet
class TenantViewSet(NetBoxModelViewSet):
queryset = Tenant.objects.all()
serializer_class = serializers.TenantSerializer
filterset_class = filtersets.TenantFilterSet
#
# Contacts
#
class ContactGroupViewSet(MPTTLockedMixin, NetBoxModelViewSet):
queryset = ContactGroup.objects.add_related_count(
ContactGroup.objects.all(),
Contact,
'group',
'contact_count',
cumulative=True
)
serializer_class = serializers.ContactGroupSerializer
filterset_class = filtersets.ContactGroupFilterSet
class ContactRoleViewSet(NetBoxModelViewSet):
queryset = ContactRole.objects.all()
serializer_class = serializers.ContactRoleSerializer
filterset_class = filtersets.ContactRoleFilterSet
class ContactViewSet(NetBoxModelViewSet):
queryset = Contact.objects.all()
serializer_class = serializers.ContactSerializer
filterset_class = filtersets.ContactFilterSet
class ContactAssignmentViewSet(NetBoxModelViewSet):
queryset = ContactAssignment.objects.all()
serializer_class = serializers.ContactAssignmentSerializer
filterset_class = filtersets.ContactAssignmentFilterSet