support language switcher for post detail view, add navigation modifier to select current category in the menu when in post detail view

This commit is contained in:
Tadas Dailyda 2015-02-25 22:37:58 +00:00
parent b3a1dc9015
commit 9c93d7cbf2

View file

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from cms.menu_bases import CMSAttachMenu from cms.menu_bases import CMSAttachMenu
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 django.db.models.signals import post_save, post_delete from django.db.models.signals import post_save, post_delete
from django.utils.translation import ugettext_lazy as _, get_language from django.utils.translation import ugettext_lazy as _, get_language
@ -28,6 +28,30 @@ 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):
if not post_cut: return nodes
if not hasattr(request, 'toolbar'):
return nodes
if request.toolbar.get_object_model() != 'djangocms_blog.post':
return nodes
cat = request.toolbar.obj.categories.first()
if not cat: return nodes
for node in nodes:
if (node.namespace == BlogCategoryMenu.__name__ and
cat.pk == node.id):
node.selected = True
break
return nodes
menu_pool.register_modifier(BlogNavModifier)
def clear_menu_cache(**kwargs): def clear_menu_cache(**kwargs):
menu_pool.clear(all=True) menu_pool.clear(all=True)