Skip to content

Commit bdbaca3

Browse files
authored
Fixes #48 (#62)
* Add current state * test fixes and cleanup * context render separation * might fix issues * cleanup * fixed flake8 * add xenial * skip cms 34 * fix typo * fiy tests * fix missing import * make tests run for 3.4 * also cleanup
1 parent d9f6f73 commit bdbaca3

File tree

7 files changed

+100
-20
lines changed

7 files changed

+100
-20
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ matrix:
2525
# Django 2.2
2626
- python: 3.6
2727
env: DJANGO='dj22' CMS='cms37'
28+
dist: xenial
29+
sudo: true
2830
- python: 3.7
2931
env: DJANGO='dj22' CMS='cms37'
3032
dist: xenial

CHANGELOG.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ Changelog
33
=========
44

55

6+
2.3.0 (unreleased)
7+
==================
8+
9+
* Fixed an issue where render requires a dict instead of a context
10+
* Added ``DJANGOCMS_SNIPPET_CACHE`` cache settings for snippets
11+
12+
613
2.2.0 (2019-05-06)
714
==================
815

djangocms_snippet/cms_plugins.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
from django import template
3-
from django.template.context import Context
3+
from django.conf import settings
44
from django.utils.html import escape
55
from django.utils.safestring import mark_safe
66
from django.utils.translation import ugettext_lazy as _
@@ -11,37 +11,41 @@
1111
from .models import SnippetPtr
1212

1313

14+
CACHE_ENABLED = getattr(settings, 'DJANGOCMS_SNIPPET_CACHE', False)
15+
16+
1417
class SnippetPlugin(CMSPluginBase):
1518
model = SnippetPtr
1619
name = _('Snippet')
1720
render_template = 'djangocms_snippet/snippet.html'
1821
text_enabled = True
1922
text_editor_preview = False
20-
cache = getattr(settings, 'DJANGOCMS_SNIPPET_CACHE', True)
23+
cache = CACHE_ENABLED
2124

2225
def render(self, context, instance, placeholder):
23-
context.update({
24-
'placeholder': placeholder,
25-
'object': instance,
26-
})
2726
try:
2827
if instance.snippet.template:
28+
context = context.flatten()
2929
t = template.loader.get_template(instance.snippet.template)
30-
context.update({
31-
'html': mark_safe(instance.snippet.html)
32-
})
33-
content = t.render(Context(context))
30+
content = t.render(context)
3431
else:
32+
# only html provided
3533
t = template.Template(instance.snippet.html)
36-
content = t.render(Context(context))
34+
content = t.render(context)
3735
except template.TemplateDoesNotExist:
3836
content = _('Template %(template)s does not exist.') % {
39-
'template': instance.snippet.template}
37+
'template': instance.snippet.template
38+
}
4039
except Exception as e:
4140
content = escape(str(e))
41+
4242
context.update({
43-
'content': mark_safe(content),
43+
'placeholder': placeholder,
44+
'object': instance,
45+
'html': mark_safe(instance.snippet.html),
46+
'content': content,
4447
})
48+
4549
return context
4650

4751

tests/settings.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33
HELPER_SETTINGS = {
4-
'INSTALLED_APPS': [],
4+
'INSTALLED_APPS': [
5+
'tests.utils',
6+
],
57
'CMS_LANGUAGES': {
68
1: [{
79
'code': 'en',

tests/test_models.py

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,81 @@
11
# -*- coding: utf-8 -*-
2-
from django.test import TestCase
2+
from cms.api import add_plugin, create_page
3+
from cms.test_utils.testcases import CMSTestCase
34

45
from djangocms_snippet.models import Snippet
56

67

7-
class SnippetTestCase(TestCase):
8+
class SnippetTestCase(CMSTestCase):
89

910
def setUp(self):
10-
Snippet.objects.create(
11-
name='snippet',
11+
self.superuser = self.get_superuser()
12+
self.home = create_page(
13+
title="home",
14+
template="page.html",
15+
language="en",
16+
)
17+
self.home.publish("en")
18+
self.page = create_page(
19+
title="help",
20+
template="page.html",
21+
language="en",
1222
)
1323

24+
def tearDown(self):
25+
self.page.delete()
26+
self.home.delete()
27+
1428
def test_snippet_instance(self):
1529
"""Snippet instance has been created"""
16-
snippet = Snippet.objects.get(name='snippet')
17-
self.assertEqual(snippet.name, 'snippet')
30+
Snippet.objects.create(
31+
name="snippet",
32+
)
33+
snippet = Snippet.objects.get(name="snippet")
34+
self.assertEqual(snippet.name, "snippet")
35+
36+
def test_html_rendering(self):
37+
snippet = Snippet.objects.create(
38+
name="plugin_snippet",
39+
html="<p>Hello World</p>",
40+
slug="plugin_snippet",
41+
)
42+
plugin = add_plugin(
43+
self.page.placeholders.get(slot="content"),
44+
"SnippetPlugin",
45+
"en",
46+
snippet=snippet,
47+
)
48+
self.page.publish("en")
49+
self.assertEqual(plugin.snippet.name, "plugin_snippet")
50+
self.assertEqual(plugin.snippet.html, "<p>Hello World</p>")
51+
self.assertEqual(plugin.snippet.slug, "plugin_snippet")
52+
53+
with self.login_user_context(self.superuser):
54+
response = self.client.get(self.page.get_absolute_url('en'))
55+
56+
self.assertIn(b"<p>Hello World</p>", response.content)
57+
58+
def test_file_rendering(self):
59+
template = "snippet.html"
60+
snippet = Snippet.objects.create(
61+
name="plugin_snippet",
62+
template=template,
63+
slug="plugin_snippet",
64+
)
65+
plugin = add_plugin(
66+
self.page.placeholders.get(slot="content"),
67+
"SnippetPlugin",
68+
"en",
69+
snippet=snippet,
70+
)
71+
self.page.publish("en")
72+
self.assertEqual(plugin.snippet.name, "plugin_snippet")
73+
self.assertEqual(plugin.snippet.slug, "plugin_snippet")
74+
75+
with self.login_user_context(self.superuser):
76+
response = self.client.get(self.page.get_absolute_url('en'))
77+
78+
self.assertNotIn("Template {} does not exist".format(template).encode(), response.content)
79+
self.assertNotIn(b"context must be a dict rather than Context", response.content)
80+
self.assertNotIn(b"context must be a dict rather than PluginContext", response.content)
81+
self.assertContains(response, "<p>Hello World Template</p>")

tests/utils/__init__.py

Whitespace-only changes.

tests/utils/templates/snippet.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>Hello World Template</p>

0 commit comments

Comments
 (0)