djangocms_blog/djangocms_blog/views.py

171 lines
6.6 KiB
Python
Raw Normal View History

2014-01-04 17:07:09 +01:00
# -*- coding: utf-8 -*-
2015-09-13 00:46:05 +02:00
from __future__ import absolute_import, print_function, unicode_literals
2014-09-04 09:16:56 +02:00
from django.contrib.auth import get_user_model
2014-01-04 17:07:09 +01:00
from django.core.urlresolvers import resolve
2014-09-04 09:16:56 +02:00
from django.utils.timezone import now
from django.utils.translation import get_language
2015-07-18 17:33:36 +02:00
from django.views.generic import DetailView, ListView
from parler.views import TranslatableSlugMixin, ViewUrlMixin
2014-01-04 17:07:09 +01:00
2015-07-18 17:33:36 +02:00
from .models import BLOG_CURRENT_POST_IDENTIFIER, BlogCategory, Post
2014-10-11 12:23:36 +02:00
from .settings import get_setting
2014-01-04 17:07:09 +01:00
2014-09-04 09:16:56 +02:00
User = get_user_model()
2014-01-04 17:07:09 +01:00
class BaseBlogView(ViewUrlMixin):
2014-01-04 17:07:09 +01:00
def get_queryset(self):
language = get_language()
2015-09-13 00:27:48 +02:00
queryset = self.model._default_manager.all().active_translations(language_code=language)
if not getattr(self.request, 'toolbar', False) or not self.request.toolbar.edit_mode:
2014-09-01 10:45:10 +02:00
queryset = queryset.published()
2015-09-13 00:27:48 +02:00
return queryset
2014-01-04 17:07:09 +01:00
def render_to_response(self, context, **response_kwargs):
response_kwargs['current_app'] = resolve(self.request.path).namespace
return super(BaseBlogView, self).render_to_response(context, **response_kwargs)
class PostListView(BaseBlogView, ListView):
model = Post
context_object_name = 'post_list'
2014-09-05 08:28:10 +02:00
template_name = 'djangocms_blog/post_list.html'
2014-10-11 12:23:36 +02:00
paginate_by = get_setting('PAGINATION')
view_url_name = 'djangocms_blog:posts-latest'
2014-01-04 17:07:09 +01:00
def get_context_data(self, **kwargs):
context = super(PostListView, self).get_context_data(**kwargs)
2014-10-11 12:23:36 +02:00
context['TRUNCWORDS_COUNT'] = get_setting('POSTS_LIST_TRUNCWORDS_COUNT')
return context
2014-01-04 17:07:09 +01:00
2014-03-07 18:04:46 +01:00
class PostDetailView(TranslatableSlugMixin, BaseBlogView, DetailView):
2014-01-04 17:07:09 +01:00
model = Post
context_object_name = 'post'
2014-09-05 08:28:10 +02:00
template_name = 'djangocms_blog/post_detail.html'
slug_field = 'slug'
view_url_name = 'djangocms_blog:post-detail'
2014-01-04 17:07:09 +01:00
2015-09-13 00:27:48 +02:00
def get_queryset(self):
queryset = self.model._default_manager.all()
if not getattr(self.request, 'toolbar', False) or not self.request.toolbar.edit_mode:
queryset = queryset.published()
return queryset
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()
2014-11-30 21:48:51 +00:00
context['use_placeholder'] = get_setting('USE_PLACEHOLDER')
setattr(self.request, BLOG_CURRENT_POST_IDENTIFIER, self.get_object())
return context
2014-01-04 17:07:09 +01:00
2014-01-04 21:13:25 +01:00
class PostArchiveView(BaseBlogView, ListView):
2014-01-04 17:07:09 +01:00
model = Post
context_object_name = 'post_list'
2014-09-05 08:28:10 +02:00
template_name = 'djangocms_blog/post_list.html'
2014-01-04 17:07:09 +01:00
date_field = 'date_published'
allow_empty = True
allow_future = True
2014-10-11 12:23:36 +02:00
paginate_by = get_setting('PAGINATION')
view_url_name = 'djangocms_blog:posts-archive'
2014-01-04 17:07:09 +01:00
def get_queryset(self):
qs = super(PostArchiveView, self).get_queryset()
if 'month' in self.kwargs:
2014-09-05 08:28:10 +02:00
qs = qs.filter(**{'%s__month' % self.date_field: self.kwargs['month']})
2014-01-04 17:07:09 +01:00
if 'year' in self.kwargs:
2014-09-05 08:28:10 +02:00
qs = qs.filter(**{'%s__year' % self.date_field: self.kwargs['year']})
2014-01-04 17:07:09 +01:00
return qs
def get_context_data(self, **kwargs):
kwargs['month'] = int(self.kwargs.get('month')) if 'month' in self.kwargs else None
kwargs['year'] = int(self.kwargs.get('year')) if 'year' in self.kwargs else None
if kwargs['year']:
2014-08-13 23:59:08 +02:00
kwargs['archive_date'] = now().replace(kwargs['year'], kwargs['month'] or 1, 1)
context = super(PostArchiveView, self).get_context_data(**kwargs)
context['TRUNCWORDS_COUNT'] = get_setting('POSTS_LIST_TRUNCWORDS_COUNT')
return context
2014-01-04 17:07:09 +01:00
class TaggedListView(BaseBlogView, ListView):
model = Post
context_object_name = 'post_list'
2014-09-05 08:28:10 +02:00
template_name = 'djangocms_blog/post_list.html'
2014-10-11 12:23:36 +02:00
paginate_by = get_setting('PAGINATION')
view_url_name = 'djangocms_blog:posts-tagged'
2014-01-04 17:07:09 +01:00
def get_queryset(self):
qs = super(TaggedListView, self).get_queryset()
return qs.filter(tags__slug=self.kwargs['tag'])
def get_context_data(self, **kwargs):
kwargs['tagged_entries'] = (self.kwargs.get('tag')
if 'tag' in self.kwargs else None)
context = super(TaggedListView, self).get_context_data(**kwargs)
context['TRUNCWORDS_COUNT'] = get_setting('POSTS_LIST_TRUNCWORDS_COUNT')
return context
2014-01-04 17:07:09 +01:00
class AuthorEntriesView(BaseBlogView, ListView):
model = Post
context_object_name = 'post_list'
2014-09-05 08:28:10 +02:00
template_name = 'djangocms_blog/post_list.html'
2014-10-11 12:23:36 +02:00
paginate_by = get_setting('PAGINATION')
view_url_name = 'djangocms_blog:posts-authors'
2014-01-04 17:07:09 +01:00
def get_queryset(self):
qs = super(AuthorEntriesView, self).get_queryset()
if 'username' in self.kwargs:
2014-09-04 09:16:56 +02:00
qs = qs.filter(**{'author__%s' % User.USERNAME_FIELD: self.kwargs['username']})
2014-01-04 17:07:09 +01:00
return qs
def get_context_data(self, **kwargs):
2014-09-04 09:16:56 +02:00
kwargs['author'] = User.objects.get(**{User.USERNAME_FIELD: self.kwargs.get('username')})
context = super(AuthorEntriesView, self).get_context_data(**kwargs)
context['TRUNCWORDS_COUNT'] = get_setting('POSTS_LIST_TRUNCWORDS_COUNT')
return context
2014-01-08 22:33:20 +01:00
2014-01-09 09:54:40 +01:00
2014-01-08 22:33:20 +01:00
class CategoryEntriesView(BaseBlogView, ListView):
model = Post
context_object_name = 'post_list'
2014-09-05 08:28:10 +02:00
template_name = 'djangocms_blog/post_list.html'
2014-01-08 22:33:20 +01:00
_category = None
2014-10-11 12:23:36 +02:00
paginate_by = get_setting('PAGINATION')
view_url_name = 'djangocms_blog:posts-category'
2014-01-08 22:33:20 +01:00
@property
def category(self):
if not self._category:
2015-09-13 00:33:13 +02:00
self._category = BlogCategory.objects.active_translations(
get_language(), slug=self.kwargs['category']
).get()
2014-01-08 22:33:20 +01:00
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)
2014-01-08 22:33:20 +01:00
def get_queryset(self):
qs = super(CategoryEntriesView, self).get_queryset()
2015-07-17 16:57:30 +03:00
if 'category' in self.kwargs:
2014-01-08 22:33:20 +01:00
qs = qs.filter(categories=self.category.pk)
return qs
def get_context_data(self, **kwargs):
kwargs['category'] = self.category
context = super(CategoryEntriesView, self).get_context_data(**kwargs)
context['TRUNCWORDS_COUNT'] = get_setting('POSTS_LIST_TRUNCWORDS_COUNT')
return context