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.name }}

- {% if tag.description %} -

{{ tag.description }}

- {% endif %} -
-
- - {% trans "Edit" %} - - - {% trans "Delete" %} - +
+ +
+
+
+

{{ tag.name }}

+ {% if tag.description %} +

{{ tag.description }}

+ {% endif %} +
+ {% trans "Used" %}: {{ total_usage_count }} {% trans "times" %} +
+
+
-
-

{% trans "Links with this tag" %}

- {% if links %} -
- {% for link in links %} -
-

- - {{ link.alias }} - -

- {% if link.description %} -

{{ link.description }}

- {% endif %} -
- {% trans "Created" %}: {{ link.created_at|date:"Y-m-d" }} + +
+ +
+ + +
+ + + + + + + + + + + - {% else %} -

{% trans "No links found with this tag." %}

- {% endif %}
+ + + {% endblock %} diff --git a/links/templates/links/tag_list.html b/links/templates/links/tag_list.html index 2f4ea04..58cb893 100644 --- a/links/templates/links/tag_list.html +++ b/links/templates/links/tag_list.html @@ -10,23 +10,43 @@
-
+
{% for tag in tags %} -
-

- {{ tag.name }} -

- {% if tag.description %} -

{{ tag.description }}

- {% endif %} -
- {% with link_count=tag.links.count %} - {{ link_count }} {% if link_count == 1 %}{% trans "link" %}{% else %}{% trans "links" %}{% endif %} +
+
+ {% with icon_classes="w-8 h-8 mx-auto" %} + {% if forloop.counter|divisibleby:5 %} + + + + {% elif forloop.counter|divisibleby:4 %} + + + + {% elif forloop.counter|divisibleby:3 %} + + + + {% elif forloop.counter|divisibleby:2 %} + + + + {% else %} + + + + {% endif %} {% endwith %}
+

{{ tag.name }}

+
+ + {{ tag.total_count }} {% trans "uses" %} + +
- +
{% empty %}
{% trans "No tags found." %} @@ -55,28 +75,42 @@