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 d8906569c9
commit ba1c46a2fe
2 changed files with 31 additions and 1 deletions

View file

@ -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)

View file

@ -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()