From cdc9e1f1e3abec93e48b76f13f5cd7f50bb7cf6b Mon Sep 17 00:00:00 2001 From: Angelo Dini Date: Thu, 27 Jun 2019 15:47:58 +0200 Subject: [PATCH 01/13] Add current state --- CHANGELOG.rst | 7 +++++++ djangocms_snippet/cms_plugins.py | 5 +++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index cbdd456b..8840070c 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -3,6 +3,13 @@ Changelog ========= +2.3.0 (unreleased) +================== + +* Fixed an issue where render requires a dict instad of a context +* Added ``DJANGOCMS_SNIPPET_CACHE`` cache settings for snippets + + 2.2.0 (2019-05-06) ================== diff --git a/djangocms_snippet/cms_plugins.py b/djangocms_snippet/cms_plugins.py index 02d62f3b..4de7e454 100644 --- a/djangocms_snippet/cms_plugins.py +++ b/djangocms_snippet/cms_plugins.py @@ -20,6 +20,7 @@ class SnippetPlugin(CMSPluginBase): cache = getattr(settings, 'DJANGOCMS_SNIPPET_CACHE', True) def render(self, context, instance, placeholder): + context=context.flatten() context.update({ 'placeholder': placeholder, 'object': instance, @@ -30,10 +31,10 @@ def render(self, context, instance, placeholder): context.update({ 'html': mark_safe(instance.snippet.html) }) - content = t.render(Context(context)) + content = t.render(context) else: 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} From 9976bc4bb043f269a3cb4e91eb84835e716578f6 Mon Sep 17 00:00:00 2001 From: Angelo Dini Date: Thu, 27 Jun 2019 16:00:55 +0200 Subject: [PATCH 02/13] test fixes and cleanup --- djangocms_snippet/cms_plugins.py | 7 +++++-- tests/test_models.py | 22 ++++++++++++++++++++-- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/djangocms_snippet/cms_plugins.py b/djangocms_snippet/cms_plugins.py index 4de7e454..27a54a4a 100644 --- a/djangocms_snippet/cms_plugins.py +++ b/djangocms_snippet/cms_plugins.py @@ -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 _ @@ -11,13 +11,16 @@ 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=context.flatten() diff --git a/tests/test_models.py b/tests/test_models.py index 3025b245..82045d56 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -1,17 +1,35 @@ # -*- coding: utf-8 -*- from django.test import TestCase +from cms.api import add_plugin, create_page + from djangocms_snippet.models import Snippet class SnippetTestCase(TestCase): def setUp(self): - Snippet.objects.create( - name='snippet', + self.page = create_page( + title='help', + template='page.html', + language='en', ) + def tearDown(self): + self.page.delete() + def test_snippet_instance(self): """Snippet instance has been created""" + Snippet.objects.create( + name='snippet', + ) snippet = Snippet.objects.get(name='snippet') self.assertEqual(snippet.name, 'snippet') + + def test_plugin_rendering(self): + pass + # plugin = add_plugin( + # self.page.placeholders.get(slot="content"), + # "SnippetPlugin", + # "en", + # ) From 96d0228f3176dddacd017e4f6cac840c46ae8ae7 Mon Sep 17 00:00:00 2001 From: Angelo Dini Date: Thu, 27 Jun 2019 16:12:04 +0200 Subject: [PATCH 03/13] context render separation --- djangocms_snippet/cms_plugins.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/djangocms_snippet/cms_plugins.py b/djangocms_snippet/cms_plugins.py index 27a54a4a..22324978 100644 --- a/djangocms_snippet/cms_plugins.py +++ b/djangocms_snippet/cms_plugins.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- from django import template from django.conf import settings +from django.template.context import Context from django.utils.html import escape from django.utils.safestring import mark_safe from django.utils.translation import ugettext_lazy as _ @@ -23,30 +24,30 @@ class SnippetPlugin(CMSPluginBase): cache = CACHE_ENABLED def render(self, context, instance, placeholder): - context=context.flatten() - context.update({ + render_context = Context(context.flatten()) + render_context.update({ 'placeholder': placeholder, 'object': instance, }) try: if instance.snippet.template: t = template.loader.get_template(instance.snippet.template) - context.update({ + render_context.update({ 'html': mark_safe(instance.snippet.html) }) - content = t.render(context) + content = t.render(render_context) else: t = template.Template(instance.snippet.html) - content = t.render(context) + content = t.render(render_context) except template.TemplateDoesNotExist: content = _('Template %(template)s does not exist.') % { 'template': instance.snippet.template} except Exception as e: content = escape(str(e)) - context.update({ + render_context.update({ 'content': mark_safe(content), }) - return context + return render_context plugin_pool.register_plugin(SnippetPlugin) From 0bc1d6d691569a317cf18c029f38f2dea2f51c25 Mon Sep 17 00:00:00 2001 From: Angelo Dini Date: Thu, 27 Jun 2019 17:35:17 +0200 Subject: [PATCH 04/13] might fix issues --- djangocms_snippet/cms_plugins.py | 28 +++++----- tests/settings.py | 4 +- tests/test_models.py | 82 +++++++++++++++++++++++------- tests/utils/__init__.py | 0 tests/utils/templates/snippet.html | 1 + 5 files changed, 83 insertions(+), 32 deletions(-) create mode 100644 tests/utils/__init__.py create mode 100644 tests/utils/templates/snippet.html diff --git a/djangocms_snippet/cms_plugins.py b/djangocms_snippet/cms_plugins.py index 22324978..b1131468 100644 --- a/djangocms_snippet/cms_plugins.py +++ b/djangocms_snippet/cms_plugins.py @@ -24,30 +24,30 @@ class SnippetPlugin(CMSPluginBase): cache = CACHE_ENABLED def render(self, context, instance, placeholder): - render_context = Context(context.flatten()) - render_context.update({ - 'placeholder': placeholder, - 'object': instance, - }) try: if instance.snippet.template: + context = context.flatten() t = template.loader.get_template(instance.snippet.template) - render_context.update({ - 'html': mark_safe(instance.snippet.html) - }) - content = t.render(render_context) + content = t.render(context) else: + # only html provided t = template.Template(instance.snippet.html) - content = t.render(render_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)) - render_context.update({ - 'content': mark_safe(content), + + context.update({ + 'placeholder': placeholder, + 'object': instance, + 'html': mark_safe(instance.snippet.html), + 'content': content, }) - return render_context + + return context plugin_pool.register_plugin(SnippetPlugin) diff --git a/tests/settings.py b/tests/settings.py index 76e8d4ac..f6997859 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -1,7 +1,9 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- HELPER_SETTINGS = { - 'INSTALLED_APPS': [], + 'INSTALLED_APPS': [ + 'tests.utils', + ], 'CMS_LANGUAGES': { 1: [{ 'code': 'en', diff --git a/tests/test_models.py b/tests/test_models.py index 82045d56..fd3ac29f 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -1,18 +1,20 @@ # -*- 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 +from django import template + -class SnippetTestCase(TestCase): +class SnippetTestCase(CMSTestCase): def setUp(self): + self.superuser = self.get_superuser() self.page = create_page( - title='help', - template='page.html', - language='en', + title="help", + template="page.html", + language="en", ) def tearDown(self): @@ -21,15 +23,61 @@ def tearDown(self): def test_snippet_instance(self): """Snippet instance has been created""" Snippet.objects.create( - name='snippet', + 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="

Hello World

", + slug="plugin_snippet", ) - snippet = Snippet.objects.get(name='snippet') - self.assertEqual(snippet.name, 'snippet') - - def test_plugin_rendering(self): - pass - # plugin = add_plugin( - # self.page.placeholders.get(slot="content"), - # "SnippetPlugin", - # "en", - # ) + plugin = add_plugin( + self.page.placeholders.get(slot="content"), + "SnippetPlugin", + "en", + snippet=snippet, + ) + self.assertEqual(plugin.snippet.name, "plugin_snippet") + self.assertEqual(plugin.snippet.html, "

Hello World

") + 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"

Hello World

", 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.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) + + # print(dir(response)) + # print(response.content) + # does not contain Template tests/utils/templates/snippet.html does not exist. + # does not contain context must be a dict rather than Context. + # self.assertContains(response, "

Hello World Template

") + + # print(response.content) + + #self.assertContains(response, "

Hello World

") diff --git a/tests/utils/__init__.py b/tests/utils/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/utils/templates/snippet.html b/tests/utils/templates/snippet.html new file mode 100644 index 00000000..3e63332d --- /dev/null +++ b/tests/utils/templates/snippet.html @@ -0,0 +1 @@ +

Hello World Template

From c84621fc0b020aa8604cff3bfd49efca6c0e41a3 Mon Sep 17 00:00:00 2001 From: Angelo Dini Date: Thu, 27 Jun 2019 17:36:10 +0200 Subject: [PATCH 05/13] cleanup --- tests/test_models.py | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/tests/test_models.py b/tests/test_models.py index fd3ac29f..d9022150 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -71,13 +71,4 @@ def test_file_rendering(self): 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) - - # print(dir(response)) - # print(response.content) - # does not contain Template tests/utils/templates/snippet.html does not exist. - # does not contain context must be a dict rather than Context. - # self.assertContains(response, "

