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
|
|
@ -1,21 +1,34 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
|
||||
from cms.apphook_pool import apphook_pool
|
||||
from cms.menu_bases import CMSAttachMenu
|
||||
from django.core.urlresolvers import resolve
|
||||
from django.db.models.signals import post_delete, post_save
|
||||
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 .cms_appconfig import BlogConfig
|
||||
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):
|
||||
"""
|
||||
Main menu class
|
||||
|
||||
Handles all types of blog menu
|
||||
"""
|
||||
name = _('Blog menu')
|
||||
|
||||
def get_nodes(self, request):
|
||||
"""
|
||||
Generates the nodelist
|
||||
|
||||
:param request:
|
||||
:return: list of nodes
|
||||
"""
|
||||
nodes = []
|
||||
|
||||
language = get_language_from_request(request, check_path=True)
|
||||
|
|
@ -75,7 +88,44 @@ class BlogCategoryMenu(CMSAttachMenu):
|
|||
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):
|
||||
"""
|
||||
Empty menu cache when saving categories
|
||||
"""
|
||||
menu_pool.clear(all=True)
|
||||
|
||||
post_save.connect(clear_menu_cache, sender=BlogCategory)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue