Skip to content

Fixes #48 #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jun 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ matrix:
# Django 2.2
- python: 3.6
env: DJANGO='dj22' CMS='cms37'
dist: xenial
sudo: true
- python: 3.7
env: DJANGO='dj22' CMS='cms37'
dist: xenial
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ Changelog
=========


2.3.0 (unreleased)
==================

* Fixed an issue where render requires a dict instead of a context
* Added ``DJANGOCMS_SNIPPET_CACHE`` cache settings for snippets


2.2.0 (2019-05-06)
==================

Expand Down
30 changes: 17 additions & 13 deletions djangocms_snippet/cms_plugins.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
from django import template
from django.template.context import Context
from django.conf import settings
from django.utils.html import escape
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
Expand All @@ -11,37 +11,41 @@
from .models import SnippetPtr


CACHE_ENABLED = getattr(settings, 'DJANGOCMS_SNIPPET_CACHE', False)


class SnippetPlugin(CMSPluginBase):
model = SnippetPtr
name = _('Snippet')
render_template = 'djangocms_snippet/snippet.html'
text_enabled = True
text_editor_preview = False
cache = getattr(settings, 'DJANGOCMS_SNIPPET_CACHE', True)
cache = CACHE_ENABLED

def render(self, context, instance, placeholder):
context.update({
'placeholder': placeholder,
'object': instance,
})
try:
if instance.snippet.template:
context = context.flatten()
t = template.loader.get_template(instance.snippet.template)
context.update({
'html': mark_safe(instance.snippet.html)
})
content = t.render(Context(context))
content = t.render(context)
else:
# only html provided
t = template.Template(instance.snippet.html)
content = t.render(Context(context))
content = t.render(context)
except template.TemplateDoesNotExist:
content = _('Template %(template)s does not exist.') % {
'template': instance.snippet.template}
'template': instance.snippet.template
}
except Exception as e:
content = escape(str(e))

context.update({
'content': mark_safe(content),
'placeholder': placeholder,
'object': instance,
'html': mark_safe(instance.snippet.html),
'content': content,
})

return context


Expand Down
4 changes: 3 additions & 1 deletion tests/settings.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
HELPER_SETTINGS = {
'INSTALLED_APPS': [],
'INSTALLED_APPS': [
'tests.utils',
],
'CMS_LANGUAGES': {
1: [{
'code': 'en',
Expand Down
76 changes: 70 additions & 6 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,81 @@
# -*- coding: utf-8 -*-
from django.test import TestCase
from cms.api import add_plugin, create_page
from cms.test_utils.testcases import CMSTestCase

from djangocms_snippet.models import Snippet


class SnippetTestCase(TestCase):
class SnippetTestCase(CMSTestCase):

def setUp(self):
Snippet.objects.create(
name='snippet',
self.superuser = self.get_superuser()
self.home = create_page(
title="home",
template="page.html",
language="en",
)
self.home.publish("en")
self.page = create_page(
title="help",
template="page.html",
language="en",
)

def tearDown(self):
self.page.delete()
self.home.delete()

def test_snippet_instance(self):
"""Snippet instance has been created"""
snippet = Snippet.objects.get(name='snippet')
self.assertEqual(snippet.name, 'snippet')
Snippet.objects.create(
name="snippet",
)
snippet = Snippet.objects.get(name="snippet")
self.assertEqual(snippet.name, "snippet")

def test_html_rendering(self):
snippet = Snippet.objects.create(
name="plugin_snippet",
html="<p>Hello World</p>",
slug="plugin_snippet",
)
plugin = add_plugin(
self.page.placeholders.get(slot="content"),
"SnippetPlugin",
"en",
snippet=snippet,
)
self.page.publish("en")
self.assertEqual(plugin.snippet.name, "plugin_snippet")
self.assertEqual(plugin.snippet.html, "<p>Hello World</p>")
self.assertEqual(plugin.snippet.slug, "plugin_snippet")

with self.login_user_context(self.superuser):
response = self.client.get(self.page.get_absolute_url('en'))

self.assertIn(b"<p>Hello World</p>", response.content)

def test_file_rendering(self):
template = "snippet.html"
snippet = Snippet.objects.create(
name="plugin_snippet",
template=template,
slug="plugin_snippet",
)
plugin = add_plugin(
self.page.placeholders.get(slot="content"),
"SnippetPlugin",
"en",
snippet=snippet,
)
self.page.publish("en")
self.assertEqual(plugin.snippet.name, "plugin_snippet")
self.assertEqual(plugin.snippet.slug, "plugin_snippet")

with self.login_user_context(self.superuser):
response = self.client.get(self.page.get_absolute_url('en'))

self.assertNotIn("Template {} does not exist".format(template).encode(), response.content)
self.assertNotIn(b"context must be a dict rather than Context", response.content)
self.assertNotIn(b"context must be a dict rather than PluginContext", response.content)
self.assertContains(response, "<p>Hello World Template</p>")
Empty file added tests/utils/__init__.py
Empty file.
1 change: 1 addition & 0 deletions tests/utils/templates/snippet.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>Hello World Template</p>