Skip to content

Commit

Permalink
Fix error when overriding templates folder (#562)
Browse files Browse the repository at this point in the history
* Default plugin templates to djangocms_blog base versions.

If a template_prefix has been set in the blog app_config, any
plugin will cause a TemplateDoesNotExist exception unless the
entire template directory is copied.

Co-authored-by: Brian Schott <[email protected]>
  • Loading branch information
yakky and electroniceagle authored May 15, 2020
1 parent 62b5968 commit 68f4577
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 21 deletions.
18 changes: 11 additions & 7 deletions djangocms_blog/cms_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from cms.plugin_pool import plugin_pool
from django.contrib.sites.shortcuts import get_current_site
from django.db import models
from django.template.loader import select_template

from .forms import AuthorPostsForm, LatestEntriesForm
from .models import AuthorEntriesPlugin, BlogCategory, GenericBlogPlugin, LatestPostsPlugin, Post
Expand All @@ -17,14 +18,17 @@ class BlogPlugin(CMSPluginBase):
module = get_setting('PLUGIN_MODULE_NAME')

def get_render_template(self, context, instance, placeholder):
templates = [
os.path.join('djangocms_blog', instance.template_folder, self.base_render_template)
]
if instance.app_config and instance.app_config.template_prefix:
return os.path.join(instance.app_config.template_prefix,
instance.template_folder,
self.base_render_template)
else:
return os.path.join('djangocms_blog',
instance.template_folder,
self.base_render_template)
templates.insert(0, os.path.join(
instance.app_config.template_prefix, instance.template_folder,
self.base_render_template
))

selected = select_template(templates)
return selected.template.name


class BlogLatestEntriesPlugin(BlogPlugin):
Expand Down
52 changes: 38 additions & 14 deletions tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,28 +200,52 @@ def test_blog_archive_plugin(self):
self.assertEqual(context['dates'][0]['count'], 1)

def test_templates(self):
def _test_custom_templates_path(parts):
templates_path = os.path.join(os.path.dirname(__file__), 'test_utils', 'templates')

self.app_config_1.app_data.config.template_prefix = parts[0]
self.app_config_1.save()
tmp = plugin.template_folder
plugin.template_folder = parts[1]
plugin.save()
dir_parts = (templates_path,) + parts
template_parts = parts + (plugin_class.base_render_template, )
try:
os.makedirs(os.path.join(*dir_parts))
except OSError:
pass
fake_template = os.path.join(*template_parts)
with open(os.path.join(templates_path, fake_template), 'w'):
self.assertEqual(
plugin_class.get_render_template(context, plugin, ph),
fake_template
)
plugin.template_folder = tmp
plugin.save()
self.app_config_1.app_data.config.template_prefix = ''
self.app_config_1.save()
os.unlink(os.path.join(templates_path, fake_template))

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)
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.template_folder, plugin_class.base_render_template))
self.assertEqual(
plugin_class.get_render_template(context, plugin, ph),
os.path.join('djangocms_blog', plugin.template_folder, plugin_class.base_render_template)
)

self.app_config_1.app_data.config.template_prefix = 'whatever'
self.app_config_1.save()
tmp = plugin.template_folder
plugin.template_folder = 'whereever'
plugin.save()
self.assertEqual(plugin_class.get_render_template(context, plugin, ph),
os.path.join('whatever', 'whereever', plugin_class.base_render_template))
plugin.template_folder = tmp
plugin.save()
self.app_config_1.app_data.config.template_prefix = ''
self.app_config_1.save()
custom_parts = ('whatever', 'whereever')
_test_custom_templates_path(custom_parts)

custom_parts = ('djangocms_blog', 'whereever')
_test_custom_templates_path(custom_parts)


class PluginTest10(BaseTest):
Expand Down

0 comments on commit 68f4577

Please sign in to comment.