From da81a43e5396d1c43e3af9fa96d80fe0f9a43c86 Mon Sep 17 00:00:00 2001 From: ptzan Date: Wed, 17 Apr 2024 14:50:47 +0200 Subject: [PATCH] Include cache_suffix tinymce parameter in project's static files --- tests/test_widgets.py | 18 +++++++++++++++++- tinymce/widgets.py | 14 +++++++++++++- tox.ini | 1 + 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/tests/test_widgets.py b/tests/test_widgets.py index 9cb68843..99b0f92a 100644 --- a/tests/test_widgets.py +++ b/tests/test_widgets.py @@ -124,6 +124,22 @@ def test_tinymce_widget_media(self): ], ) + def test_widget_media_applied_cache_suffix(self): + tinymce_version = "6.8" + + orig_config = tinymce.settings.DEFAULT_CONFIG + with patch.dict(tinymce.settings.DEFAULT_CONFIG, {**orig_config, "cache_suffix": f"?ver={tinymce_version}"}): + widget = TinyMCE() + + self.assertEqual(list(widget.media.render_css()), []) + self.assertEqual( + widget.media.render_js(), + [ + f'', + f'' + ] + ) + def test_tinymce_widget_required(self): """ The TinyMCE widget should never output the required HTML attribute, even @@ -142,7 +158,7 @@ def test_tinymce_widget_allow_translated_options(self): orig_config = tinymce.settings.DEFAULT_CONFIG style_formats = [{"title": gettext_lazy("Awesome style"), "inline": "strong"}] with patch.dict( - tinymce.settings.DEFAULT_CONFIG, {**orig_config, "style_formats": style_formats} + tinymce.settings.DEFAULT_CONFIG, {**orig_config, "style_formats": style_formats} ): html = widget.render("foobar", "lorem ipsum", attrs={"id": "id_foobar"}) self.assertIn("Awesome style", html) diff --git a/tinymce/widgets.py b/tinymce/widgets.py index 65f91e0d..b7066f34 100644 --- a/tinymce/widgets.py +++ b/tinymce/widgets.py @@ -16,7 +16,7 @@ from django.core.serializers.json import DjangoJSONEncoder from django.forms.utils import flatatt from django.urls import reverse -from django.utils.html import escape +from django.utils.html import escape, format_html from django.utils.safestring import mark_safe from django.utils.translation import get_language, gettext as _, to_locale @@ -114,6 +114,18 @@ def _media(self): media = property(_media) + def _render_js(self): + revision_parameter = tinymce.settings.DEFAULT_CONFIG.get('cache_suffix', '') + + return [ + format_html( + '', + (self.absolute_path(path)), revision_parameter) + for path in self._js + ] + + forms.widgets.Media.render_js = _render_js + class AdminTinyMCE(TinyMCE, admin_widgets.AdminTextareaWidget): pass diff --git a/tox.ini b/tox.ini index ff3eba08..f8e10a06 100644 --- a/tox.ini +++ b/tox.ini @@ -12,6 +12,7 @@ deps = dj50: Django>=5.0,<5.1 djmain: https://github.com/django/django/archive/main.tar.gz coverage[toml] + pdbpp usedevelop = True ignore_outcome = djmain: True