class ClusterViewSet(CustomFieldModelViewSet): queryset = Cluster.objects.prefetch_related("type", "group", "tenant", "site", "tags").annotate( device_count=count_related(Device, "cluster"), virtualmachine_count=count_related(VirtualMachine, "cluster"), ) serializer_class = serializers.ClusterSerializer filterset_class = filters.ClusterFilterSet
class DeviceRoleViewSet(CustomFieldModelViewSet): queryset = DeviceRole.objects.annotate( device_count=count_related(Device, "device_role"), virtualmachine_count=count_related(VirtualMachine, "role"), ) serializer_class = serializers.DeviceRoleSerializer filterset_class = filters.DeviceRoleFilterSet
class PlatformViewSet(CustomFieldModelViewSet): queryset = Platform.objects.annotate( device_count=count_related(Device, "platform"), virtualmachine_count=count_related(VirtualMachine, "platform"), ) serializer_class = serializers.PlatformSerializer filterset_class = filters.PlatformFilterSet
class RackViewSet(StatusViewSetMixin, CustomFieldModelViewSet): queryset = Rack.objects.prefetch_related( "site", "group__site", "status", "role", "tenant", "tags").annotate( device_count=count_related(Device, "rack"), powerfeed_count=count_related(PowerFeed, "rack"), ) serializer_class = serializers.RackSerializer filterset_class = filters.RackFilterSet @swagger_auto_schema( responses={200: serializers.RackUnitSerializer(many=True)}, query_serializer=serializers.RackElevationDetailFilterSerializer, ) @action(detail=True) def elevation(self, request, pk=None): """ Rack elevation representing the list of rack units. Also supports rendering the elevation as an SVG. """ rack = get_object_or_404(self.queryset, pk=pk) serializer = serializers.RackElevationDetailFilterSerializer( data=request.GET) serializer.is_valid(raise_exception=True) data = serializer.validated_data if data["render"] == "svg": # Render and return the elevation as an SVG drawing with the correct content type drawing = rack.get_elevation_svg( face=data["face"], user=request.user, unit_width=data["unit_width"], unit_height=data["unit_height"], legend_width=data["legend_width"], include_images=data["include_images"], base_url=request.build_absolute_uri("/"), ) return HttpResponse(drawing.tostring(), content_type="image/svg+xml") else: # Return a JSON representation of the rack units in the elevation elevation = rack.get_rack_units( face=data["face"], user=request.user, exclude=data["exclude"], expand_devices=data["expand_devices"], ) # Enable filtering rack units by ID q = data["q"] if q: elevation = [ u for u in elevation if q in str(u["id"]) or q in str(u["name"]) ] page = self.paginate_queryset(elevation) if page is not None: rack_units = serializers.RackUnitSerializer( page, many=True, context={"request": request}) return self.get_paginated_response(rack_units.data)
class RoleViewSet(CustomFieldModelViewSet): queryset = Role.objects.annotate( prefix_count=count_related(Prefix, "role"), vlan_count=count_related(VLAN, "role"), ) serializer_class = serializers.RoleSerializer filterset_class = filters.RoleFilterSet
class VRFViewSet(CustomFieldModelViewSet): queryset = (VRF.objects.prefetch_related("tenant").prefetch_related( "import_targets", "export_targets", "tags").annotate( ipaddress_count=count_related(IPAddress, "vrf"), prefix_count=count_related(Prefix, "vrf"), )) serializer_class = serializers.VRFSerializer filterset_class = filters.VRFFilterSet
class ManufacturerViewSet(CustomFieldModelViewSet): queryset = Manufacturer.objects.annotate( devicetype_count=count_related(DeviceType, "manufacturer"), inventoryitem_count=count_related(InventoryItem, "manufacturer"), platform_count=count_related(Platform, "manufacturer"), ) serializer_class = serializers.ManufacturerSerializer filterset_class = filters.ManufacturerFilterSet
class ClusterListView(generic.ObjectListView): permission_required = "virtualization.view_cluster" queryset = Cluster.objects.annotate( device_count=count_related(Device, "cluster"), vm_count=count_related(VirtualMachine, "cluster"), ) table = tables.ClusterTable filterset = filters.ClusterFilterSet filterset_form = forms.ClusterFilterForm
class RIRListView(generic.ObjectListView): queryset = RIR.objects.annotate( aggregate_count=count_related(Aggregate, "rir")) filterset = filters.RIRFilterSet filterset_form = forms.RIRFilterForm table = tables.RIRTable template_name = "ipam/rir_list.html"
class DeviceTypeViewSet(CustomFieldModelViewSet): queryset = DeviceType.objects.prefetch_related( "manufacturer", "tags").annotate(device_count=count_related(Device, "device_type")) serializer_class = serializers.DeviceTypeSerializer filterset_class = filters.DeviceTypeFilterSet brief_prefetch_fields = ["manufacturer"]
class TenantViewSet(CustomFieldModelViewSet): queryset = Tenant.objects.prefetch_related("group", "tags").annotate( circuit_count=count_related(Circuit, "tenant"), device_count=count_related(Device, "tenant"), ipaddress_count=count_related(IPAddress, "tenant"), prefix_count=count_related(Prefix, "tenant"), rack_count=count_related(Rack, "tenant"), site_count=count_related(Site, "tenant"), virtualmachine_count=count_related(VirtualMachine, "tenant"), vlan_count=count_related(VLAN, "tenant"), vrf_count=count_related(VRF, "tenant"), ) serializer_class = serializers.TenantSerializer filterset_class = filters.TenantFilterSet
class VLANViewSet(StatusViewSetMixin, CustomFieldModelViewSet): queryset = VLAN.objects.prefetch_related( "group", "site", "status", "role", "tags", "tenant", ).annotate(prefix_count=count_related(Prefix, "vlan")) serializer_class = serializers.VLANSerializer filterset_class = filters.VLANFilterSet
def get_extra_context(self, request, instance): # Clusters clusters = ( Cluster.objects.restrict(request.user, "view") .filter(group=instance) .prefetch_related("type", "site", "tenant") ).annotate( device_count=count_related(Device, "cluster"), vm_count=count_related(VirtualMachine, "cluster"), ) cluster_table = tables.ClusterTable(clusters) cluster_table.columns.hide("group") paginate = { "paginator_class": EnhancedPaginator, "per_page": get_paginate_count(request), } RequestConfig(request, paginate).configure(cluster_table) return { "cluster_table": cluster_table, }
class TagViewSet(CustomFieldModelViewSet): queryset = Tag.objects.annotate(tagged_items=count_related(TaggedItem, "tag")) serializer_class = serializers.TagSerializerVersion13 filterset_class = filters.TagFilterSet def get_serializer_class(self): if ( not self.brief and not getattr(self, "swagger_fake_view", False) and ( not hasattr(self.request, "major_version") or self.request.major_version > 1 or (self.request.major_version == 1 and self.request.minor_version < 3) ) ): return serializers.TagSerializer return super().get_serializer_class()
class SiteViewSet(StatusViewSetMixin, CustomFieldModelViewSet): queryset = Site.objects.prefetch_related( "region", "status", "tenant", "tags").annotate( device_count=count_related(Device, "site"), rack_count=count_related(Rack, "site"), prefix_count=count_related(Prefix, "site"), vlan_count=count_related(VLAN, "site"), circuit_count=count_related(Circuit, "terminations__site"), virtualmachine_count=count_related(VirtualMachine, "cluster__site"), ) serializer_class = serializers.SiteSerializer filterset_class = filters.SiteFilterSet
class ProviderBulkDeleteView(generic.BulkDeleteView): queryset = Provider.objects.annotate( count_circuits=count_related(Circuit, "provider")) filterset = filters.ProviderFilterSet table = tables.ProviderTable
class ProviderListView(generic.ObjectListView): queryset = Provider.objects.annotate( count_circuits=count_related(Circuit, "provider")) filterset = filters.ProviderFilterSet filterset_form = forms.ProviderFilterForm table = tables.ProviderTable
class PowerPanelViewSet(ModelViewSet): queryset = PowerPanel.objects.prefetch_related( "site", "rack_group").annotate( powerfeed_count=count_related(PowerFeed, "power_panel")) serializer_class = serializers.PowerPanelSerializer filterset_class = filters.PowerPanelFilterSet
class ClusterGroupViewSet(CustomFieldModelViewSet): queryset = ClusterGroup.objects.annotate(cluster_count=count_related(Cluster, "group")) serializer_class = serializers.ClusterGroupSerializer filterset_class = filters.ClusterGroupFilterSet
class ClusterTypeViewSet(CustomFieldModelViewSet): queryset = ClusterType.objects.annotate(cluster_count=count_related(Cluster, "type")) serializer_class = serializers.ClusterTypeSerializer filterset_class = filters.ClusterTypeFilterSet
class VLANGroupBulkDeleteView(generic.BulkDeleteView): queryset = VLANGroup.objects.prefetch_related("site").annotate(vlan_count=count_related(VLAN, "group")) filterset = filters.VLANGroupFilterSet table = tables.VLANGroupTable
class RIRViewSet(CustomFieldModelViewSet): queryset = RIR.objects.annotate( aggregate_count=count_related(Aggregate, "rir")) serializer_class = serializers.RIRSerializer filterset_class = filters.RIRFilterSet
class RoleListView(generic.ObjectListView): queryset = Role.objects.annotate( prefix_count=count_related(Prefix, "role"), vlan_count=count_related(VLAN, "role"), ) table = tables.RoleTable
class RIRBulkDeleteView(generic.BulkDeleteView): queryset = RIR.objects.annotate(aggregate_count=count_related(Aggregate, "rir")) filterset = filters.RIRFilterSet table = tables.RIRTable
class ClusterGroupListView(generic.ObjectListView): queryset = ClusterGroup.objects.annotate( cluster_count=count_related(Cluster, "group")) table = tables.ClusterGroupTable
class ClusterTypeBulkDeleteView(generic.BulkDeleteView): queryset = ClusterType.objects.annotate( cluster_count=count_related(Cluster, "type")) table = tables.ClusterTypeTable
class CircuitTypeListView(generic.ObjectListView): queryset = CircuitType.objects.annotate( circuit_count=count_related(Circuit, "type")) table = tables.CircuitTypeTable
class VLANGroupListView(generic.ObjectListView): queryset = VLANGroup.objects.prefetch_related("site").annotate(vlan_count=count_related(VLAN, "group")) filterset = filters.VLANGroupFilterSet filterset_form = forms.VLANGroupFilterForm table = tables.VLANGroupTable
class TagViewSet(CustomFieldModelViewSet): queryset = Tag.objects.annotate( tagged_items=count_related(TaggedItem, "tag")) serializer_class = serializers.TagSerializer filterset_class = filters.TagFilterSet
class VLANGroupViewSet(CustomFieldModelViewSet): queryset = VLANGroup.objects.prefetch_related("site").annotate( vlan_count=count_related(VLAN, "group")) serializer_class = serializers.VLANGroupSerializer filterset_class = filters.VLANGroupFilterSet