diff --git a/links/tag_views.py b/links/tag_views.py index 9857bd8..7d0f90c 100644 --- a/links/tag_views.py +++ b/links/tag_views.py @@ -4,12 +4,32 @@ from django.contrib import messages from django.shortcuts import get_object_or_404 from .models import Tag from .forms import TagForm +from django.db.models import Count class TagListView(ListView): model = Tag context_object_name = 'tags' template_name = 'links/tag_list.html' paginate_by = 20 + + def get_queryset(self): + queryset = super().get_queryset() + # Annotate each tag with its usage count + queryset = queryset.annotate( + links_count=Count('links', distinct=True), + pages_count=Count('pages', distinct=True), + posts_count=Count('posts', distinct=True), + image_collections_count=Count('image_collections', distinct=True), + ) + # Add a total_count attribute to each tag + for tag in queryset: + tag.total_count = ( + tag.links_count + + tag.pages_count + + tag.posts_count + + tag.image_collections_count + ) + return queryset class TagDetailView(DetailView): model = Tag @@ -19,7 +39,20 @@ class TagDetailView(DetailView): def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) + # Get all resource types that have this tag context['links'] = self.object.links.all() + context['pages'] = self.object.pages.all() + context['posts'] = self.object.posts.all() + context['image_collections'] = self.object.image_collections.all() + + # Calculate total usage count + total_count = ( + context['links'].count() + + context['pages'].count() + + context['posts'].count() + + context['image_collections'].count() + ) + context['total_usage_count'] = total_count return context class TagCreateView(CreateView): diff --git a/links/templates/links/tag_detail.html b/links/templates/links/tag_detail.html index 183e7b9..9d95e72 100644 --- a/links/templates/links/tag_detail.html +++ b/links/templates/links/tag_detail.html @@ -3,48 +3,216 @@ {% block content %}
{{ tag.description }}
- {% endif %} -{{ tag.description }}
+ {% endif %} +{{ link.description }}
- {% endif %} -{{ link.description }}
+ {% endif %} +{% trans "No links found with this tag." %}
+{% trans "No links found with this tag." %}
- {% endif %}