Просмотр исходного кода

Closes #6210: Include child locations on location view

jeremystretch 4 лет назад
Родитель
Сommit
4e405ce530
3 измененных файлов с 31 добавлено и 16 удалено
  1. 1 0
      docs/release-notes/version-2.11.md
  2. 22 8
      netbox/dcim/views.py
  3. 8 8
      netbox/templates/dcim/location.html

+ 1 - 0
docs/release-notes/version-2.11.md

@@ -7,6 +7,7 @@
 * [#6179](https://github.com/netbox-community/netbox/issues/6179) - Enable natural ordering for virtual machines
 * [#6189](https://github.com/netbox-community/netbox/issues/6189) - Add ability to search for locations by name or description
 * [#6190](https://github.com/netbox-community/netbox/issues/6190) - Allow filtering devices with no location assigned
+* [#6210](https://github.com/netbox-community/netbox/issues/6210) - Include child locations on location view
 
 ### Bug Fixes
 

+ 22 - 8
netbox/dcim/views.py

@@ -364,16 +364,30 @@ class LocationView(generic.ObjectView):
     queryset = Location.objects.all()
 
     def get_extra_context(self, request, instance):
-        devices = Device.objects.restrict(request.user, 'view').filter(
-            location=instance
-        )
-
-        devices_table = tables.DeviceTable(devices)
-        devices_table.columns.hide('location')
-        paginate_table(devices_table, request)
+        location_ids = instance.get_descendants(include_self=True).values_list('pk', flat=True)
+        rack_count = Rack.objects.filter(location__in=location_ids).count()
+        device_count = Device.objects.filter(location__in=location_ids).count()
+
+        child_locations = Location.objects.add_related_count(
+            Location.objects.add_related_count(
+                Location.objects.all(),
+                Device,
+                'location',
+                'device_count',
+                cumulative=True
+            ),
+            Rack,
+            'location',
+            'rack_count',
+            cumulative=True
+        ).filter(pk__in=location_ids).exclude(pk=instance.pk)
+        child_locations_table = tables.LocationTable(child_locations)
+        paginate_table(child_locations_table, request)
 
         return {
-            'devices_table': devices_table,
+            'rack_count': rack_count,
+            'device_count': device_count,
+            'child_locations_table': child_locations_table,
         }
 
 

+ 8 - 8
netbox/templates/dcim/location.html

@@ -43,13 +43,13 @@
         <tr>
           <td>Racks</td>
           <td>
-            <a href="{% url 'dcim:rack_list' %}?location_id={{ object.pk }}">{{ object.racks.count }}</a>
+            <a href="{% url 'dcim:rack_list' %}?location_id={{ object.pk }}">{{ rack_count }}</a>
           </td>
         </tr>
         <tr>
           <td>Devices</td>
           <td>
-            <a href="{% url 'dcim:device_list' %}?location_id={{ object.pk }}">{{ devices_table.rows|length }}</a>
+            <a href="{% url 'dcim:device_list' %}?location_id={{ object.pk }}">{{ device_count }}</a>
           </td>
         </tr>
       </table>
@@ -79,18 +79,18 @@
 	<div class="col-md-12">
     <div class="panel panel-default">
       <div class="panel-heading">
-        <strong>Devices</strong>
+        <strong>Locations</strong>
       </div>
-      {% include 'inc/table.html' with table=devices_table %}
-      {% if perms.dcim.add_device %}
+      {% include 'inc/table.html' with table=child_locations_table %}
+      {% if perms.dcim.add_location %}
         <div class="panel-footer text-right noprint">
-          <a href="{% url 'dcim:device_add' %}?location={{ object.pk }}" class="btn btn-xs btn-primary">
-            <span class="mdi mdi-plus-thick" aria-hidden="true"></span> Add device
+          <a href="{% url 'dcim:location_add' %}?site={{ object.site.pk }}&parent={{ object.pk }}" class="btn btn-xs btn-primary">
+            <span class="mdi mdi-plus-thick" aria-hidden="true"></span> Add location
           </a>
         </div>
       {% endif %}
       </div>
-      {% include 'inc/paginator.html' with paginator=devices_table.paginator page=devices_table.page %}
+      {% include 'inc/paginator.html' with paginator=child_locations_table.paginator page=child_locations_table.page %}
       {% plugin_full_width_page object %}
   </div>
 </div>