From 014b94449b4feefc81a997892d21440d88e842b9 Mon Sep 17 00:00:00 2001 From: Fabian Braun Date: Wed, 6 Jul 2016 13:31:58 +0200 Subject: [PATCH] Feature-add: Plugins have a field that allows to specify a (sub-)folder from which the plugin templates are loaded. Default folder is "plugins". It goes into the "djangocms_blog" template folder (or, if set, the folder named in the app hook). This allows, e.g., different templates for showing a post list as tables, columns, or you name it. New templates have the same names as the standard templates in the "plugins" folder ("latest_entries.html", "authors.html", "tags.html", "categories.html", "archive.html"). The folder options can be defined in a new setting called BLOG_PLUGIN_TEMPLATE_FOLDERS. It's default is "( ("plugins", _("Default template") )" to be compatible with hitherto configurations. To add new templates write in settings.py, e.g., BLOG_PLUGIN_TEMPLATE_FOLDERS = ( ('plugins', _('Default template') ), # reads from "templates/djangocms_blog/plugins/ ('timeline', _('Vertical timeline') ), # reads from "templates/djangocms_blog/vertical/ ('masonry', _('Masonry style') ), # reads from "templates/djangocms_blog/masonry/ ) --- djangocms_blog/cms_plugins.py | 20 ++++++++++++-------- djangocms_blog/models.py | 8 ++++++++ djangocms_blog/settings.py | 3 +++ 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/djangocms_blog/cms_plugins.py b/djangocms_blog/cms_plugins.py index 70bb4b5..91dac35 100644 --- a/djangocms_blog/cms_plugins.py +++ b/djangocms_blog/cms_plugins.py @@ -18,9 +18,13 @@ class BlogPlugin(CMSPluginBase): def get_render_template(self, context, instance, placeholder): if instance.app_config and instance.app_config.template_prefix: - return os.path.join(instance.app_config.template_prefix, self.base_render_template) + return os.path.join(instance.app_config.template_prefix, + instance.template_folder, + self.base_render_template) else: - return os.path.join('djangocms_blog', self.base_render_template) + return os.path.join('djangocms_blog', + instance.template_folder, + self.base_render_template) class BlogLatestEntriesPlugin(BlogPlugin): @@ -34,7 +38,7 @@ class BlogLatestEntriesPlugin(BlogPlugin): filter_horizontal = ('categories',) fields = ('app_config', 'latest_posts', 'tags', 'categories') cache = False - base_render_template = 'plugins/latest_entries.html' + base_render_template = 'latest_entries.html' def render(self, context, instance, placeholder): context = super(BlogLatestEntriesPlugin, self).render(context, instance, placeholder) @@ -52,7 +56,7 @@ class BlogLatestEntriesPluginCached(BlogPlugin): form = LatestEntriesForm filter_horizontal = ('categories',) fields = ('app_config', 'latest_posts', 'tags', 'categories') - base_render_template = 'plugins/latest_entries.html' + base_render_template = 'latest_entries.html' def render(self, context, instance, placeholder): context = super(BlogLatestEntriesPluginCached, self).render(context, instance, placeholder) @@ -65,7 +69,7 @@ class BlogAuthorPostsPlugin(BlogPlugin): module = get_setting('PLUGIN_MODULE_NAME') name = get_setting('AUTHOR_POSTS_PLUGIN_NAME') model = AuthorEntriesPlugin - base_render_template = 'plugins/authors.html' + base_render_template = 'authors.html' filter_horizontal = ['authors'] def render(self, context, instance, placeholder): @@ -78,7 +82,7 @@ class BlogTagsPlugin(BlogPlugin): module = get_setting('PLUGIN_MODULE_NAME') name = get_setting('TAGS_PLUGIN_NAME') model = GenericBlogPlugin - base_render_template = 'plugins/tags.html' + base_render_template = 'tags.html' def render(self, context, instance, placeholder): context = super(BlogTagsPlugin, self).render(context, instance, placeholder) @@ -91,7 +95,7 @@ class BlogCategoryPlugin(BlogPlugin): module = get_setting('PLUGIN_MODULE_NAME') name = get_setting('CATEGORY_PLUGIN_NAME') model = GenericBlogPlugin - base_render_template = 'plugins/categories.html' + base_render_template = 'categories.html' def render(self, context, instance, placeholder): context = super(BlogCategoryPlugin, self).render(context, instance, placeholder) @@ -111,7 +115,7 @@ class BlogArchivePlugin(BlogPlugin): module = get_setting('PLUGIN_MODULE_NAME') name = get_setting('ARCHIVE_PLUGIN_NAME') model = GenericBlogPlugin - base_render_template = 'plugins/archive.html' + base_render_template = 'archive.html' def render(self, context, instance, placeholder): context = super(BlogArchivePlugin, self).render(context, instance, placeholder) diff --git a/djangocms_blog/models.py b/djangocms_blog/models.py index 91df778..7e040de 100644 --- a/djangocms_blog/models.py +++ b/djangocms_blog/models.py @@ -33,6 +33,7 @@ from .settings import get_setting BLOG_CURRENT_POST_IDENTIFIER = get_setting('CURRENT_POST_IDENTIFIER') BLOG_CURRENT_NAMESPACE = get_setting('CURRENT_NAMESPACE') +BLOG_PLUGIN_TEMPLATE_FOLDERS = get_setting('PLUGIN_TEMPLATE_FOLDERS') try: # pragma: no cover from cmsplugin_filer_image.models import ThumbnailOption # NOQA @@ -378,6 +379,13 @@ class BasePostPlugin(CMSPlugin): current_site = models.BooleanField( _('current site'), default=True, help_text=_('Select items from the current site only') ) + template_folder = models.CharField( + max_length = 40, + verbose_name = _('Plugin template'), + help_text = _('Select plugin template to load for this instance'), + default = BLOG_PLUGIN_TEMPLATE_FOLDERS[0][1], + choices = BLOG_PLUGIN_TEMPLATE_FOLDERS + ) class Meta: abstract = True diff --git a/djangocms_blog/settings.py b/djangocms_blog/settings.py index 0199182..790a1d9 100644 --- a/djangocms_blog/settings.py +++ b/djangocms_blog/settings.py @@ -135,5 +135,8 @@ def get_setting(name): 'BLOG_LIVEBLOG_PLUGINS': getattr( settings, 'BLOG_LIVEBLOG_PLUGINS', ('LiveblogPlugin',)), + 'BLOG_PLUGIN_TEMPLATE_FOLDERS': getattr( + settings, 'BLOG_PLUGIN_TEMPLATE_FOLDERS', (('plugins', _('Default template')),) ), + } return default['BLOG_%s' % name]