From 9c93d7cbf29d241dc4dd309eed5f8971f5f9b4d3 Mon Sep 17 00:00:00 2001 From: Tadas Dailyda Date: Wed, 25 Feb 2015 22:37:58 +0000 Subject: [PATCH] support language switcher for post detail view, add navigation modifier to select current category in the menu when in post detail view --- djangocms_blog/menu.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/djangocms_blog/menu.py b/djangocms_blog/menu.py index 8953e44..69c990f 100644 --- a/djangocms_blog/menu.py +++ b/djangocms_blog/menu.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- 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 django.db.models.signals import post_save, post_delete from django.utils.translation import ugettext_lazy as _, get_language @@ -28,6 +28,30 @@ 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): + 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): menu_pool.clear(all=True)