From ba1c46a2fea81546639fc794ae84b606d9cd678c 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 +++++++++++++++++++++++++- djangocms_blog/views.py | 6 ++++++ 2 files changed, 31 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) diff --git a/djangocms_blog/views.py b/djangocms_blog/views.py index a378308..53d1646 100644 --- a/djangocms_blog/views.py +++ b/djangocms_blog/views.py @@ -47,6 +47,12 @@ class PostDetailView(TranslatableSlugMixin, BaseBlogView, DetailView): slug_field = 'slug' view_url_name = 'djangocms_blog:post-detail' + def get(self, *args, **kwargs): + # submit object to cms to get corrent language switcher and selected category behavior + if hasattr(self.request, 'toolbar'): + self.request.toolbar.set_object(self.get_object()) + return super(PostDetailView, self).get(*args, **kwargs) + def get_context_data(self, **kwargs): context = super(PostDetailView, self).get_context_data(**kwargs) context['meta'] = self.get_object().as_meta()