Update campaign count handling in channel list view and template

This commit is contained in:
Joakim Hellsén 2026-01-07 21:13:19 +01:00
commit 2f20bb265d
No known key found for this signature in database
2 changed files with 13 additions and 7 deletions

View file

@ -38,7 +38,7 @@
href="{% url 'twitch:channel_detail' channel.twitch_id %}">{{ channel.display_name }}</a> href="{% url 'twitch:channel_detail' channel.twitch_id %}">{{ channel.display_name }}</a>
</td> </td>
<td>{{ channel.name }}</td> <td>{{ channel.name }}</td>
<td>{{ channel.campaign_count }}</td> <td>{{ channel.campaign_count|default:0 }}</td>
<td> <td>
<time datetime="{{ channel.added_at|date:'c' }}" <time datetime="{{ channel.added_at|date:'c' }}"
title="{{ channel.added_at|date:'DATETIME_FORMAT' }}"> title="{{ channel.added_at|date:'DATETIME_FORMAT' }}">

View file

@ -14,8 +14,10 @@ from django.core.paginator import Paginator
from django.core.serializers import serialize from django.core.serializers import serialize
from django.db.models import Count from django.db.models import Count
from django.db.models import F from django.db.models import F
from django.db.models import OuterRef
from django.db.models import Prefetch from django.db.models import Prefetch
from django.db.models import Q from django.db.models import Q
from django.db.models import Subquery
from django.db.models.functions import Trim from django.db.models.functions import Trim
from django.db.models.query import QuerySet from django.db.models.query import QuerySet
from django.http import Http404 from django.http import Http404
@ -827,13 +829,17 @@ class ChannelListView(ListView):
search_query: str | None = self.request.GET.get("search") search_query: str | None = self.request.GET.get("search")
if search_query: if search_query:
queryset = queryset.filter( queryset = queryset.filter(Q(name__icontains=search_query) | Q(display_name__icontains=search_query))
Q(name__icontains=search_query) | Q(display_name__icontains=search_query),
)
return queryset.annotate( campaign_count_subquery: QuerySet[DropCampaign, dict[str, Any]] = (
campaign_count=Count("allowed_campaigns", distinct=True), DropCampaign.objects
).order_by("-campaign_count", "name") .filter(allow_channels=OuterRef("pk"))
.values("allow_channels")
.annotate(count=Count("pk"))
.values("count")
)
return queryset.annotate(campaign_count=Subquery(campaign_count_subquery)).order_by("-campaign_count", "name")
def get_context_data(self, **kwargs) -> dict[str, Any]: def get_context_data(self, **kwargs) -> dict[str, Any]:
"""Add additional context data. """Add additional context data.