diff --git a/djangocms_blog/cms_app.py b/djangocms_blog/cms_app.py index 477d7c5..cefe215 100644 --- a/djangocms_blog/cms_app.py +++ b/djangocms_blog/cms_app.py @@ -15,12 +15,12 @@ class BlogCategoryMenu(CMSAttachMenu): def get_nodes(self, request): nodes = [] qs = BlogCategory.objects.translated(get_language()) - qs = qs.order_by('parent_id', 'translations__name').distinct() + qs = qs.order_by('parent_id', 'translations__name') for category in qs: kwargs = { 'category': category.slug } node = NavigationNode( category.name, - reverse('djangocms_blog:posts-category', kwargs=kwargs), + category.get_absolute_url(), category.pk, category.parent_id ) diff --git a/djangocms_blog/models.py b/djangocms_blog/models.py index 125fbe8..1429fae 100644 --- a/djangocms_blog/models.py +++ b/djangocms_blog/models.py @@ -49,6 +49,16 @@ class BlogCategory(TranslatableModel): def count(self): return self.blog_posts.filter(publish=True).count() + def get_absolute_url(self): + slug = None + lang = get_language() + if self.has_translation(lang): + slug = self.safe_translation_getter('slug', language_code=lang) + if slug is not None: + return reverse('djangocms_blog:posts-category', kwargs={'category':slug}) + # fallback to url with id in case there is no translation of slug + return reverse('djangocms_blog:posts-category-id', kwargs={'category_id':self.pk}) + def __str__(self): return self.safe_translation_getter('name') diff --git a/djangocms_blog/urls.py b/djangocms_blog/urls.py index 41cf7a4..2073984 100644 --- a/djangocms_blog/urls.py +++ b/djangocms_blog/urls.py @@ -15,6 +15,7 @@ urlpatterns = patterns( url(r'^(?P\d{4})/(?P\d{1,2})/(?P\d{1,2})/(?P\w[-\w]*)/$', PostDetailView.as_view(), name='post-detail'), url(r'^author/(?P[\w\.@+-]+)/$', AuthorEntriesView.as_view(), name='posts-author'), url(r'^category/(?P[\w\.@+-]+)/$', CategoryEntriesView.as_view(), name='posts-category'), + url(r'^category_id/(?P\d+)/$', CategoryEntriesView.as_view(), name='posts-category-id'), url(r'^tag/(?P[-\w]+)/$', TaggedListView.as_view(), name='posts-tagged'), url(r'^tag/(?P[-\w]+)/feed/$', TagFeed(), name='posts-tagged-feed'), ) diff --git a/djangocms_blog/views.py b/djangocms_blog/views.py index 86885bf..a378308 100644 --- a/djangocms_blog/views.py +++ b/djangocms_blog/views.py @@ -133,12 +133,21 @@ class CategoryEntriesView(BaseBlogView, ListView): @property def category(self): if not self._category: - self._category = BlogCategory.objects.active_translations(get_language(), slug=self.kwargs['category']).latest('pk') + if 'category' in self.kwargs: + self._category = BlogCategory.objects.active_translations(get_language(), slug=self.kwargs['category']).latest('pk') + else: + self._category = BlogCategory.objects.get(pk=self.kwargs['category_id']) return self._category + def get(self, *args, **kwargs): + # submit object to cms toolbar to get correct language switcher behavior + if hasattr(self.request, 'toolbar'): + self.request.toolbar.set_object(self.category) + return super(CategoryEntriesView, self).get(*args, **kwargs) + def get_queryset(self): qs = super(CategoryEntriesView, self).get_queryset() - if 'category' in self.kwargs: + if 'category' in self.kwargs or 'category_id' in self.kwargs: qs = qs.filter(categories=self.category.pk) return qs