Hello World Template

") - - # print(response.content) - - #self.assertContains(response, "

Hello World

") + self.assertContains(response, "

Hello World Template

") From 2fde424c2bef2e72b04c03c4eb3b14545b26854d Mon Sep 17 00:00:00 2001 From: Angelo Dini Date: Thu, 27 Jun 2019 17:46:09 +0200 Subject: [PATCH 06/13] fixed flake8 --- djangocms_snippet/cms_plugins.py | 1 - tests/test_models.py | 2 -- 2 files changed, 3 deletions(-) diff --git a/djangocms_snippet/cms_plugins.py b/djangocms_snippet/cms_plugins.py index b1131468..5f4052a8 100644 --- a/djangocms_snippet/cms_plugins.py +++ b/djangocms_snippet/cms_plugins.py @@ -1,7 +1,6 @@ # -*- coding: utf-8 -*- from django import template from django.conf import settings -from django.template.context import Context from django.utils.html import escape from django.utils.safestring import mark_safe from django.utils.translation import ugettext_lazy as _ diff --git a/tests/test_models.py b/tests/test_models.py index d9022150..a77f5b7f 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -4,8 +4,6 @@ from djangocms_snippet.models import Snippet -from django import template - class SnippetTestCase(CMSTestCase): From 5b4dcafdd8c27b12ad390a1456fd83f91369891d Mon Sep 17 00:00:00 2001 From: Angelo Dini Date: Thu, 27 Jun 2019 17:47:38 +0200 Subject: [PATCH 07/13] add xenial --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index 9916ff37..710ca6c4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 From 96a3307fc70778b3d60d3c9cd2e1362fe8eb9e88 Mon Sep 17 00:00:00 2001 From: Angelo Dini Date: Thu, 27 Jun 2019 18:00:11 +0200 Subject: [PATCH 08/13] skip cms 34 --- tests/test_models.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/test_models.py b/tests/test_models.py index a77f5b7f..331f5b4b 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -5,6 +5,9 @@ from djangocms_snippet.models import Snippet +CMS_35 = LooseVersion(__version__) >= LooseVersion('3.5') + + class SnippetTestCase(CMSTestCase): def setUp(self): @@ -26,6 +29,7 @@ def test_snippet_instance(self): snippet = Snippet.objects.get(name="snippet") self.assertEqual(snippet.name, "snippet") + @skipUnless(CMS_35, "Test relevant only for CMS>=3.5") def test_html_rendering(self): snippet = Snippet.objects.create( name="plugin_snippet", @@ -47,6 +51,7 @@ def test_html_rendering(self): self.assertIn(b"

