Skip to content

Commit cf647d0

Browse files
committed
Include cache_suffix tinymce parameter in project's static files
1 parent eac5cc3 commit cf647d0

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

tests/test_widgets.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,22 @@ def test_tinymce_widget_media(self):
124124
],
125125
)
126126

127+
def test_widget_media_applied_cache_suffix(self):
128+
tinymce_version = "6.8"
129+
130+
orig_config = tinymce.settings.DEFAULT_CONFIG
131+
with patch.dict(tinymce.settings.DEFAULT_CONFIG, {**orig_config, "cache_suffix": f"?ver={tinymce_version}"}):
132+
widget = TinyMCE()
133+
134+
self.assertEqual(list(widget.media.render_css()), [])
135+
self.assertEqual(
136+
widget.media.render_js(),
137+
[
138+
f'<script src="/tinymce/compressor/?ver={tinymce_version}"></script>',
139+
f'<script src="/static/django_tinymce/init_tinymce.js?ver={tinymce_version}"></script>'
140+
]
141+
)
142+
127143
def test_tinymce_widget_required(self):
128144
"""
129145
The TinyMCE widget should never output the required HTML attribute, even
@@ -142,7 +158,7 @@ def test_tinymce_widget_allow_translated_options(self):
142158
orig_config = tinymce.settings.DEFAULT_CONFIG
143159
style_formats = [{"title": gettext_lazy("Awesome style"), "inline": "strong"}]
144160
with patch.dict(
145-
tinymce.settings.DEFAULT_CONFIG, {**orig_config, "style_formats": style_formats}
161+
tinymce.settings.DEFAULT_CONFIG, {**orig_config, "style_formats": style_formats}
146162
):
147163
html = widget.render("foobar", "lorem ipsum", attrs={"id": "id_foobar"})
148164
self.assertIn("Awesome style", html)

tinymce/widgets.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from django.core.serializers.json import DjangoJSONEncoder
1717
from django.forms.utils import flatatt
1818
from django.urls import reverse
19-
from django.utils.html import escape
19+
from django.utils.html import escape, format_html
2020
from django.utils.safestring import mark_safe
2121
from django.utils.translation import get_language, gettext as _, to_locale
2222

@@ -114,6 +114,18 @@ def _media(self):
114114

115115
media = property(_media)
116116

117+
def _render_js(self):
118+
revision_parameter = tinymce.settings.DEFAULT_CONFIG.get('cache_suffix', '')
119+
120+
return [
121+
format_html(
122+
'<script src="{}{}"></script>',
123+
(self.absolute_path(path)), revision_parameter)
124+
for path in self._js
125+
]
126+
127+
forms.widgets.Media.render_js = _render_js
128+
117129

118130
class AdminTinyMCE(TinyMCE, admin_widgets.AdminTextareaWidget):
119131
pass

0 commit comments

Comments
 (0)