Skip to content

Commit

Permalink
added device and vm tab on device role (#11500)
Browse files Browse the repository at this point in the history
* added vm tab on device role

* added blank lines

* updated templates

* fixed lint issues
  • Loading branch information
abhi1693 authored Jan 20, 2023
1 parent a1c1b19 commit ddd4f80
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 7 deletions.
38 changes: 38 additions & 0 deletions netbox/dcim/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
7 changes: 0 additions & 7 deletions netbox/templates/dcim/devicerole.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,6 @@ <h5 class="card-header">
</div>
<div class="row mb-3">
<div class="col col-md-12">
<div class="card">
<h5 class="card-header">Devices</h5>
<div class="card-body table-responsive">
{% render_table devices_table 'inc/table.html' %}
{% include 'inc/paginator.html' with paginator=devices_table.paginator page=devices_table.page %}
</div>
</div>
{% plugin_full_width_page object %}
</div>
</div>
Expand Down
20 changes: 20 additions & 0 deletions netbox/templates/dcim/devicerole/devices.html
Original file line number Diff line number Diff line change
@@ -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' %}
<form method="post">
{% csrf_token %}
<div class="card">
<div class="card-body" id="object_list">
{% include 'htmx/table.html' %}
</div>
</div>
</form>
{% endblock content %}

{% block modals %}
{{ block.super }}
{% table_config_form table %}
{% endblock modals %}
20 changes: 20 additions & 0 deletions netbox/templates/dcim/devicerole/virtual_machines.html
Original file line number Diff line number Diff line change
@@ -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' %}
<form method="post">
{% csrf_token %}
<div class="card">
<div class="card-body" id="object_list">
{% include 'htmx/table.html' %}
</div>
</div>
</form>
{% endblock content %}

{% block modals %}
{{ block.super }}
{% table_config_form table %}
{% endblock modals %}

0 comments on commit ddd4f80

Please sign in to comment.