Update campaign count handling in channel list view and template
This commit is contained in:
parent
bd16fefd08
commit
2f20bb265d
2 changed files with 13 additions and 7 deletions
|
|
@ -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' }}">
|
||||||
|
|
|
||||||
|
|
@ -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),
|
|
||||||
|
campaign_count_subquery: QuerySet[DropCampaign, dict[str, Any]] = (
|
||||||
|
DropCampaign.objects
|
||||||
|
.filter(allow_channels=OuterRef("pk"))
|
||||||
|
.values("allow_channels")
|
||||||
|
.annotate(count=Count("pk"))
|
||||||
|
.values("count")
|
||||||
)
|
)
|
||||||
|
|
||||||
return queryset.annotate(
|
return queryset.annotate(campaign_count=Subquery(campaign_count_subquery)).order_by("-campaign_count", "name")
|
||||||
campaign_count=Count("allowed_campaigns", distinct=True),
|
|
||||||
).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.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue