|
1 | 1 | import json |
2 | 2 | import operator |
3 | 3 | import re |
4 | | -from distutils.version import LooseVersion |
5 | 4 |
|
6 | 5 | from django.contrib.admin.utils import unquote |
7 | 6 | from django.core import signing |
|
19 | 18 | from django.views.decorators.clickjacking import xframe_options_sameorigin |
20 | 19 | from django.views.decorators.http import require_POST |
21 | 20 |
|
22 | | -import cms |
23 | 21 | from cms.models import CMSPlugin |
24 | 22 | from cms.plugin_base import CMSPluginBase |
25 | 23 | from cms.plugin_pool import plugin_pool |
|
30 | 28 | from .forms import ActionTokenValidationForm, DeleteOnCancelForm, RenderPluginForm, TextForm |
31 | 29 | from .models import Text |
32 | 30 | from .utils import ( |
33 | | - OBJ_ADMIN_WITH_CONTENT_RE_PATTERN, _plugin_tags_to_html, plugin_tags_to_admin_html, plugin_tags_to_id_list, |
34 | | - plugin_tags_to_user_html, plugin_to_tag, random_comment_exempt, replace_plugin_tags, |
| 31 | + OBJ_ADMIN_WITH_CONTENT_RE_PATTERN, _plugin_tags_to_html, cms_placeholder_add_plugin, plugin_tags_to_admin_html, |
| 32 | + plugin_tags_to_id_list, plugin_tags_to_user_html, plugin_to_tag, random_comment_exempt, replace_plugin_tags, |
35 | 33 | ) |
36 | 34 | from .widgets import TextEditorWidget |
37 | 35 |
|
38 | 36 |
|
39 | | -CMS_34 = LooseVersion(cms.__version__) >= LooseVersion("3.4") |
40 | | - |
41 | | - |
42 | | -def _user_can_change_placeholder(request, placeholder): |
43 | | - if CMS_34: |
44 | | - return placeholder.has_change_permission(request.user) |
45 | | - return placeholder.has_change_permission(request) |
46 | | - |
47 | | - |
48 | 37 | def post_add_plugin(operation, **kwargs): |
49 | 38 | from djangocms_history.actions import ADD_PLUGIN |
50 | 39 | from djangocms_history.helpers import get_bound_plugins, get_plugin_data |
@@ -187,10 +176,9 @@ class TextPlugin(CMSPluginBase): |
187 | 176 | "pre_change_plugin": pre_change_plugin, |
188 | 177 | } |
189 | 178 |
|
190 | | - if CMS_34: |
191 | | - # On django CMS 3.5 this attribute is set automatically |
192 | | - # when do_post_copy is defined in the plugin class. |
193 | | - _has_do_post_copy = True |
| 179 | + # On django CMS 3.5 this attribute is set automatically |
| 180 | + # when do_post_copy is defined in the plugin class. |
| 181 | + _has_do_post_copy = True |
194 | 182 |
|
195 | 183 | @classmethod |
196 | 184 | def do_post_copy(cls, instance, source_map): |
@@ -251,6 +239,7 @@ def get_editor_widget(self, request, plugins, plugin): |
251 | 239 | pk=plugin.pk, |
252 | 240 | placeholder=plugin.placeholder, |
253 | 241 | plugin_language=plugin.language, |
| 242 | + plugin_position=plugin.position, |
254 | 243 | configuration=self.ckeditor_configuration, |
255 | 244 | render_plugin_url=render_plugin_url, |
256 | 245 | cancel_url=cancel_url, |
@@ -331,6 +320,14 @@ def __init__(self, *args, **kwargs): |
331 | 320 |
|
332 | 321 | return TextPluginForm |
333 | 322 |
|
| 323 | + @staticmethod |
| 324 | + def _create_ghost_plugin(placeholder, plugin): |
| 325 | + """CMS version-save function to add a plugin to a placeholder""" |
| 326 | + if hasattr(placeholder, "add_plugin"): # available as of CMS v4 |
| 327 | + placeholder.add_plugin(plugin) |
| 328 | + else: # CMS < v4 |
| 329 | + plugin.save() |
| 330 | + |
334 | 331 | @xframe_options_sameorigin |
335 | 332 | def add_view(self, request, form_url="", extra_context=None): |
336 | 333 | if "plugin" in request.GET: |
@@ -381,18 +378,19 @@ def add_view(self, request, form_url="", extra_context=None): |
381 | 378 | # Sadly we have to create the CMSPlugin record on add GET request |
382 | 379 | # because we need this record in order to allow the user to add |
383 | 380 | # child plugins to the text (image, link, etc..) |
384 | | - plugin = CMSPlugin.objects.create( |
| 381 | + plugin = CMSPlugin( |
385 | 382 | language=data["plugin_language"], |
386 | 383 | plugin_type=data["plugin_type"], |
387 | | - position=data["position"], |
388 | 384 | placeholder=data["placeholder_id"], |
| 385 | + position=data["position"], |
389 | 386 | parent=data.get("plugin_parent"), |
390 | 387 | ) |
| 388 | + self._create_ghost_plugin(data["placeholder_id"], plugin) |
391 | 389 |
|
392 | 390 | query = request.GET.copy() |
393 | 391 | query["plugin"] = str(plugin.pk) |
394 | 392 |
|
395 | | - success_url = admin_reverse("cms_page_add_plugin") |
| 393 | + success_url = admin_reverse(cms_placeholder_add_plugin) # Version dependent |
396 | 394 | # Because we've created the cmsplugin record |
397 | 395 | # we need to delete the plugin when a user cancels. |
398 | 396 | success_url += "?delete-on-cancel&" + query.urlencode() |
@@ -449,7 +447,7 @@ def render_plugin(self, request): |
449 | 447 |
|
450 | 448 | if not ( |
451 | 449 | plugin_class.has_change_permission(request, obj=text_plugin) |
452 | | - and _user_can_change_placeholder(request, text_plugin.placeholder) # noqa |
| 450 | + and text_plugin.placeholder.has_change_permission(request.user) # noqa |
453 | 451 | ): |
454 | 452 | raise PermissionDenied |
455 | 453 | return HttpResponse(form.render_plugin(request)) |
@@ -486,7 +484,7 @@ def delete_on_cancel(self, request): |
486 | 484 | # and the ckeditor plugin itself. |
487 | 485 | if not ( |
488 | 486 | plugin_class.has_add_permission(request) |
489 | | - and _user_can_change_placeholder(request, text_plugin.placeholder) # noqa |
| 487 | + and text_plugin.placeholder.has_change_permission(request.user) # noqa |
490 | 488 | ): |
491 | 489 | raise PermissionDenied |
492 | 490 | # Token is validated after checking permissions |
|
0 commit comments