From ddd4f805a532d1071360ea77df9ad7877f9ddc16 Mon Sep 17 00:00:00 2001 From: Abhimanyu Saharan Date: Fri, 20 Jan 2023 20:00:18 +0530 Subject: [PATCH] added device and vm tab on device role (#11500) * added vm tab on device role * added blank lines * updated templates * fixed lint issues --- netbox/dcim/views.py | 38 +++++++++++++++++++ netbox/templates/dcim/devicerole.html | 7 ---- netbox/templates/dcim/devicerole/devices.html | 20 ++++++++++ .../dcim/devicerole/virtual_machines.html | 20 ++++++++++ 4 files changed, 78 insertions(+), 7 deletions(-) create mode 100644 netbox/templates/dcim/devicerole/devices.html create mode 100644 netbox/templates/dcim/devicerole/virtual_machines.html 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 @@
-
-
Devices
-
- {% render_table devices_table 'inc/table.html' %} - {% include 'inc/paginator.html' with paginator=devices_table.paginator page=devices_table.page %} -
-
{% plugin_full_width_page object %}
diff --git a/netbox/templates/dcim/devicerole/devices.html b/netbox/templates/dcim/devicerole/devices.html new file mode 100644 index 00000000000..d7a69f013fa --- /dev/null +++ b/netbox/templates/dcim/devicerole/devices.html @@ -0,0 +1,20 @@ +{% extends 'dcim/devicerole.html' %} +{% load helpers %} +{% load render_table from django_tables2 %} + +{% block content %} + {% include 'inc/table_controls_htmx.html' with table_modal='DeviceTable_config' %} +
+ {% csrf_token %} +
+
+ {% include 'htmx/table.html' %} +
+
+
+{% endblock content %} + +{% block modals %} + {{ block.super }} + {% table_config_form table %} +{% endblock modals %} \ No newline at end of file diff --git a/netbox/templates/dcim/devicerole/virtual_machines.html b/netbox/templates/dcim/devicerole/virtual_machines.html new file mode 100644 index 00000000000..d94e868394b --- /dev/null +++ b/netbox/templates/dcim/devicerole/virtual_machines.html @@ -0,0 +1,20 @@ +{% extends 'dcim/devicerole.html' %} +{% load helpers %} +{% load render_table from django_tables2 %} + +{% block content %} + {% include 'inc/table_controls_htmx.html' with table_modal='VirtualMachineTable_config' %} +
+ {% csrf_token %} +
+
+ {% include 'htmx/table.html' %} +
+
+
+{% endblock content %} + +{% block modals %} + {{ block.super }} + {% table_config_form table %} +{% endblock modals %} \ No newline at end of file