Skip to content

Commit

Permalink
Feat: Move data bridge data to script tags for easier extraction.
Browse files Browse the repository at this point in the history
  • Loading branch information
fsbraun committed Mar 21, 2024
1 parent d123d11 commit cf26a48
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
11 changes: 6 additions & 5 deletions cms/admin/placeholderadmin.py
@@ -1,3 +1,4 @@
import json
import uuid
import warnings
from urllib.parse import parse_qsl, urlparse
Expand Down Expand Up @@ -34,7 +35,7 @@
from cms.models.pluginmodel import CMSPlugin
from cms.plugin_pool import plugin_pool
from cms.signals import post_placeholder_operation, pre_placeholder_operation
from cms.toolbar.utils import get_plugin_tree_as_json
from cms.toolbar.utils import get_plugin_tree
from cms.utils import get_current_site
from cms.utils.compat.warnings import RemovedInDjangoCMS50Warning
from cms.utils.conf import get_cms_setting
Expand Down Expand Up @@ -441,8 +442,8 @@ def copy_plugins(self, request):
source_placeholder,
target_placeholder,
)
data = get_plugin_tree_as_json(request, new_plugins)
return HttpResponse(data, content_type='application/json')
data = get_plugin_tree(request, new_plugins)
return HttpResponse(json.dumps(data), content_type='application/json')

def _copy_plugin_to_clipboard(self, request, target_placeholder):
source_language = request.POST['source_language']
Expand Down Expand Up @@ -724,8 +725,8 @@ def move_plugin(self, request):
if new_plugin and fetch_tree:
root = (new_plugin.parent or new_plugin)
new_plugins = [root] + list(root.get_descendants())
data = get_plugin_tree_as_json(request, new_plugins)
return HttpResponse(data, content_type='application/json')
data = get_plugin_tree(request, new_plugins)
return HttpResponse(json.dumps(data), content_type='application/json')

def _paste_plugin(self, request, plugin, target_language,
target_placeholder, target_position, target_parent=None):
Expand Down
7 changes: 3 additions & 4 deletions cms/plugin_base.py
Expand Up @@ -16,7 +16,7 @@
from cms import operations
from cms.exceptions import SubClassNeededError
from cms.models import CMSPlugin
from cms.toolbar.utils import get_plugin_toolbar_info, get_plugin_tree_as_json
from cms.toolbar.utils import get_plugin_toolbar_info, get_plugin_tree_as_json, get_plugin_tree
from cms.utils.conf import get_cms_setting


Expand Down Expand Up @@ -429,12 +429,11 @@ def render_close_frame(self, request, obj, extra_context=None):
parents=parent_classes,
)
data['plugin_desc'] = escapejs(force_str(obj.get_short_description()))

data['structure'] = get_plugin_tree(request, plugins)
context = {
'plugin': obj,
'is_popup': True,
'plugin_data': json.dumps(data),
'plugin_structure': get_plugin_tree_as_json(request, plugins),
'data_bridge': data,
}

if extra_context:
Expand Down
7 changes: 2 additions & 5 deletions cms/templates/admin/cms/page/close_frame.html
Expand Up @@ -19,11 +19,7 @@
(function(Window) {
// the dataBridge is used to access plugin information from different resources
// Do NOT move this!!!
Window.CMS.API.Helpers.dataBridge = {{ plugin_data|safe }};

{% if plugin_structure %}
Window.CMS.API.Helpers.dataBridge.structure = {{ plugin_structure|safe }};
{% endif %}
Window.CMS.API.Helpers.dataBridge = JSON.parse(document.getElementById('data-bridge').textContent);

Window.CMS.$(document).ready(function () {
// make sure we're doing after the "modal" mechanism kicked in
Expand All @@ -34,4 +30,5 @@
});
})(window.parent || window);
</script>
{{ data_bridge|json_script:"data-bridge" }}
{% endblock %}
6 changes: 5 additions & 1 deletion cms/toolbar/utils.py
Expand Up @@ -64,6 +64,10 @@ def get_plugin_toolbar_js(plugin, children=None, parents=None):


def get_plugin_tree_as_json(request, plugins):
assert False, ("This function is deprecated. Use get_plugin_tree instead.")

Check failure on line 67 in cms/toolbar/utils.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (B011)

cms/toolbar/utils.py:67:12: B011 Do not `assert False` (`python -O` removes these calls), raise `AssertionError()`
return json.dumps(get_plugin_tree(request, plugins))

def get_plugin_tree(request, plugins):
from cms.utils.plugins import downcast_plugins, get_plugin_restrictions

tree_data = []
Expand Down Expand Up @@ -116,7 +120,7 @@ def collect_plugin_data(plugin):
}
tree_structure.append(template.render(context))
tree_data.reverse()
return json.dumps({'html': '\n'.join(tree_structure), 'plugins': tree_data})
return {'html': '\n'.join(tree_structure), 'plugins': tree_data}


def get_toolbar_from_request(request):
Expand Down

0 comments on commit cf26a48

Please sign in to comment.