Add tagged items count to tag view

This commit is contained in:
jeremystretch 2021-04-05 13:53:25 -04:00
parent 2e07ac5a47
commit aa2beb1d78
2 changed files with 31 additions and 3 deletions

View File

@ -1,7 +1,6 @@
from django import template
from django.contrib import messages from django.contrib import messages
from django.contrib.contenttypes.models import ContentType 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.http import Http404, HttpResponseForbidden
from django.shortcuts import get_object_or_404, redirect, render from django.shortcuts import get_object_or_404, redirect, render
from django.urls import reverse from django.urls import reverse
@ -45,9 +44,17 @@ class TagView(generic.ObjectView):
) )
paginate_table(taggeditem_table, request) 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 { return {
'taggeditem_table': taggeditem_table, 'taggeditem_table': taggeditem_table,
'tagged_item_count': tagged_items.count(), 'tagged_item_count': tagged_items.count(),
'object_types': object_types,
} }

View File

@ -40,6 +40,27 @@
{% plugin_left_page object %} {% plugin_left_page object %}
</div> </div>
<div class="col-md-6"> <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 %} {% plugin_right_page object %}
</div> </div>
</div> </div>
@ -47,7 +68,7 @@
<div class="col-md-12"> <div class="col-md-12">
<div class="panel panel-default"> <div class="panel panel-default">
<div class="panel-heading"> <div class="panel-heading">
<strong>Device Types</strong> <strong>Tagged Items</strong>
</div> </div>
{% include 'inc/table.html' with table=taggeditem_table %} {% include 'inc/table.html' with table=taggeditem_table %}
</div> </div>