Hello World

", response.content) + @skipUnless(CMS_35, "Test relevant only for CMS>=3.5") def test_file_rendering(self): template = "snippet.html" snippet = Snippet.objects.create( From 9294c42d1ef884ef640a71ab494d781dea716132 Mon Sep 17 00:00:00 2001 From: Angelo Dini Date: Thu, 27 Jun 2019 18:01:32 +0200 Subject: [PATCH 09/13] fix typo --- CHANGELOG.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 8840070c..6ee7fe3a 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -6,7 +6,7 @@ Changelog 2.3.0 (unreleased) ================== -* Fixed an issue where render requires a dict instad of a context +* Fixed an issue where render requires a dict instead of a context * Added ``DJANGOCMS_SNIPPET_CACHE`` cache settings for snippets From 303b280ec73651c3a641379f9cdc33c6820d5407 Mon Sep 17 00:00:00 2001 From: Angelo Dini Date: Thu, 27 Jun 2019 18:05:26 +0200 Subject: [PATCH 10/13] fiy tests --- tests/test_models.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test_models.py b/tests/test_models.py index 331f5b4b..0271b57c 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -1,4 +1,7 @@ # -*- coding: utf-8 -*- +from distutils.version import LooseVersion + +from cms import __version__ from cms.api import add_plugin, create_page from cms.test_utils.testcases import CMSTestCase From 8e57bd3d71325408c1d7c9c46dfd17f351465a1b Mon Sep 17 00:00:00 2001 From: Angelo Dini Date: Thu, 27 Jun 2019 18:11:33 +0200 Subject: [PATCH 11/13] fix missing import --- tests/test_models.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_models.py b/tests/test_models.py index 0271b57c..eeff5a41 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- from distutils.version import LooseVersion +from unittest import skipUnless from cms import __version__ from cms.api import add_plugin, create_page From d1e13124f057747b78d42f6193e6ee1b672fab5b Mon Sep 17 00:00:00 2001 From: Angelo Dini Date: Fri, 28 Jun 2019 11:51:58 +0200 Subject: [PATCH 12/13] make tests run for 3.4 --- tests/test_models.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/tests/test_models.py b/tests/test_models.py index eeff5a41..28d43f69 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -1,21 +1,20 @@ # -*- coding: utf-8 -*- -from distutils.version import LooseVersion -from unittest import skipUnless - -from cms import __version__ from cms.api import add_plugin, create_page from cms.test_utils.testcases import CMSTestCase from djangocms_snippet.models import Snippet -CMS_35 = LooseVersion(__version__) >= LooseVersion('3.5') - - class SnippetTestCase(CMSTestCase): def setUp(self): 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", @@ -33,7 +32,6 @@ def test_snippet_instance(self): snippet = Snippet.objects.get(name="snippet") self.assertEqual(snippet.name, "snippet") - @skipUnless(CMS_35, "Test relevant only for CMS>=3.5") def test_html_rendering(self): snippet = Snippet.objects.create( name="plugin_snippet", @@ -46,6 +44,7 @@ def test_html_rendering(self): "en", snippet=snippet, ) + self.page.publish("en") self.assertEqual(plugin.snippet.name, "plugin_snippet") self.assertEqual(plugin.snippet.html, "

Hello World

") self.assertEqual(plugin.snippet.slug, "plugin_snippet") @@ -55,7 +54,6 @@ def test_html_rendering(self): self.assertIn(b"

Hello World

", response.content) - @skipUnless(CMS_35, "Test relevant only for CMS>=3.5") def test_file_rendering(self): template = "snippet.html" snippet = Snippet.objects.create( @@ -69,6 +67,7 @@ def test_file_rendering(self): "en", snippet=snippet, ) + self.page.publish("en") self.assertEqual(plugin.snippet.name, "plugin_snippet") self.assertEqual(plugin.snippet.slug, "plugin_snippet") From 86102bbc556480f2a2d5783a8e9a11de0d8858af Mon Sep 17 00:00:00 2001 From: Angelo Dini Date: Fri, 28 Jun 2019 11:53:23 +0200 Subject: [PATCH 13/13] also cleanup --- tests/test_models.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_models.py b/tests/test_models.py index 28d43f69..81bcaf66 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -23,6 +23,7 @@ def setUp(self): def tearDown(self): self.page.delete() + self.home.delete() def test_snippet_instance(self): """Snippet instance has been created"""