Skip to content

Commit

Permalink
Limit amount of nonracked devices displayed
Browse files Browse the repository at this point in the history
Fixes netbox-community#8920

Limits the amount of non-racked devices on Site and Location view to 10 and provides a link to the device list this is pre-filtered to the relevant site or location.
  • Loading branch information
Jason Yates committed Apr 7, 2022
1 parent b9f6a56 commit c3d9910
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 29 deletions.
18 changes: 16 additions & 2 deletions netbox/dcim/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,13 @@ def get_extra_context(self, request, instance):
site=instance,
position__isnull=True,
parent_bay__isnull=True
).prefetch_related('device_type__manufacturer')
).prefetch_related('device_type__manufacturer').order_by('-pk')[:10]

total_nonracked_devices_count = Device.objects.filter(
site=instance,
position__isnull=True,
parent_bay__isnull=True
).count()

asns = ASN.objects.restrict(request.user, 'view').filter(sites=instance)
asn_count = asns.count()
Expand All @@ -358,6 +364,7 @@ def get_extra_context(self, request, instance):
'locations': locations,
'asns': asns,
'nonracked_devices': nonracked_devices,
'total_nonracked_devices_count': total_nonracked_devices_count,
}


Expand Down Expand Up @@ -439,13 +446,20 @@ def get_extra_context(self, request, instance):
location=instance,
position__isnull=True,
parent_bay__isnull=True
).prefetch_related('device_type__manufacturer')
).prefetch_related('device_type__manufacturer').order_by('-pk')[:10]

total_nonracked_devices_count = Device.objects.filter(
location=instance,
position__isnull=True,
parent_bay__isnull=True
).count()

return {
'rack_count': rack_count,
'device_count': device_count,
'child_locations_table': child_locations_table,
'nonracked_devices': nonracked_devices,
'total_nonracked_devices_count': total_nonracked_devices_count,
}


Expand Down
68 changes: 41 additions & 27 deletions netbox/templates/dcim/inc/nonracked_devices.html
Original file line number Diff line number Diff line change
@@ -1,40 +1,54 @@
{% load helpers %}

<div class="card">
<h5 class="card-header">
Non-Racked Devices
</h5>
<div class="card-body">
{% if nonracked_devices %}
<table class="table table-hover">
<tr>
<th>Name</th>
<th>Role</th>
<th>Type</th>
<th colspan="2">Parent Device</th>
</tr>
{% for device in nonracked_devices %}
<tr{% if device.device_type.u_height %} class="warning"{% endif %}>
<td>
<a href="{% url 'dcim:device' pk=device.pk %}">{{ device }}</a>
</td>
<td>{{ device.device_role }}</td>
<td>{{ device.device_type }}</td>
{% if device.parent_bay %}
<td>{{ device.parent_bay.device|linkify }}</td>
<td>{{ device.parent_bay }}</td>
{% else %}
<td colspan="2" class="text-muted">&mdash;</td>
<h5 class="card-header">
Non-Racked Devices
</h5>
<div class="card-body">
{% if nonracked_devices %}
<table class="table table-hover">
<tr>
<th>Name</th>
<th>Role</th>
<th>Type</th>
<th colspan="2">Parent Device</th>
</tr>
{% for device in nonracked_devices %}
<tr{% if device.device_type.u_height %} class="warning"{% endif %}>
<td>
<a href="{% url 'dcim:device' pk=device.pk %}">{{ device }}</a>
</td>
<td>{{ device.device_role }}</td>
<td>{{ device.device_type }}</td>
{% if device.parent_bay %}
<td>{{ device.parent_bay.device|linkify }}</td>
<td>{{ device.parent_bay }}</td>
{% else %}
<td colspan="2" class="text-muted">&mdash;</td>
{% endif %}
</tr>
{% endfor %}
</table>

{% if nonracked_devices.count == 10 %}
{% if object|meta:'verbose_name' == 'site' %}
<div class="text-muted">
Displaying 10 of {{ total_nonracked_devices_count }} devices (<a href="{% url 'dcim:device_list' %}?site_id={{ object.pk }}&rack_id=null">View full list</a>)
</div>
{% elif object|meta:'verbose_name' == 'location' %}
<div class="text-muted">
Displaying 10 of {{ total_nonracked_devices_count }} devices (<a href="{% url 'dcim:device_list' %}?location_id={{ object.pk }}&rack_id=null">View full list</a>)
</div>
{% endif %}
</tr>
{% endfor %}
</table>
{% endif %}

{% else %}
<div class="text-muted">
None
</div>
{% endif %}
</div>

{% if perms.dcim.add_device %}
{% if object|meta:'verbose_name' == 'rack' %}
<div class="card-footer text-end noprint">
Expand Down

0 comments on commit c3d9910

Please sign in to comment.