From ae4407a624237a90d6d583c9854ec362cc267c2f Mon Sep 17 00:00:00 2001 From: artragis Date: Fri, 29 Apr 2016 13:27:03 +0200 Subject: [PATCH 1/7] =?UTF-8?q?rend=20la=20topbar=20coh=C3=A9rente?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- zds/tutorialv2/managers.py | 14 ++++++++++++++ zds/tutorialv2/views/views_published.py | 13 +------------ zds/utils/templatetags/topbar.py | 21 ++------------------- 3 files changed, 17 insertions(+), 31 deletions(-) diff --git a/zds/tutorialv2/managers.py b/zds/tutorialv2/managers.py index 506d3855f1..d81cb506f2 100644 --- a/zds/tutorialv2/managers.py +++ b/zds/tutorialv2/managers.py @@ -43,6 +43,20 @@ def get_contents_count(self): return self.filter(must_redirect=False)\ .count() + def get_top_tags(self, displayed_types, limit=-1): + 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=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..38d5cb5c31 100644 --- a/zds/tutorialv2/views/views_published.py +++ b/zds/tutorialv2/views/views_published.py @@ -675,15 +675,4 @@ class TagsListView(ListView): template_name = "tutorialv2/view/tags.html" context_object_name = 'tags' 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 + queryset = PublishedContent.objects.get_top_tags(self.displayed_types) diff --git a/zds/utils/templatetags/topbar.py b/zds/utils/templatetags/topbar.py index c8832f5a0c..c72f2c1c7d 100644 --- a/zds/utils/templatetags/topbar.py +++ b/zds/utils/templatetags/topbar.py @@ -93,25 +93,8 @@ 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(limit=settings.ZDS_APP['forum']['top_tag_max']), + "categories": cats} @register.filter('auth_forum') From 0f31077b45213f5248747b07269e2afbb34494b0 Mon Sep 17 00:00:00 2001 From: artragis Date: Fri, 29 Apr 2016 13:38:22 +0200 Subject: [PATCH 2/7] typo --- zds/tutorialv2/managers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zds/tutorialv2/managers.py b/zds/tutorialv2/managers.py index d81cb506f2..31d742b716 100644 --- a/zds/tutorialv2/managers.py +++ b/zds/tutorialv2/managers.py @@ -46,7 +46,7 @@ def get_contents_count(self): def get_top_tags(self, displayed_types, limit=-1): published = PublishedContent.objects.filter( must_redirect=False, - content__type__in=self.displayed_types).values('content__tags').distinct() + 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, From b68c277eb68c7219748c057030820ecb71f071a5 Mon Sep 17 00:00:00 2001 From: artragis Date: Fri, 29 Apr 2016 16:28:10 +0200 Subject: [PATCH 3/7] typo --- zds/tutorialv2/views/views_published.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/zds/tutorialv2/views/views_published.py b/zds/tutorialv2/views/views_published.py index 38d5cb5c31..69e205d537 100644 --- a/zds/tutorialv2/views/views_published.py +++ b/zds/tutorialv2/views/views_published.py @@ -675,4 +675,5 @@ class TagsListView(ListView): template_name = "tutorialv2/view/tags.html" context_object_name = 'tags' displayed_types = ["TUTORIAL", "ARTICLE"] - queryset = PublishedContent.objects.get_top_tags(self.displayed_types) + def get_queryset(self): + return PublishedContent.objects.get_top_tags(self.displayed_types) From 673947f50c2498d35b95794ae124e2b87ab556f1 Mon Sep 17 00:00:00 2001 From: artragis Date: Fri, 29 Apr 2016 17:08:12 +0200 Subject: [PATCH 4/7] typo --- zds/utils/templatetags/topbar.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/zds/utils/templatetags/topbar.py b/zds/utils/templatetags/topbar.py index c72f2c1c7d..b229738320 100644 --- a/zds/utils/templatetags/topbar.py +++ b/zds/utils/templatetags/topbar.py @@ -93,7 +93,8 @@ def top_categories_content(_type): else: cats[key] = [(csc['subcategory__title'], csc['subcategory__slug'])] - return {"tags": PublishedContent.objects.get_top_tags(limit=settings.ZDS_APP['forum']['top_tag_max']), + return {"tags": PublishedContent.objects.get_top_tags(["TUTORIAL", "ARTICLE"], + limit=settings.ZDS_APP['forum']['top_tag_max']), "categories": cats} From 96e1cc65e5c9c1fa0d8cae1b345a75e92020ce9a Mon Sep 17 00:00:00 2001 From: artragis Date: Fri, 29 Apr 2016 17:31:47 +0200 Subject: [PATCH 5/7] typo --- zds/tutorialv2/managers.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/zds/tutorialv2/managers.py b/zds/tutorialv2/managers.py index 31d742b716..c0e6de4f67 100644 --- a/zds/tutorialv2/managers.py +++ b/zds/tutorialv2/managers.py @@ -2,6 +2,7 @@ from django.conf import settings from django.db import models +from zds.utils.models import Tag class PublishedContentManager(models.Manager): @@ -44,7 +45,7 @@ def get_contents_count(self): .count() def get_top_tags(self, displayed_types, limit=-1): - published = PublishedContent.objects.filter( + published = self.filter( must_redirect=False, content__type__in=displayed_types).values('content__tags').distinct() tags_pk = [tag['content__tags'] for tag in published] From ef1577cfb76cbc9bf5ec0d52aab523c2d29e8cf7 Mon Sep 17 00:00:00 2001 From: artragis Date: Sat, 30 Apr 2016 11:22:22 +0200 Subject: [PATCH 6/7] typo --- zds/tutorialv2/managers.py | 1 + zds/tutorialv2/views/views_published.py | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/zds/tutorialv2/managers.py b/zds/tutorialv2/managers.py index c0e6de4f67..d0ad00e11f 100644 --- a/zds/tutorialv2/managers.py +++ b/zds/tutorialv2/managers.py @@ -3,6 +3,7 @@ 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): diff --git a/zds/tutorialv2/views/views_published.py b/zds/tutorialv2/views/views_published.py index 69e205d537..25263d0e1f 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 From 9eddd2cc8fc80667b23f36eb6b3117dd8628f54a Mon Sep 17 00:00:00 2001 From: artragis Date: Sat, 30 Apr 2016 11:38:58 +0200 Subject: [PATCH 7/7] typo --- zds/tutorialv2/views/views_published.py | 1 + 1 file changed, 1 insertion(+) diff --git a/zds/tutorialv2/views/views_published.py b/zds/tutorialv2/views/views_published.py index 25263d0e1f..e71a00eb45 100644 --- a/zds/tutorialv2/views/views_published.py +++ b/zds/tutorialv2/views/views_published.py @@ -674,5 +674,6 @@ class TagsListView(ListView): template_name = "tutorialv2/view/tags.html" context_object_name = 'tags' displayed_types = ["TUTORIAL", "ARTICLE"] + def get_queryset(self): return PublishedContent.objects.get_top_tags(self.displayed_types)