Add customisable templates

This commit is contained in:
Iacopo Spalletti 2015-09-06 13:40:20 +02:00
commit 8d69fb107e
5 changed files with 71 additions and 12 deletions

View file

@ -42,4 +42,8 @@ class BlogConfigForm(AppDataForm):
initial=get_setting('PAGINATION'),
help_text=_('When paginating list views, '
'how many articles per page?'))
template_prefix = forms.CharField(label=_('Template prefix'), required=False, initial='',
help_text=_('Alternative directory to load the blog '
'templates from')
)
setup_config(BlogConfigForm, BlogConfig)

View file

@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals
import os.path
from cms.plugin_base import CMSPluginBase
from cms.plugin_pool import plugin_pool
from django.utils.translation import ugettext_lazy as _
@ -13,19 +15,25 @@ from .settings import get_setting
class BlogPlugin(CMSPluginBase):
module = 'Blog'
def get_render_template(self, context, instance, placeholder):
if instance.app_config.template_prefix:
return os.path.join(instance.app_config.template_prefix, self.base_render_template)
else:
return os.path.join('djangocms_blog', self.base_render_template)
class BlogLatestEntriesPlugin(BlogPlugin):
"""
Non cached plugin which returns the latest posts taking into account the
user / toolbar state
"""
render_template = 'djangocms_blog/plugins/latest_entries.html'
name = _('Latest Blog Articles')
model = LatestPostsPlugin
form = LatestEntriesForm
filter_horizontal = ('categories',)
fields = ('latest_posts', 'tags', 'categories')
cache = False
base_render_template = 'plugins/latest_entries.html'
def render(self, context, instance, placeholder):
context = super(BlogLatestEntriesPlugin, self).render(context, instance, placeholder)
@ -38,12 +46,12 @@ class BlogLatestEntriesPluginCached(BlogPlugin):
"""
Cached plugin which returns the latest published posts
"""
render_template = 'djangocms_blog/plugins/latest_entries.html'
name = _('Latest Blog Articles')
model = LatestPostsPlugin
form = LatestEntriesForm
filter_horizontal = ('categories',)
fields = ('latest_posts', 'tags', 'categories')
base_render_template = 'plugins/latest_entries.html'
def render(self, context, instance, placeholder):
context = super(BlogLatestEntriesPluginCached, self).render(context, instance, placeholder)
@ -56,7 +64,7 @@ class BlogAuthorPostsPlugin(BlogPlugin):
module = _('Blog')
name = _('Author Blog Articles')
model = AuthorEntriesPlugin
render_template = 'djangocms_blog/plugins/authors.html'
base_render_template = 'plugins/authors.html'
filter_horizontal = ['authors']
def render(self, context, instance, placeholder):
@ -69,7 +77,7 @@ class BlogTagsPlugin(BlogPlugin):
module = _('Blog')
name = _('Tags')
model = GenericBlogPlugin
render_template = 'djangocms_blog/plugins/tags.html'
base_render_template = 'plugins/tags.html'
def render(self, context, instance, placeholder):
context = super(BlogTagsPlugin, self).render(context, instance, placeholder)
@ -85,7 +93,7 @@ class BlogCategoryPlugin(BlogPlugin):
module = _('Blog')
name = _('Categories')
model = GenericBlogPlugin
render_template = 'djangocms_blog/plugins/categories.html'
base_render_template = 'plugins/categories.html'
def render(self, context, instance, placeholder):
context = super(BlogCategoryPlugin, self).render(context, instance, placeholder)
@ -100,7 +108,7 @@ class BlogArchivePlugin(BlogPlugin):
module = _('Blog')
name = _('Archive')
model = GenericBlogPlugin
render_template = 'djangocms_blog/plugins/archive.html'
base_render_template = 'plugins/archive.html'
def render(self, context, instance, placeholder):
context = super(BlogArchivePlugin, self).render(context, instance, placeholder)

View file

@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals
import os.path
from aldryn_apphooks_config.mixins import AppConfigMixin
from django.contrib.auth import get_user_model
from django.utils.timezone import now
@ -27,11 +29,17 @@ class BaseBlogView(AppConfigMixin, ViewUrlMixin):
queryset = queryset.published()
return queryset
def get_template_names(self):
if self.config.template_prefix:
return os.path.join(self.config.template_prefix, self.base_template_name)
else:
return os.path.join('djangocms_blog', self.base_template_name)
class PostListView(BaseBlogView, ListView):
model = Post
context_object_name = 'post_list'
template_name = 'djangocms_blog/post_list.html'
base_template_name = 'post_list.html'
view_url_name = 'djangocms_blog:posts-latest'
def get_context_data(self, **kwargs):
@ -46,7 +54,7 @@ class PostListView(BaseBlogView, ListView):
class PostDetailView(TranslatableSlugMixin, BaseBlogView, DetailView):
model = Post
context_object_name = 'post'
template_name = 'djangocms_blog/post_detail.html'
base_template_name = 'post_detail.html'
slug_field = 'slug'
view_url_name = 'djangocms_blog:post-detail'
@ -73,7 +81,7 @@ class PostDetailView(TranslatableSlugMixin, BaseBlogView, DetailView):
class PostArchiveView(BaseBlogView, ListView):
model = Post
context_object_name = 'post_list'
template_name = 'djangocms_blog/post_list.html'
base_template_name = 'post_list.html'
date_field = 'date_published'
allow_empty = True
allow_future = True
@ -101,7 +109,7 @@ class PostArchiveView(BaseBlogView, ListView):
class TaggedListView(BaseBlogView, ListView):
model = Post
context_object_name = 'post_list'
template_name = 'djangocms_blog/post_list.html'
base_template_name = 'post_list.html'
paginate_by = get_setting('PAGINATION')
view_url_name = 'djangocms_blog:posts-tagged'
@ -120,7 +128,7 @@ class TaggedListView(BaseBlogView, ListView):
class AuthorEntriesView(BaseBlogView, ListView):
model = Post
context_object_name = 'post_list'
template_name = 'djangocms_blog/post_list.html'
base_template_name = 'post_list.html'
paginate_by = get_setting('PAGINATION')
view_url_name = 'djangocms_blog:posts-authors'
@ -140,7 +148,7 @@ class AuthorEntriesView(BaseBlogView, ListView):
class CategoryEntriesView(BaseBlogView, ListView):
model = Post
context_object_name = 'post_list'
template_name = 'djangocms_blog/post_list.html'
base_template_name = 'post_list.html'
_category = None
paginate_by = get_setting('PAGINATION')
view_url_name = 'djangocms_blog:posts-category'