Location model cleanup

This commit is contained in:
jeremystretch 2021-04-05 11:04:12 -04:00
parent 4883bc3dd4
commit 7b8bd2d4ce
6 changed files with 34 additions and 14 deletions

View File

@ -134,12 +134,13 @@ class LocationSerializer(NestedGroupModelSerializer):
site = NestedSiteSerializer()
parent = NestedLocationSerializer(required=False, allow_null=True)
rack_count = serializers.IntegerField(read_only=True)
device_count = serializers.IntegerField(read_only=True)
class Meta:
model = Location
fields = [
'id', 'url', 'display', 'name', 'slug', 'site', 'parent', 'description', 'custom_fields', 'created',
'last_updated', 'rack_count', '_depth',
'last_updated', 'rack_count', 'device_count', '_depth',
]

View File

@ -146,7 +146,13 @@ class SiteViewSet(CustomFieldModelViewSet):
class LocationViewSet(CustomFieldModelViewSet):
queryset = Location.objects.add_related_count(
Location.objects.all(),
Location.objects.add_related_count(
Location.objects.all(),
Device,
'location',
'device_count',
cumulative=True
),
Rack,
'location',
'rack_count',
@ -174,7 +180,7 @@ class RackRoleViewSet(CustomFieldModelViewSet):
class RackViewSet(CustomFieldModelViewSet):
queryset = Rack.objects.prefetch_related(
'site', 'location__site', 'role', 'tenant', 'tags'
'site', 'location', 'role', 'tenant', 'tags'
).annotate(
device_count=count_related(Device, 'rack'),
powerfeed_count=count_related(PowerFeed, 'rack')

View File

@ -315,6 +315,7 @@ class Location(NestedGroupModel):
)
csv_headers = ['site', 'parent', 'name', 'slug', 'description']
clone_fields = ['site', 'parent', 'description']
class Meta:
ordering = ['site', 'name']

View File

@ -272,6 +272,13 @@ class SiteView(generic.ObjectView):
'location',
'rack_count',
cumulative=True
)
locations = Location.objects.add_related_count(
locations,
Device,
'location',
'device_count',
cumulative=True
).restrict(request.user, 'view').filter(site=instance)
return {

View File

@ -26,6 +26,10 @@
<td>Description</td>
<td>{{ object.description|placeholder }}</td>
</tr>
<tr>
<td>Site</td>
<td><a href="{{ object.site.get_absolute_url }}">{{ object.site }}</a></td>
</tr>
<tr>
<td>Parent</td>
<td>

View File

@ -210,12 +210,22 @@
<strong>Locations</strong>
</div>
<table class="table table-hover panel-body">
<tr>
<th>Location</th>
<th>Racks</th>
<th>Devices</th>
<th></th>
</tr>
{% for location in locations %}
<tr>
<td style="padding-left: {{ location.level }}8px">
<i class="mdi mdi-folder-open"></i> <a href="{{ location.get_absolute_url }}">{{ location }}</a>
<i class="mdi mdi-folder-open-outline"></i> <a href="{{ location.get_absolute_url }}">{{ location }}</a>
</td>
<td>
<a href="{% url 'dcim:rack_list' %}?location_id={{ location.pk }}">{{ location.rack_count }}</a></td>
<td>
<a href="{% url 'dcim:device_list' %}?location_id={{ location.pk }}">{{ location.device_count }}</a>
</td>
<td>{{ location.rack_count }}</td>
<td class="text-right noprint">
<a href="{% url 'dcim:rack_elevation_list' %}?location_id={{ location.pk }}" class="btn btn-xs btn-primary" title="View elevations">
<i class="mdi mdi-server"></i>
@ -223,15 +233,6 @@
</td>
</tr>
{% endfor %}
<tr>
<td><i class="mdi mdi-folder-open"></i> All racks</td>
<td>{{ stats.rack_count }}</td>
<td class="text-right noprint">
<a href="{% url 'dcim:rack_elevation_list' %}?site_id={{ object.pk }}" class="btn btn-xs btn-primary" title="View elevations">
<i class="mdi mdi-server"></i>
</a>
</td>
</tr>
</table>
</div>
<div class="panel panel-default">