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

Add tagged items count to tag view

jeremystretch 4 лет назад
Родитель
Сommit
aa2beb1d78
2 измененных файлов с 31 добавлено и 3 удалено
  1. 9 2
      netbox/extras/views.py
  2. 22 1
      netbox/templates/extras/tag.html

+ 9 - 2
netbox/extras/views.py

@@ -1,7 +1,6 @@
-from django import template
 from django.contrib import messages
 from django.contrib.contenttypes.models import ContentType
-from django.db.models import Q
+from django.db.models import Count, Q
 from django.http import Http404, HttpResponseForbidden
 from django.shortcuts import get_object_or_404, redirect, render
 from django.urls import reverse
@@ -45,9 +44,17 @@ class TagView(generic.ObjectView):
         )
         paginate_table(taggeditem_table, request)
 
+        object_types = [
+            {
+                'content_type': ContentType.objects.get(pk=ti['content_type']),
+                'item_count': ti['item_count']
+            } for ti in tagged_items.values('content_type').annotate(item_count=Count('pk'))
+        ]
+
         return {
             'taggeditem_table': taggeditem_table,
             'tagged_item_count': tagged_items.count(),
+            'object_types': object_types,
         }
 
 

+ 22 - 1
netbox/templates/extras/tag.html

@@ -40,6 +40,27 @@
     {% plugin_left_page object %}
   </div>
 	<div class="col-md-6">
+    <div class="panel panel-default">
+      <div class="panel-heading">
+        <strong>Tagged Item Types</strong>
+      </div>
+      <table class="table table-hover panel-body">
+        {% for object_type in object_types %}
+          <tr>
+            <td>{{ object_type.content_type.name|bettertitle }}</td>
+            <td>
+              {% with viewname=object_type.content_type.model_class|validated_viewname:"list" %}
+                {% if viewname %}
+                  <a href="{% url viewname %}?tag={{ object.slug }}">{{ object_type.item_count }}</a>
+                {% else %}
+                  {{ object_type.item_count }}
+                {% endif %}
+              {% endwith %}
+            </td>
+          </tr>
+        {% endfor %}
+      </table>
+    </div>
     {% plugin_right_page object %}
 	</div>
 </div>
@@ -47,7 +68,7 @@
 	<div class="col-md-12">
     <div class="panel panel-default">
       <div class="panel-heading">
-        <strong>Device Types</strong>
+        <strong>Tagged Items</strong>
       </div>
       {% include 'inc/table.html' with table=taggeditem_table %}
     </div>