Filter out duplicated categories due to parler joins

This commit is contained in:
Iacopo Spalletti 2017-06-11 12:35:47 +02:00
parent 384442f340
commit 88e7ab6490
No known key found for this signature in database
GPG key ID: BDCBC2EB289F60C6
2 changed files with 33 additions and 19 deletions

View file

@ -52,6 +52,13 @@ class BlogCategoryMenu(CMSAttachMenu):
namespace=self.instance.application_namespace namespace=self.instance.application_namespace
) )
config = self._config[self.instance.application_namespace] config = self._config[self.instance.application_namespace]
if hasattr(self, 'instance') and self.instance:
if not getattr(request, 'toolbar', False) or not request.toolbar.edit_mode:
if self.instance == self.instance.get_draft_object():
return []
else:
if self.instance == self.instance.get_public_object():
return []
if config and config.menu_structure in (MENU_TYPE_COMPLETE, MENU_TYPE_CATEGORIES): if config and config.menu_structure in (MENU_TYPE_COMPLETE, MENU_TYPE_CATEGORIES):
categories_menu = True categories_menu = True
if config and config.menu_structure in (MENU_TYPE_COMPLETE, MENU_TYPE_POSTS): if config and config.menu_structure in (MENU_TYPE_COMPLETE, MENU_TYPE_POSTS):
@ -61,21 +68,24 @@ class BlogCategoryMenu(CMSAttachMenu):
categories = BlogCategory.objects categories = BlogCategory.objects
if config: if config:
categories = categories.namespace(self.instance.application_namespace) categories = categories.namespace(self.instance.application_namespace)
categories = categories.active_translations(language).distinct() categories = categories.active_translations(language)
categories = categories.order_by('parent__id', 'translations__name').\ categories = categories.order_by('parent__id', 'translations__name').\
select_related('app_config').prefetch_related('translations') select_related('app_config').prefetch_related('translations')
added_categories = []
for category in categories: for category in categories:
node = NavigationNode( if category.pk not in added_categories:
category.name, node = NavigationNode(
category.get_absolute_url(), category.name,
'{0}-{1}'.format(category.__class__.__name__, category.pk), category.get_absolute_url(),
( '{0}-{1}'.format(category.__class__.__name__, category.pk),
'{0}-{1}'.format( (
category.__class__.__name__, category.parent_id '{0}-{1}'.format(
) if category.parent_id else None category.__class__.__name__, category.parent_id
) if category.parent_id else None
)
) )
) nodes.append(node)
nodes.append(node) added_categories.append(category.pk)
if posts_menu: if posts_menu:
posts = Post.objects posts = Post.objects

View file

