Fix selecting current menu item according to menu layout
This commit is contained in:
parent
c71fb4e534
commit
2588f6c020
3 changed files with 84 additions and 2 deletions
|
@ -11,6 +11,7 @@ History
|
||||||
* Use all_languages_column to admin
|
* Use all_languages_column to admin
|
||||||
* Add publish button
|
* Add publish button
|
||||||
* Fix issues in migrations. Thanks @skirsdeda
|
* Fix issues in migrations. Thanks @skirsdeda
|
||||||
|
* Fix selecting current menu item according to menu layout
|
||||||
|
|
||||||
0.6.3 (2015-12-22)
|
0.6.3 (2015-12-22)
|
||||||
++++++++++++++++++
|
++++++++++++++++++
|
||||||
|
|
|
@ -1,21 +1,34 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from __future__ import absolute_import, print_function, unicode_literals
|
from __future__ import absolute_import, print_function, unicode_literals
|
||||||
|
|
||||||
|
from cms.apphook_pool import apphook_pool
|
||||||
from cms.menu_bases import CMSAttachMenu
|
from cms.menu_bases import CMSAttachMenu
|
||||||
|
from django.core.urlresolvers import resolve
|
||||||
from django.db.models.signals import post_delete, post_save
|
from django.db.models.signals import post_delete, post_save
|
||||||
from django.utils.translation import get_language_from_request, ugettext_lazy as _
|
from django.utils.translation import get_language_from_request, ugettext_lazy as _
|
||||||
from menus.base import NavigationNode
|
from menus.base import Modifier, NavigationNode
|
||||||
from menus.menu_pool import menu_pool
|
from menus.menu_pool import menu_pool
|
||||||
|
|
||||||
from .cms_appconfig import BlogConfig
|
from .cms_appconfig import BlogConfig
|
||||||
from .models import BlogCategory, Post
|
from .models import BlogCategory, Post
|
||||||
from .settings import MENU_TYPE_CATEGORIES, MENU_TYPE_COMPLETE, MENU_TYPE_POSTS
|
from .settings import MENU_TYPE_CATEGORIES, MENU_TYPE_COMPLETE, MENU_TYPE_POSTS, get_setting
|
||||||
|
|
||||||
|
|
||||||
class BlogCategoryMenu(CMSAttachMenu):
|
class BlogCategoryMenu(CMSAttachMenu):
|
||||||
|
"""
|
||||||
|
Main menu class
|
||||||
|
|
||||||
|
Handles all types of blog menu
|
||||||
|
"""
|
||||||
name = _('Blog menu')
|
name = _('Blog menu')
|
||||||
|
|
||||||
def get_nodes(self, request):
|
def get_nodes(self, request):
|
||||||
|
"""
|
||||||
|
Generates the nodelist
|
||||||
|
|
||||||
|
:param request:
|
||||||
|
:return: list of nodes
|
||||||
|
"""
|
||||||
nodes = []
|
nodes = []
|
||||||
|
|
||||||
language = get_language_from_request(request, check_path=True)
|
language = get_language_from_request(request, check_path=True)
|
||||||
|
@ -75,7 +88,44 @@ class BlogCategoryMenu(CMSAttachMenu):
|
||||||
menu_pool.register_menu(BlogCategoryMenu)
|
menu_pool.register_menu(BlogCategoryMenu)
|
||||||
|
|
||||||
|
|
||||||
|
class BlogNavModifier(Modifier):
|
||||||
|
"""
|
||||||
|
This navigation modifier makes sure that when
|
||||||
|
a particular blog post is viewed,
|
||||||
|
a corresponding category is selected in menu
|
||||||
|
"""
|
||||||
|
def modify(self, request, nodes, namespace, root_id, post_cut, breadcrumb):
|
||||||
|
app = None
|
||||||
|
config = None
|
||||||
|
if getattr(request, 'current_page', None) and request.current_page.application_urls:
|
||||||
|
app = apphook_pool.get_apphook(request.current_page.application_urls)
|
||||||
|
|
||||||
|
if app and app.app_config:
|
||||||
|
namespace = resolve(request.path).namespace
|
||||||
|
config = app.get_config(namespace)
|
||||||
|
if config and config.menu_structure != MENU_TYPE_CATEGORIES:
|
||||||
|
return nodes
|
||||||
|
if post_cut:
|
||||||
|
return nodes
|
||||||
|
current_post = getattr(request, get_setting('CURRENT_POST_IDENTIFIER'), None)
|
||||||
|
category = None
|
||||||
|
if current_post and current_post.__class__ == Post:
|
||||||
|
category = current_post.categories.first()
|
||||||
|
if not category:
|
||||||
|
return nodes
|
||||||
|
|
||||||
|
for node in nodes:
|
||||||
|
if '%s-%s' % (category.__class__.__name__, category.pk) == node.id:
|
||||||
|
node.selected = True
|
||||||
|
return nodes
|
||||||
|
|
||||||
|
menu_pool.register_modifier(BlogNavModifier)
|
||||||
|
|
||||||
|
|
||||||
def clear_menu_cache(**kwargs):
|
def clear_menu_cache(**kwargs):
|
||||||
|
"""
|
||||||
|
Empty menu cache when saving categories
|
||||||
|
"""
|
||||||
menu_pool.clear(all=True)
|
menu_pool.clear(all=True)
|
||||||
|
|
||||||
post_save.connect(clear_menu_cache, sender=BlogCategory)
|
post_save.connect(clear_menu_cache, sender=BlogCategory)
|
||||||
|
|
|
@ -29,6 +29,7 @@ class MenuTest(BaseTest):
|
||||||
self.cats.append(cat)
|
self.cats.append(cat)
|
||||||
|
|
||||||
activate('en')
|
activate('en')
|
||||||
|
menu_pool.clear(all=True)
|
||||||
menu_pool.discover_menus()
|
menu_pool.discover_menus()
|
||||||
# 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
|
||||||
|
@ -139,6 +140,10 @@ class MenuTest(BaseTest):
|
||||||
(PostDetailView, 'slug', posts[0], posts[0].categories.first()),
|
(PostDetailView, 'slug', posts[0], posts[0].categories.first()),
|
||||||
(CategoryEntriesView, 'category', self.cats[2], self.cats[2])
|
(CategoryEntriesView, 'category', self.cats[2], self.cats[2])
|
||||||
)
|
)
|
||||||
|
menu_pool.clear(all=True)
|
||||||
|
cache.clear()
|
||||||
|
self.app_config_1.app_data.config.menu_structure = MENU_TYPE_COMPLETE
|
||||||
|
self.app_config_1.save()
|
||||||
for view_cls, kwarg, obj, cat in tests:
|
for view_cls, kwarg, obj, cat in tests:
|
||||||
request = self.get_page_request(pages[1], self.user, path=obj.get_absolute_url())
|
request = self.get_page_request(pages[1], self.user, path=obj.get_absolute_url())
|
||||||
with smart_override('en'):
|
with smart_override('en'):
|
||||||
|
@ -149,6 +154,7 @@ class MenuTest(BaseTest):
|
||||||
view_obj.app_config = self.app_config_1
|
view_obj.app_config = self.app_config_1
|
||||||
view_obj.kwargs = {kwarg: obj.slug}
|
view_obj.kwargs = {kwarg: obj.slug}
|
||||||
view_obj.get(request)
|
view_obj.get(request)
|
||||||
|
view_obj.get_context_data()
|
||||||
# check if selected menu node points to cat
|
# check if selected menu node points to cat
|
||||||
nodes = menu_pool.get_nodes(request)
|
nodes = menu_pool.get_nodes(request)
|
||||||
found = False
|
found = False
|
||||||
|
@ -158,3 +164,28 @@ class MenuTest(BaseTest):
|
||||||
found = True
|
found = True
|
||||||
break
|
break
|
||||||
self.assertTrue(found)
|
self.assertTrue(found)
|
||||||
|
|
||||||
|
menu_pool.clear(all=True)
|
||||||
|
cache.clear()
|
||||||
|
self.app_config_1.app_data.config.menu_structure = MENU_TYPE_CATEGORIES
|
||||||
|
self.app_config_1.save()
|
||||||
|
for view_cls, kwarg, obj, cat in tests:
|
||||||
|
request = self.get_page_request(pages[1], self.user, path=obj.get_absolute_url())
|
||||||
|
with smart_override('en'):
|
||||||
|
with switch_language(obj, 'en'):
|
||||||
|
view_obj = view_cls()
|
||||||
|
view_obj.request = request
|
||||||
|
view_obj.namespace, view_obj.config = get_app_instance(request)
|
||||||
|
view_obj.app_config = self.app_config_1
|
||||||
|
view_obj.kwargs = {kwarg: obj.slug}
|
||||||
|
view_obj.get(request)
|
||||||
|
view_obj.get_context_data()
|
||||||
|
# check if selected menu node points to cat
|
||||||
|
nodes = menu_pool.get_nodes(request)
|
||||||
|
found = False
|
||||||
|
for node in nodes:
|
||||||
|
if node.selected:
|
||||||
|
self.assertEqual(node.url, cat.get_absolute_url())
|
||||||
|
found = True
|
||||||
|
break
|
||||||
|
self.assertTrue(found)
|
||||||
|
|
Loading…
Reference in a new issue