Add support for view_url_name. Smarter toolbar menu. Sync with latest parler updates.

This commit is contained in:
Iacopo Spalletti 2014-03-13 22:16:40 +01:00
parent b6142d86df
commit 2515b2a646
3 changed files with 34 additions and 13 deletions

View file

@ -5,13 +5,23 @@ from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from .models import BLOG_CURRENT_POST_IDENTIFIER
@toolbar_pool.register @toolbar_pool.register
class BlogToolbar(CMSToolbar): class BlogToolbar(CMSToolbar):
def populate(self): def populate(self):
if not (self.is_current_app and self.request.user.has_perm('djangocms_blog.add_post')):
return
admin_menu = self.toolbar.get_or_create_menu("djangocms_blog", _('Blog')) admin_menu = self.toolbar.get_or_create_menu("djangocms_blog", _('Blog'))
url = reverse('admin:djangocms_blog_post_changelist') url = reverse('admin:djangocms_blog_post_changelist')
admin_menu.add_modal_item(_('Post list'), url=url) admin_menu.add_modal_item(_('Post list'), url=url)
url = reverse('admin:djangocms_blog_post_add') url = reverse('admin:djangocms_blog_post_add')
admin_menu.add_modal_item(_('Add post'), url=url) admin_menu.add_modal_item(_('Add post'), url=url)
current_post = getattr(self.request, BLOG_CURRENT_POST_IDENTIFIER, None)
if current_post and self.request.user.has_perm('djangocms_blog.change_post'):
admin_menu.add_modal_item(_('Edit Post'),reverse(
'admin:djangocms_blog_post_change', args=(current_post.pk,)),
active=True)

View file

@ -17,6 +17,7 @@ from taggit_autosuggest.managers import TaggableManager
from . import settings from . import settings
from .managers import GenericDateTaggedManager from .managers import GenericDateTaggedManager
BLOG_CURRENT_POST_IDENTIFIER = 'djangocms_post_current'
class BlogCategory(TranslatableModel): class BlogCategory(TranslatableModel):
""" """
@ -101,6 +102,7 @@ class Post(TranslatableModel):
verbose_name = _('blog article') verbose_name = _('blog article')
verbose_name_plural = _('blog articles') verbose_name_plural = _('blog articles')
ordering = ("-date_published", "-date_created") ordering = ("-date_published", "-date_created")
get_latest_by = 'date_published'
def __unicode__(self): def __unicode__(self):
return self.safe_translation_getter('title') return self.safe_translation_getter('title')

View file