@ -6,6 +6,7 @@ from django.utils.translation import activate
from menus.menu_pool import menu_pool from menus.menu_pool import menu_pool
from parler.utils.context import smart_override, switch_language from parler.utils.context import smart_override, switch_language
from djangocms_blog.models import BlogCategory
from djangocms_blog.settings import ( from djangocms_blog.settings import (
MENU_TYPE_CATEGORIES, MENU_TYPE_COMPLETE, MENU_TYPE_NONE, MENU_TYPE_POSTS, MENU_TYPE_CATEGORIES, MENU_TYPE_COMPLETE, MENU_TYPE_NONE, MENU_TYPE_POSTS,
) )
@ -46,9 +47,10 @@ class MenuTest(BaseTest):
for lang in ('en', 'it'): for lang in ('en', 'it'):
with smart_override(lang): with smart_override(lang):
self._reset_menus() self._reset_menus()
request = self.get_page_request(pages[1], self.user, pages[1].get_absolute_url(lang)) request = self.get_page_request(pages[1], self.user, pages[1].get_absolute_url(lang), edit=True)
nodes = menu_pool.get_nodes(request) nodes = menu_pool.get_nodes(request)
nodes_url = set([node.url for node in nodes]) self.assertTrue(len(nodes), BlogCategory.objects.all().count() + len(pages))
nodes_url = set([node.get_absolute_url() for node in nodes])
cats_url = set([cat.get_absolute_url() for cat in self.cats if cat.has_translation(lang)]) cats_url = set([cat.get_absolute_url() for cat in self.cats if cat.has_translation(lang)])
self.assertTrue(cats_url.issubset(nodes_url)) self.assertTrue(cats_url.issubset(nodes_url))
@ -57,9 +59,11 @@ class MenuTest(BaseTest):
for lang in ('en', 'it'): for lang in ('en', 'it'):
with smart_override(lang): with smart_override(lang):
self._reset_menus() self._reset_menus()
request = self.get_page_request(pages[1], self.user, pages[1].get_absolute_url(lang)) request = self.get_page_request(pages[1].get_draft_object(), self.user, pages[1].get_draft_object().get_absolute_url(lang))
nodes = menu_pool.get_nodes(request) nodes = menu_pool.get_nodes(request)
nodes_url = set([node.url for node in nodes]) urls = [node.get_absolute_url() for node in nodes]
nodes_url = [node.get_absolute_url() for node in nodes]
self.assertTrue(len(nodes_url), BlogCategory.objects.all().count() + len(pages))
self.assertFalse(posts[0].get_absolute_url(lang) in nodes_url) self.assertFalse(posts[0].get_absolute_url(lang) in nodes_url)
self.assertTrue(posts[1].get_absolute_url(lang) in nodes_url) self.assertTrue(posts[1].get_absolute_url(lang) in nodes_url)
@ -90,7 +94,7 @@ class MenuTest(BaseTest):
with smart_override(lang): with smart_override(lang):
self._reset_menus() self._reset_menus()
nodes = menu_pool.get_nodes(request) nodes = menu_pool.get_nodes(request)
nodes_url = set([node.url for node in nodes]) nodes_url = set([node.get_absolute_url() for node in nodes])
self.assertFalse(cats_url[lang].issubset(nodes_url)) self.assertFalse(cats_url[lang].issubset(nodes_url))
self.assertFalse(posts_url[lang].issubset(nodes_url)) self.assertFalse(posts_url[lang].issubset(nodes_url))
@ -103,7 +107,7 @@ class MenuTest(BaseTest):
with smart_override(lang): with smart_override(lang):
self._reset_menus() self._reset_menus()
nodes = menu_pool.get_nodes(request) nodes = menu_pool.get_nodes(request)
nodes_url = set([node.url for node in nodes]) nodes_url = set([node.get_absolute_url() for node in nodes])
self.assertFalse(cats_url[lang].issubset(nodes_url)) self.assertFalse(cats_url[lang].issubset(nodes_url))
self.assertTrue(posts_url[lang].issubset(nodes_url)) self.assertTrue(posts_url[lang].issubset(nodes_url))
@ -116,7 +120,7 @@ class MenuTest(BaseTest):
with smart_override(lang): with smart_override(lang):
self._reset_menus() self._reset_menus()
nodes = menu_pool.get_nodes(request) nodes = menu_pool.get_nodes(request)
nodes_url = set([node.url for node in nodes]) nodes_url = set([node.get_absolute_url() for node in nodes])
self.assertTrue(cats_url[lang].issubset(nodes_url)) self.assertTrue(cats_url[lang].issubset(nodes_url))
self.assertFalse(posts_url[lang].issubset(nodes_url)) self.assertFalse(posts_url[lang].issubset(nodes_url))
@ -129,7 +133,7 @@ class MenuTest(BaseTest):
with smart_override(lang): with smart_override(lang):
self._reset_menus() self._reset_menus()
nodes = menu_pool.get_nodes(request) nodes = menu_pool.get_nodes(request)
nodes_url = set([node.url for node in nodes]) nodes_url = set([node.get_absolute_url() for node in nodes])
self.assertTrue(cats_url[lang].issubset(nodes_url)) self.assertTrue(cats_url[lang].issubset(nodes_url))
self.assertTrue(posts_url[lang].issubset(nodes_url)) self.assertTrue(posts_url[lang].issubset(nodes_url))