Merge pull request #195 from nephila/feature/non_required_categories

Fix menu behavior
This commit is contained in:
Iacopo Spalletti 2016-02-07 15:51:14 +01:00
commit ae2b4a1f6f
5 changed files with 48 additions and 27 deletions

3
.checkignore Normal file
View file

@ -0,0 +1,3 @@
tests/*
docs/*
djangocms_blog/south_migrations/*

View file

@ -235,11 +235,14 @@ Menu
``djangocms_blog`` provides support for django CMS menu framework. ``djangocms_blog`` provides support for django CMS menu framework.
By default all the categories and posts are added to the menu, in a hierarcical structure. By default all the categories and posts are added to the menu, in a hierarchical structure.
Is it possibile to configure per Apphook, whether the menu includes post and categories Is it possibile to configure per Apphook, whether the menu includes post and categories
(the default), only categorie, only posts or no item. (the default), only categorie, only posts or no item.
If "post and categories" or "only categories" are set, all the posts not associated with a
category are not added to the menu.
Templates Templates
+++++++++ +++++++++

View file

@ -69,7 +69,7 @@ HELPER_SETTINGS = dict(
{'code': 'en'}, {'code': 'en'},
), ),
'default': { 'default': {
'fallback': 'en', 'fallbacks': ['en'],
'hide_untranslated': False, 'hide_untranslated': False,
} }
}, },

View file

@ -52,20 +52,23 @@ class BlogCategoryMenu(CMSAttachMenu):
posts = posts.namespace(self.instance.application_namespace) posts = posts.namespace(self.instance.application_namespace)
posts = posts.active_translations(language).distinct() posts = posts.active_translations(language).distinct()
for post in posts: for post in posts:
post_id = None
parent = None
if categories_menu: if categories_menu:
category = post.categories.first() category = post.categories.first()
parent = '%s-%s' % (category.__class__.__name__, category.pk) if category:
post_id = '%s-%s' % (post.__class__.__name__, post.pk), parent = '%s-%s' % (category.__class__.__name__, category.pk)
post_id = '%s-%s' % (post.__class__.__name__, post.pk),
else: else:
parent = None
post_id = '%s-%s' % (post.__class__.__name__, post.pk), post_id = '%s-%s' % (post.__class__.__name__, post.pk),
node = NavigationNode( if post_id:
post.get_title(), node = NavigationNode(
post.get_absolute_url(language), post.get_title(),
post_id, post.get_absolute_url(language),
parent post_id,
) parent
nodes.append(node) )
nodes.append(node)
return nodes return nodes

View file

@ -3,7 +3,7 @@ from __future__ import absolute_import, print_function, unicode_literals
from aldryn_apphooks_config.utils import get_app_instance from aldryn_apphooks_config.utils import get_app_instance
from django.core.cache import cache from django.core.cache import cache
from django.utils.translation import activate from django.utils.translation import activate, override
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
@ -32,22 +32,34 @@ class MenuTest(BaseTest):
# All cms menu modifiers should be removed from menu_pool.modifiers # All cms menu modifiers should be removed from menu_pool.modifiers
# so that they do not interfere with our menu nodes # so that they do not interfere with our menu nodes
menu_pool.modifiers = [m for m in menu_pool.modifiers if m.__module__.startswith('djangocms_blog')] menu_pool.modifiers = [m for m in menu_pool.modifiers if m.__module__.startswith('djangocms_blog')]
cache.clear()
def test_menu_nodes(self): def test_menu_nodes(self):
""" """
Tests if all categories are present in the menu Tests if all categories are present in the menu
""" """
self.get_posts() posts = self.get_posts()
self.get_pages() pages = self.get_pages()
self.reload_urlconf()
for lang in ('en', 'it'): for lang in ('en', 'it'):
request = self.get_page_request(None, self.user, r'/%s/page-two/' % lang)
with smart_override(lang): with smart_override(lang):
nodes = menu_pool.get_nodes(request, namespace='BlogCategoryMenu') request = self.get_page_request(pages[1], self.user, pages[1].get_absolute_url(lang))
nodes = menu_pool.get_nodes(request)
nodes_url = set([node.url for node in nodes]) nodes_url = set([node.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))
cache.clear()
posts[0].categories.clear()
for lang in ('en', 'it'):
with smart_override(lang):
request = self.get_page_request(pages[1], self.user, pages[1].get_absolute_url(lang))
nodes = menu_pool.get_nodes(request)
nodes_url = set([node.url for node in nodes])
self.assertFalse(posts[0].get_absolute_url(lang) in nodes_url)
self.assertTrue(posts[1].get_absolute_url(lang) in nodes_url)
def test_menu_options(self): def test_menu_options(self):
""" """
Tests menu structure based on menu_structure configuration Tests menu structure based on menu_structure configuration
@ -72,7 +84,7 @@ class MenuTest(BaseTest):
for lang in languages: for lang in languages:
request = self.get_page_request(None, self.user, r'/%s/page-two/' % lang) request = self.get_page_request(None, self.user, r'/%s/page-two/' % lang)
with smart_override(lang): with smart_override(lang):
nodes = menu_pool.get_nodes(request, namespace='BlogCategoryMenu') nodes = menu_pool.get_nodes(request)
nodes_url = set([node.url for node in nodes]) nodes_url = set([node.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))
@ -84,7 +96,7 @@ class MenuTest(BaseTest):
for lang in languages: for lang in languages:
request = self.get_page_request(None, self.user, r'/%s/page-two/' % lang) request = self.get_page_request(None, self.user, r'/%s/page-two/' % lang)
with smart_override(lang): with smart_override(lang):
nodes = menu_pool.get_nodes(request, namespace='BlogCategoryMenu') nodes = menu_pool.get_nodes(request)
nodes_url = set([node.url for node in nodes]) nodes_url = set([node.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))
@ -96,7 +108,7 @@ class MenuTest(BaseTest):
for lang in languages: for lang in languages:
request = self.get_page_request(None, self.user, r'/%s/page-two/' % lang) request = self.get_page_request(None, self.user, r'/%s/page-two/' % lang)
with smart_override(lang): with smart_override(lang):
nodes = menu_pool.get_nodes(request, namespace='BlogCategoryMenu') nodes = menu_pool.get_nodes(request)
nodes_url = set([node.url for node in nodes]) nodes_url = set([node.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))
@ -108,7 +120,7 @@ class MenuTest(BaseTest):
for lang in languages: for lang in languages:
request = self.get_page_request(None, self.user, r'/%s/page-two/' % lang) request = self.get_page_request(None, self.user, r'/%s/page-two/' % lang)
with smart_override(lang): with smart_override(lang):
nodes = menu_pool.get_nodes(request, namespace='BlogCategoryMenu') nodes = menu_pool.get_nodes(request)
nodes_url = set([node.url for node in nodes]) nodes_url = set([node.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))
@ -137,7 +149,7 @@ class MenuTest(BaseTest):
view_obj.kwargs = {kwarg: obj.slug} view_obj.kwargs = {kwarg: obj.slug}
view_obj.get(request) view_obj.get(request)
# check if selected menu node points to cat # check if selected menu node points to cat
nodes = menu_pool.get_nodes(request, namespace='BlogCategoryMenu') nodes = menu_pool.get_nodes(request)
found = False found = False
for node in nodes: for node in nodes:
if node.selected: if node.selected: