From 8d69fb107ed5b5a1e283cbb2d7767cccbf3f47e6 Mon Sep 17 00:00:00 2001 From: Iacopo Spalletti Date: Sun, 6 Sep 2015 13:40:20 +0200 Subject: [PATCH] Add customisable templates --- djangocms_blog/cms_appconfig.py | 4 ++++ djangocms_blog/cms_plugins.py | 20 ++++++++++++++------ djangocms_blog/views.py | 20 ++++++++++++++------ tests/test_plugins.py | 19 +++++++++++++++++++ tests/test_views.py | 20 ++++++++++++++++++++ 5 files changed, 71 insertions(+), 12 deletions(-) diff --git a/djangocms_blog/cms_appconfig.py b/djangocms_blog/cms_appconfig.py index 5c4cdc8..dc55dab 100644 --- a/djangocms_blog/cms_appconfig.py +++ b/djangocms_blog/cms_appconfig.py @@ -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) diff --git a/djangocms_blog/cms_plugins.py b/djangocms_blog/cms_plugins.py index d115b78..aab204a 100644 --- a/djangocms_blog/cms_plugins.py +++ b/djangocms_blog/cms_plugins.py @@ -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) diff --git a/djangocms_blog/views.py b/djangocms_blog/views.py index 87268d4..d0ec7b5 100644 --- a/djangocms_blog/views.py +++ b/djangocms_blog/views.py @@ -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' diff --git a/tests/test_plugins.py b/tests/test_plugins.py index 6e8b640..4b00943 100644 --- a/tests/test_plugins.py +++ b/tests/test_plugins.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- from __future__ import absolute_import, print_function, unicode_literals +import os.path import re from cms.api import add_plugin @@ -132,3 +133,21 @@ class PluginTest(BaseTest): 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]['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() + diff --git a/tests/test_views.py b/tests/test_views.py index adc716b..ae921c0 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -2,6 +2,8 @@ from __future__ import absolute_import, print_function, unicode_literals from aldryn_apphooks_config.utils import get_app_instance +import os.path + from django.contrib.auth.models import AnonymousUser from django.http import Http404 from django.utils.timezone import now @@ -299,3 +301,21 @@ class ViewTest(BaseTest): self.assertEqual(sitemap.items().count(), 3) for item in sitemap.items(): 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()