diff --git a/zds/tutorialv2/managers.py b/zds/tutorialv2/managers.py index 506d3855f1..d0ad00e11f 100644 --- a/zds/tutorialv2/managers.py +++ b/zds/tutorialv2/managers.py @@ -2,6 +2,8 @@ from django.conf import settings from django.db import models +from zds.utils.models import Tag +from django.db.models import Count class PublishedContentManager(models.Manager): @@ -43,6 +45,20 @@ def get_contents_count(self): return self.filter(must_redirect=False)\ .count() + def get_top_tags(self, displayed_types, limit=-1): + published = self.filter( + must_redirect=False, + content__type__in=displayed_types).values('content__tags').distinct() + tags_pk = [tag['content__tags'] for tag in published] + queryset = Tag.objects\ + .filter(pk__in=tags_pk, publishablecontent__public_version__isnull=False, + publishablecontent__type__in=displayed_types)\ + .annotate(num_content=Count('publishablecontent'))\ + .order_by('-num_content', 'title') + if limit > 0: + queryset = queryset[:limit] + return queryset + class PublishableContentManager(models.Manager): """...""" diff --git a/zds/tutorialv2/views/views_published.py b/zds/tutorialv2/views/views_published.py index 880b15db7e..e71a00eb45 100644 --- a/zds/tutorialv2/views/views_published.py +++ b/zds/tutorialv2/views/views_published.py @@ -6,7 +6,6 @@ from django.contrib.auth.models import User from django.core.exceptions import PermissionDenied from django.db import transaction -from django.db.models import Count from django.http import Http404, HttpResponsePermanentRedirect, StreamingHttpResponse, HttpResponse from django.shortcuts import get_object_or_404, redirect, render_to_response from django.template.loader import render_to_string @@ -677,13 +676,4 @@ class TagsListView(ListView): displayed_types = ["TUTORIAL", "ARTICLE"] def get_queryset(self): - published = PublishedContent.objects.filter( - must_redirect=False, - content__type__in=self.displayed_types).values('content__tags').distinct() - tags_pk = [tag['content__tags'] for tag in published] - queryset = Tag.objects\ - .filter(pk__in=tags_pk, publishablecontent__public_version__isnull=False, - publishablecontent__type__in=self.displayed_types)\ - .annotate(num_content=Count('publishablecontent'))\ - .order_by('-num_content', 'title') - return queryset + return PublishedContent.objects.get_top_tags(self.displayed_types) diff --git a/zds/utils/templatetags/topbar.py b/zds/utils/templatetags/topbar.py index c8832f5a0c..b229738320 100644 --- a/zds/utils/templatetags/topbar.py +++ b/zds/utils/templatetags/topbar.py @@ -93,25 +93,9 @@ def top_categories_content(_type): else: cats[key] = [(csc['subcategory__title'], csc['subcategory__slug'])] - # get all tags from contents - tmp_tags = (PublishedContent.objects - .filter(must_redirect=False) - .values_list('content__tags__pk', flat=True) - .select_related('content') - .distinct() - .filter(content__tags__isnull=False) - [:settings.ZDS_APP['forum']['top_tag_max'] + len(settings.ZDS_APP['forum']['top_tag_exclu'])]) - - tags_not_filtered = Tag.objects.filter(pk__in=[pk for pk in tmp_tags]) - - # select tags that are not in the excluded list - tags = [] - for tag in tags_not_filtered: - if tag.title not in settings.ZDS_APP['forum']['top_tag_exclu'] \ - and len(tags) <= settings.ZDS_APP['forum']['top_tag_max']: - tags.append(tag) - - return {"tags": tags, "categories": cats} + return {"tags": PublishedContent.objects.get_top_tags(["TUTORIAL", "ARTICLE"], + limit=settings.ZDS_APP['forum']['top_tag_max']), + "categories": cats} @register.filter('auth_forum')