diff --git a/netbox/dcim/views.py b/netbox/dcim/views.py index 794d58d1e23..46d12937b03 100644 --- a/netbox/dcim/views.py +++ b/netbox/dcim/views.py @@ -21,7 +21,9 @@ from utilities.permissions import get_permission_for_model from utilities.utils import count_related from utilities.views import GetReturnURLMixin, ObjectPermissionRequiredMixin, ViewTab, register_model_view +from virtualization.filtersets import VirtualMachineFilterSet from virtualization.models import VirtualMachine +from virtualization.tables import VirtualMachineTable from . import filtersets, forms, tables from .choices import DeviceFaceChoices from .constants import NONCONNECTABLE_IFACE_TYPES @@ -1736,6 +1738,42 @@ def get_extra_context(self, request, instance): } +@register_model_view(DeviceRole, 'devices', path='devices') +class DeviceRoleDevicesView(generic.ObjectChildrenView): + queryset = DeviceRole.objects.all() + child_model = Device + table = tables.DeviceTable + filterset = filtersets.DeviceFilterSet + template_name = 'dcim/devicerole/devices.html' + tab = ViewTab( + label=_('Devices'), + badge=lambda obj: obj.devices.count(), + permission='dcim.view_device', + weight=400 + ) + + def get_children(self, request, parent): + return Device.objects.restrict(request.user, 'view').filter(device_role=parent) + + +@register_model_view(DeviceRole, 'virtual_machines', path='virtual-machines') +class DeviceRoleVirtualMachinesView(generic.ObjectChildrenView): + queryset = DeviceRole.objects.all() + child_model = VirtualMachine + table = VirtualMachineTable + filterset = VirtualMachineFilterSet + template_name = 'dcim/devicerole/virtual_machines.html' + tab = ViewTab( + label=_('Virtual machines'), + badge=lambda obj: obj.virtual_machines.count(), + permission='virtualization.view_virtualmachine', + weight=500 + ) + + def get_children(self, request, parent): + return VirtualMachine.objects.restrict(request.user, 'view').filter(role=parent) + + @register_model_view(DeviceRole, 'edit') class DeviceRoleEditView(generic.ObjectEditView): queryset = DeviceRole.objects.all() diff --git a/netbox/templates/dcim/devicerole.html b/netbox/templates/dcim/devicerole.html index 610c53071a4..6724333d91b 100644 --- a/netbox/templates/dcim/devicerole.html +++ b/netbox/templates/dcim/devicerole.html @@ -71,13 +71,6 @@