@ -1,23 +1,23 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import datetime import datetime
from cms.utils import get_language_from_request from django.utils.translation import get_language
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.core.urlresolvers import resolve from django.core.urlresolvers import resolve
from django.db.models import Count
from django.http import Http404 from django.http import Http404
from django.utils.translation import ugettext_lazy as _
from django.views.generic import ListView, DetailView from django.views.generic import ListView, DetailView
from parler.utils import get_active_language_choices from parler.utils import get_active_language_choices
from .models import Post, BlogCategory from parler.views import ViewUrlMixin
from .models import Post, BlogCategory, BLOG_CURRENT_POST_IDENTIFIER
from .settings import BLOG_PAGINATION, BLOG_POSTS_LIST_TRUNCWORDS_COUNT from .settings import BLOG_PAGINATION, BLOG_POSTS_LIST_TRUNCWORDS_COUNT
class BaseBlogView(object): class BaseBlogView(ViewUrlMixin):
def get_queryset(self): def get_queryset(self):
language = get_language_from_request(self.request) language = get_language()
manager = self.model._default_manager.translated(language) manager = self.model._default_manager.language(language)
if not self.request.user.is_staff: if not self.request.user.is_staff:
manager = manager.filter(publish=True) manager = manager.filter(publish=True)
return manager return manager
@ -32,6 +32,7 @@ class PostListView(BaseBlogView, ListView):
context_object_name = 'post_list' context_object_name = 'post_list'
template_name = "djangocms_blog/post_list.html" template_name = "djangocms_blog/post_list.html"
paginate_by = BLOG_PAGINATION paginate_by = BLOG_PAGINATION
view_url_name = 'djangocms_blog:posts-latest'
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super(PostListView, self).get_context_data(**kwargs) context = super(PostListView, self).get_context_data(**kwargs)
@ -43,18 +44,22 @@ class PostDetailView(BaseBlogView, DetailView):
model = Post model = Post
context_object_name = 'post' context_object_name = 'post'
template_name = "djangocms_blog/post_detail.html" template_name = "djangocms_blog/post_detail.html"
slug_field = 'translations__slug' slug_field = 'slug'
view_url_name = 'djangocms_blog:post-detail'
def get_object(self, queryset=None): def get_object(self, queryset=None):
try: try:
qs = self.model._default_manager.get(**{ qs = self.model._default_manager.active_translations(**{
'translations__language_code': get_language_from_request(self.request),
self.slug_field: self.kwargs.get(self.slug_url_kwarg, None) self.slug_field: self.kwargs.get(self.slug_url_kwarg, None)
}) }).latest()
except Post.DoesNotExist: except Post.DoesNotExist:
raise Http404() raise Http404()
return qs return qs
def get_context_data(self, **kwargs):
context = super(PostDetailView, self).get_context_data(**kwargs)
setattr(self.request, BLOG_CURRENT_POST_IDENTIFIER, self.get_object())
return context
class PostArchiveView(BaseBlogView, ListView): class PostArchiveView(BaseBlogView, ListView):
model = Post model = Post
@ -64,6 +69,7 @@ class PostArchiveView(BaseBlogView, ListView):
allow_empty = True allow_empty = True
allow_future = True allow_future = True
paginate_by = BLOG_PAGINATION paginate_by = BLOG_PAGINATION
view_url_name = 'djangocms_blog:posts-archive'
def get_queryset(self): def get_queryset(self):
qs = super(PostArchiveView, self).get_queryset() qs = super(PostArchiveView, self).get_queryset()
@ -86,6 +92,7 @@ class TaggedListView(BaseBlogView, ListView):
context_object_name = 'post_list' context_object_name = 'post_list'
template_name = "djangocms_blog/post_list.html" template_name = "djangocms_blog/post_list.html"
paginate_by = BLOG_PAGINATION paginate_by = BLOG_PAGINATION
view_url_name = 'djangocms_blog:posts-tagged'
def get_queryset(self): def get_queryset(self):
qs = super(TaggedListView, self).get_queryset() qs = super(TaggedListView, self).get_queryset()
@ -102,6 +109,7 @@ class AuthorEntriesView(BaseBlogView, ListView):
context_object_name = 'post_list' context_object_name = 'post_list'
template_name = "djangocms_blog/post_list.html" template_name = "djangocms_blog/post_list.html"
paginate_by = BLOG_PAGINATION paginate_by = BLOG_PAGINATION
view_url_name = 'djangocms_blog:posts-authors'
def get_queryset(self): def get_queryset(self):
qs = super(AuthorEntriesView, self).get_queryset() qs = super(AuthorEntriesView, self).get_queryset()
@ -120,11 +128,12 @@ class CategoryEntriesView(BaseBlogView, ListView):
template_name = "djangocms_blog/post_list.html" template_name = "djangocms_blog/post_list.html"
_category = None _category = None
paginate_by = BLOG_PAGINATION paginate_by = BLOG_PAGINATION
view_url_name = 'djangocms_blog:posts-category'
@property @property
def category(self): def category(self):
if not self._category: if not self._category:
language = get_language_from_request(self.request) language = get_language()
self._category = BlogCategory._default_manager.language(language).get( self._category = BlogCategory._default_manager.language(language).get(
translations__language_code=language, translations__language_code=language,
translations__slug=self.kwargs['category']) translations__slug=self.kwargs['category'])