Add customisable templates
This commit is contained in:
parent
a1497360b1
commit
8d69fb107e
5 changed files with 71 additions and 12 deletions
|
@ -42,4 +42,8 @@ class BlogConfigForm(AppDataForm):
|
||||||
initial=get_setting('PAGINATION'),
|
initial=get_setting('PAGINATION'),
|
||||||
help_text=_('When paginating list views, '
|
help_text=_('When paginating list views, '
|
||||||
'how many articles per page?'))
|
'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)
|
setup_config(BlogConfigForm, BlogConfig)
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from __future__ import absolute_import, print_function, unicode_literals
|
from __future__ import absolute_import, print_function, unicode_literals
|
||||||
|
|
||||||
|
import os.path
|
||||||
|
|
||||||
from cms.plugin_base import CMSPluginBase
|
from cms.plugin_base import CMSPluginBase
|
||||||
from cms.plugin_pool import plugin_pool
|
from cms.plugin_pool import plugin_pool
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
@ -13,19 +15,25 @@ from .settings import get_setting
|
||||||
class BlogPlugin(CMSPluginBase):
|
class BlogPlugin(CMSPluginBase):
|
||||||
module = 'Blog'
|
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):
|
class BlogLatestEntriesPlugin(BlogPlugin):
|
||||||
"""
|
"""
|
||||||
Non cached plugin which returns the latest posts taking into account the
|
Non cached plugin which returns the latest posts taking into account the
|
||||||
user / toolbar state
|
user / toolbar state
|
||||||
"""
|
"""
|
||||||
render_template = 'djangocms_blog/plugins/latest_entries.html'
|
|
||||||
name = _('Latest Blog Articles')
|
name = _('Latest Blog Articles')
|
||||||
model = LatestPostsPlugin
|
model = LatestPostsPlugin
|
||||||
form = LatestEntriesForm
|
form = LatestEntriesForm
|
||||||
filter_horizontal = ('categories',)
|
filter_horizontal = ('categories',)
|
||||||
fields = ('latest_posts', 'tags', 'categories')
|
fields = ('latest_posts', 'tags', 'categories')
|
||||||
cache = False
|
cache = False
|
||||||
|
base_render_template = 'plugins/latest_entries.html'
|
||||||
|
|
||||||
def render(self, context, instance, placeholder):
|
def render(self, context, instance, placeholder):
|
||||||
context = super(BlogLatestEntriesPlugin, self).render(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
|
Cached plugin which returns the latest published posts
|
||||||
"""
|
"""
|
||||||
render_template = 'djangocms_blog/plugins/latest_entries.html'
|
|
||||||
name = _('Latest Blog Articles')
|
name = _('Latest Blog Articles')
|
||||||
model = LatestPostsPlugin
|
model = LatestPostsPlugin
|
||||||
form = LatestEntriesForm
|
form = LatestEntriesForm
|
||||||
filter_horizontal = ('categories',)
|
filter_horizontal = ('categories',)
|
||||||
fields = ('latest_posts', 'tags', 'categories')
|
fields = ('latest_posts', 'tags', 'categories')
|
||||||
|
base_render_template = 'plugins/latest_entries.html'
|
||||||
|
|
||||||
def render(self, context, instance, placeholder):
|
def render(self, context, instance, placeholder):
|
||||||
context = super(BlogLatestEntriesPluginCached, self).render(context, instance, placeholder)
|
context = super(BlogLatestEntriesPluginCached, self).render(context, instance, placeholder)
|
||||||
|
@ -56,7 +64,7 @@ class BlogAuthorPostsPlugin(BlogPlugin):
|
||||||
module = _('Blog')
|
module = _('Blog')
|
||||||
name = _('Author Blog Articles')
|
name = _('Author Blog Articles')
|
||||||
model = AuthorEntriesPlugin
|
model = AuthorEntriesPlugin
|
||||||
render_template = 'djangocms_blog/plugins/authors.html'
|
base_render_template = 'plugins/authors.html'
|
||||||
filter_horizontal = ['authors']
|
filter_horizontal = ['authors']
|
||||||
|
|
||||||
def render(self, context, instance, placeholder):
|
def render(self, context, instance, placeholder):
|
||||||
|
@ -69,7 +77,7 @@ class BlogTagsPlugin(BlogPlugin):
|
||||||
module = _('Blog')
|
module = _('Blog')
|
||||||
name = _('Tags')
|
name = _('Tags')
|
||||||
model = GenericBlogPlugin
|
model = GenericBlogPlugin
|
||||||
render_template = 'djangocms_blog/plugins/tags.html'
|
base_render_template = 'plugins/tags.html'
|
||||||
|
|
||||||
def render(self, context, instance, placeholder):
|
def render(self, context, instance, placeholder):
|
||||||
context = super(BlogTagsPlugin, self).render(context, instance, placeholder)
|
context = super(BlogTagsPlugin, self).render(context, instance, placeholder)
|
||||||
|
@ -85,7 +93,7 @@ class BlogCategoryPlugin(BlogPlugin):
|
||||||
module = _('Blog')
|
module = _('Blog')
|
||||||
name = _('Categories')
|
name = _('Categories')
|
||||||
model = GenericBlogPlugin
|
model = GenericBlogPlugin
|
||||||
render_template = 'djangocms_blog/plugins/categories.html'
|
base_render_template = 'plugins/categories.html'
|
||||||
|
|
||||||
def render(self, context, instance, placeholder):
|
def render(self, context, instance, placeholder):
|
||||||
context = super(BlogCategoryPlugin, self).render(context, instance, placeholder)
|
context = super(BlogCategoryPlugin, self).render(context, instance, placeholder)
|
||||||
|
@ -100,7 +108,7 @@ class BlogArchivePlugin(BlogPlugin):
|
||||||
module = _('Blog')
|
module = _('Blog')
|
||||||
name = _('Archive')
|
name = _('Archive')
|
||||||
model = GenericBlogPlugin
|
model = GenericBlogPlugin
|
||||||
render_template = 'djangocms_blog/plugins/archive.html'
|
base_render_template = 'plugins/archive.html'
|
||||||
|
|
||||||
def render(self, context, instance, placeholder):
|
def render(self, context, instance, placeholder):
|
||||||
context = super(BlogArchivePlugin, self).render(context, instance, placeholder)
|
context = super(BlogArchivePlugin, self).render(context, instance, placeholder)
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from __future__ import absolute_import, print_function, unicode_literals
|
from __future__ import absolute_import, print_function, unicode_literals
|
||||||
|
|
||||||
|
import os.path
|
||||||
|
|
||||||
from aldryn_apphooks_config.mixins import AppConfigMixin
|
from aldryn_apphooks_config.mixins import AppConfigMixin
|
||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
from django.utils.timezone import now
|
from django.utils.timezone import now
|
||||||
|
@ -27,11 +29,17 @@ class BaseBlogView(AppConfigMixin, ViewUrlMixin):
|
||||||
queryset = queryset.published()
|
queryset = queryset.published()
|
||||||
return queryset
|
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):
|
class PostListView(BaseBlogView, ListView):
|
||||||
model = Post
|
model = Post
|
||||||
context_object_name = 'post_list'
|
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'
|
view_url_name = 'djangocms_blog:posts-latest'
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
|
@ -46,7 +54,7 @@ class PostListView(BaseBlogView, ListView):
|
||||||
class PostDetailView(TranslatableSlugMixin, BaseBlogView, DetailView):
|
class PostDetailView(TranslatableSlugMixin, BaseBlogView, DetailView):
|
||||||
model = Post
|
model = Post
|
||||||
context_object_name = 'post'
|
context_object_name = 'post'
|
||||||
template_name = 'djangocms_blog/post_detail.html'
|
base_template_name = 'post_detail.html'
|
||||||
slug_field = 'slug'
|
slug_field = 'slug'
|
||||||
view_url_name = 'djangocms_blog:post-detail'
|
view_url_name = 'djangocms_blog:post-detail'
|
||||||
|
|
||||||
|
@ -73,7 +81,7 @@ class PostDetailView(TranslatableSlugMixin, BaseBlogView, DetailView):
|
||||||
class PostArchiveView(BaseBlogView, ListView):
|
class PostArchiveView(BaseBlogView, ListView):
|
||||||
model = Post
|
model = Post
|
||||||
context_object_name = 'post_list'
|
context_object_name = 'post_list'
|
||||||
template_name = 'djangocms_blog/post_list.html'
|
base_template_name = 'post_list.html'
|
||||||
date_field = 'date_published'
|
date_field = 'date_published'
|
||||||
allow_empty = True
|
allow_empty = True
|
||||||
allow_future = True
|
allow_future = True
|
||||||
|
@ -101,7 +109,7 @@ class PostArchiveView(BaseBlogView, ListView):
|
||||||
class TaggedListView(BaseBlogView, ListView):
|
class TaggedListView(BaseBlogView, ListView):
|
||||||
model = Post
|
model = Post
|
||||||
context_object_name = 'post_list'
|
context_object_name = 'post_list'
|
||||||
template_name = 'djangocms_blog/post_list.html'
|
base_template_name = 'post_list.html'
|
||||||
paginate_by = get_setting('PAGINATION')
|
paginate_by = get_setting('PAGINATION')
|
||||||
view_url_name = 'djangocms_blog:posts-tagged'
|
view_url_name = 'djangocms_blog:posts-tagged'
|
||||||
|
|
||||||
|
@ -120,7 +128,7 @@ class TaggedListView(BaseBlogView, ListView):
|
||||||
class AuthorEntriesView(BaseBlogView, ListView):
|
class AuthorEntriesView(BaseBlogView, ListView):
|
||||||
model = Post
|
model = Post
|
||||||
context_object_name = 'post_list'
|
context_object_name = 'post_list'
|
||||||
template_name = 'djangocms_blog/post_list.html'
|
base_template_name = 'post_list.html'
|
||||||
paginate_by = get_setting('PAGINATION')
|
paginate_by = get_setting('PAGINATION')
|
||||||
view_url_name = 'djangocms_blog:posts-authors'
|
view_url_name = 'djangocms_blog:posts-authors'
|
||||||
|
|
||||||
|
@ -140,7 +148,7 @@ class AuthorEntriesView(BaseBlogView, ListView):
|
||||||
class CategoryEntriesView(BaseBlogView, ListView):
|
class CategoryEntriesView(BaseBlogView, ListView):
|
||||||
model = Post
|
model = Post
|
||||||
context_object_name = 'post_list'
|
context_object_name = 'post_list'
|
||||||
template_name = 'djangocms_blog/post_list.html'
|
base_template_name = 'post_list.html'
|
||||||
_category = None
|
_category = None
|
||||||
paginate_by = get_setting('PAGINATION')
|
paginate_by = get_setting('PAGINATION')
|
||||||
view_url_name = 'djangocms_blog:posts-category'
|
view_url_name = 'djangocms_blog:posts-category'
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from __future__ import absolute_import, print_function, unicode_literals
|
from __future__ import absolute_import, print_function, unicode_literals
|
||||||
|
|
||||||
|
import os.path
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from cms.api import add_plugin
|
from cms.api import add_plugin
|
||||||
|
@ -132,3 +133,21 @@ class PluginTest(BaseTest):
|
||||||
context = plugin_class.render(context, plugin, ph)
|
context = plugin_class.render(context, plugin, ph)
|
||||||
self.assertEqual(context['dates'][0]['date'].date(), now().replace(year=now().year, month=now().month, day=1).date())
|
self.assertEqual(context['dates'][0]['date'].date(), now().replace(year=now().year, month=now().month, day=1).date())
|
||||||
self.assertEqual(context['dates'][0]['count'], 1)
|
self.assertEqual(context['dates'][0]['count'], 1)
|
||||||
|
|
||||||
|
def test_templates(self):
|
||||||
|
posts = self.get_posts()
|
||||||
|
pages = self.get_pages()
|
||||||
|
|
||||||
|
ph = pages[0].placeholders.get(slot='content')
|
||||||
|
plugin = add_plugin(ph, 'BlogLatestEntriesPlugin', language='en', app_config=self.app_config_1)
|
||||||
|
|
||||||
|
context = self.get_plugin_context(pages[0], 'en', plugin)
|
||||||
|
plugin_class = plugin.get_plugin_class_instance()
|
||||||
|
self.assertEqual(plugin_class.get_render_template(context, plugin, ph), os.path.join('djangocms_blog', plugin_class.base_render_template))
|
||||||
|
|
||||||
|
self.app_config_1.app_data.config.template_prefix = 'whatever'
|
||||||
|
self.app_config_1.save()
|
||||||
|
self.assertEqual(plugin_class.get_render_template(context, plugin, ph), os.path.join('whatever', plugin_class.base_render_template))
|
||||||
|
self.app_config_1.app_data.config.template_prefix = ''
|
||||||
|
self.app_config_1.save()
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
from __future__ import absolute_import, print_function, unicode_literals
|
from __future__ import absolute_import, print_function, unicode_literals
|
||||||
from aldryn_apphooks_config.utils import get_app_instance
|
from aldryn_apphooks_config.utils import get_app_instance
|
||||||
|
|
||||||
|
import os.path
|
||||||
|
|
||||||
from django.contrib.auth.models import AnonymousUser
|
from django.contrib.auth.models import AnonymousUser
|
||||||
from django.http import Http404
|
from django.http import Http404
|
||||||
from django.utils.timezone import now
|
from django.utils.timezone import now
|
||||||
|
@ -299,3 +301,21 @@ class ViewTest(BaseTest):
|
||||||
self.assertEqual(sitemap.items().count(), 3)
|
self.assertEqual(sitemap.items().count(), 3)
|
||||||
for item in sitemap.items():
|
for item in sitemap.items():
|
||||||
self.assertTrue(sitemap.lastmod(item).date(), now().today())
|
self.assertTrue(sitemap.lastmod(item).date(), now().today())
|
||||||
|
|
||||||
|
def test_templates(self):
|
||||||
|
posts = self.get_posts()
|
||||||
|
pages = self.get_pages()
|
||||||
|
|
||||||
|
with smart_override('en'):
|
||||||
|
request = self.get_page_request(pages[1], self.user, edit=True)
|
||||||
|
view_obj = PostListView()
|
||||||
|
view_obj.request = request
|
||||||
|
view_obj.namespace = self.app_config_1.namespace
|
||||||
|
view_obj.config = self.app_config_1
|
||||||
|
self.assertEqual(view_obj.get_template_names(), os.path.join('djangocms_blog', 'post_list.html'))
|
||||||
|
|
||||||
|
self.app_config_1.app_data.config.template_prefix = 'whatever'
|
||||||
|
self.app_config_1.save()
|
||||||
|
self.assertEqual(view_obj.get_template_names(), os.path.join('whatever', 'post_list.html'))
|
||||||
|
self.app_config_1.app_data.config.template_prefix = ''
|
||||||
|
self.app_config_1.save()
|
||||||
|
|
Loading…
Add table
Reference in a new issue