Categories plugin

This commit is contained in:
Iacopo Spalletti 2014-01-08 22:33:20 +01:00
parent 67d5c3cd99
commit 086dde879d
4 changed files with 54 additions and 8 deletions

View file

@ -5,7 +5,7 @@ from cms.models.pluginmodel import CMSPlugin
from cms.plugin_base import CMSPluginBase
from cms.plugin_pool import plugin_pool
from .models import AuthorEntriesPlugin, LatestPostsPlugin, Post
from .models import AuthorEntriesPlugin, LatestPostsPlugin, Post, BlogCategory
from .forms import LatestEntriesForm
@ -13,7 +13,6 @@ class BlogPlugin(CMSPluginBase):
module = 'Blog'
class LatestEntriesPlugin(BlogPlugin):
render_template = 'djangocms_blog/plugins/latest_entries.html'
@ -26,7 +25,6 @@ class LatestEntriesPlugin(BlogPlugin):
context['instance'] = instance
return context
class AuthorPostsPlugin(BlogPlugin):
module = _('Blog')
name = _('Author Blog posts')
@ -38,7 +36,6 @@ class AuthorPostsPlugin(BlogPlugin):
context['instance'] = instance
return context
class BlogTagsPlugin(BlogPlugin):
module = _('Blog')
name = _('Tags')
@ -49,6 +46,15 @@ class BlogTagsPlugin(BlogPlugin):
context['tags'] = Post.objects.tag_cloud(queryset=Post.objects.published())
return context
class BlogCategoryPlugin(BlogPlugin):
module = _('Blog')
name = _('Categories')
model = CMSPlugin
render_template = 'djangocms_blog/plugins/categories.html'
def render(self, context, instance, placeholder):
context['categories'] = BlogCategory.objects.all()
return context
class BlogArchivePlugin(BlogPlugin):
module = _('Blog')
@ -64,3 +70,4 @@ plugin_pool.register_plugin(LatestEntriesPlugin)
plugin_pool.register_plugin(AuthorPostsPlugin)
plugin_pool.register_plugin(BlogTagsPlugin)
plugin_pool.register_plugin(BlogArchivePlugin)
plugin_pool.register_plugin(BlogCategoryPlugin)

View file

@ -2,6 +2,7 @@
from cms.models import PlaceholderField, CMSPlugin
from cmsplugin_filer_image.models import ThumbnailOption
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.core.urlresolvers import reverse
from django.db import models
from django.utils import timezone
@ -33,6 +34,10 @@ class BlogCategory(TranslatableModel):
class Meta:
verbose_name = _('blog category')
verbose_name_plural = _('blog categories')
@property
def count(self):
return self.blog_posts.count()
def __unicode__(self):
return self.lazy_translation_getter('name')
@ -107,6 +112,14 @@ class Post(TranslatableModel):
return self.main_image_thumbnail.as_dict
else:
return settings.BLOG_IMAGE_THUMBNAIL_SIZE
def get_full_url(self):
s = Site.objects.get_current()
if s.domain.find('http') > -1:
return "%s%s" % (s.domain, self.get_absolute_url())
else:
return "http://%s%s" % (s.domain, self.get_absolute_url())
def full_image_options(self):
if self.main_image_fulll_id:

View file

@ -2,7 +2,7 @@
from django.conf.urls import patterns, url
from .views import (PostListView, PostDetailView, TaggedListView,
AuthorEntriesView, PostArchiveView)
AuthorEntriesView, PostArchiveView, CategoryEntriesView)
from .feeds import LatestEntriesFeed, TagFeed
@ -13,7 +13,8 @@ urlpatterns = patterns(
url(r'^(?P<year>\d{4})/$', PostArchiveView.as_view(), name='archive-year'),
url(r'^(?P<year>\d{4})/(?P<month>\d{1,2})/$', PostArchiveView.as_view(), name='archive-month'),
url(r'^(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/(?P<slug>\w[-\w]*)/$', PostDetailView.as_view(), name='post-detail'),
url(r'^author/(?P<username>[\w.@+-]+)/$', AuthorEntriesView.as_view(), name='author-posts'),
url(r'^author/(?P<username>[\w\.@+-]+)/$', AuthorEntriesView.as_view(), name='author-posts'),
url(r'^category/(?P<category>[\w\.@+-]+)/$', CategoryEntriesView.as_view(), name='category-posts'),
url(r'^tag/(?P<tag>[-\w]+)/$', TaggedListView.as_view(), name='tagged-posts'),
url(r'^tag/(?P<tag>[-\w]+)/feed/$', TagFeed(), name='tagged-posts-feed'),
)

View file

@ -1,12 +1,14 @@
# -*- coding: utf-8 -*-
import datetime
from django.contrib.auth.models import User
from django.core.urlresolvers import resolve
from django.db.models import Count
from django.utils.translation import ugettext_lazy as _
from django.views.generic import ListView, DetailView
from hvad.admin import TranslatableModelAdminMixin
from .models import Post
from .models import Post, BlogCategory
class BaseBlogView(TranslatableModelAdminMixin):
@ -86,5 +88,28 @@ class AuthorEntriesView(BaseBlogView, ListView):
return qs
def get_context_data(self, **kwargs):
kwargs['author'] = self.kwargs.get('username')
kwargs['author'] = User.objects.get(username=self.kwargs.get('username'))
return super(AuthorEntriesView, self).get_context_data(**kwargs)
class CategoryEntriesView(BaseBlogView, ListView):
model = Post
context_object_name = 'post_list'
template_name = "djangocms_blog/post_list.html"
_category = None
@property
def category(self):
if not self._category:
language = self._language(self.request)
self._category = BlogCategory._default_manager.language(language).get(slug=self.kwargs['category'])
return self._category
def get_queryset(self):
qs = super(CategoryEntriesView, self).get_queryset()
if 'category' in self.kwargs:
qs = qs.filter(categories=self.category.pk)
return qs
def get_context_data(self, **kwargs):
kwargs['category'] = self.category
return super(CategoryEntriesView, self).get_context_data(**kwargs)