Skip to content
This repository was archived by the owner on Mar 26, 2025. It is now read-only.

Commit d08a6e4

Browse files
authored
504 copy paste plugins inside ckeditor refer to same instance (#572)
* Ran isort on models.py * Fixed copying referenced plugins removing other child plugin references * Added test case for copy_referenced_plugins method
1 parent 66a95eb commit d08a6e4

File tree

2 files changed

+76
-3
lines changed

2 files changed

+76
-3
lines changed

djangocms_text_ckeditor/models.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,20 +85,25 @@ def clean_plugins(self):
8585
def copy_referenced_plugins(self):
8686
referenced_plugins = self.get_referenced_plugins()
8787
if referenced_plugins:
88-
plugins_ziplist = copy_plugins_to(
88+
plugins_pairs = list(copy_plugins_to(
8989
referenced_plugins,
9090
self.placeholder,
9191
to_language=self.language,
9292
parent_plugin_id=self.id
93-
)
94-
self.post_copy(self, plugins_ziplist)
93+
))
94+
self.add_existing_child_plugins_to_pairs(plugins_pairs)
95+
self.post_copy(self, plugins_pairs)
9596

9697
def get_referenced_plugins(self):
9798
ids_in_body = set(plugin_tags_to_id_list(self.body))
9899
child_plugins_ids = set(self.cmsplugin_set.all().values_list('id', flat=True))
99100
referenced_plugins_ids = ids_in_body - child_plugins_ids
100101
return CMSPlugin.objects.filter(id__in=referenced_plugins_ids)
101102

103+
def add_existing_child_plugins_to_pairs(self, plugins_pairs):
104+
for plugin in self.cmsplugin_set.all():
105+
plugins_pairs.append((plugin, plugin))
106+
102107
def _get_inline_plugin_ids(self):
103108
return plugin_tags_to_id_list(self.body)
104109

tests/test_plugin.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,74 @@ def test_add_and_cancel_plugin(self):
223223
response = text_plugin_class.delete_on_cancel(request)
224224
self.assertEqual(response.status_code, 400)
225225

226+
def test_copy_referenced_plugins(self):
227+
"""
228+
Test that copy+pasting a child plugin between text editors
229+
creates proper copies of the child plugin and messes no other data up
230+
"""
231+
simple_page = create_page('test page', 'page.html', u'en')
232+
simple_placeholder = get_page_placeholders(simple_page, 'en').get(slot='content')
233+
234+
def _get_text_plugin_with_children():
235+
text_plugin = add_plugin(
236+
simple_placeholder,
237+
'TextPlugin',
238+
'en',
239+
body='Text plugin we copy child plugins to'
240+
)
241+
_add_child_plugins_to_text_plugin(text_plugin)
242+
return text_plugin
243+
244+
def _add_child_plugins_to_text_plugin(text_plugin):
245+
child_plugin_1 = add_plugin(
246+
simple_placeholder,
247+
'PicturePlugin',
248+
'en',
249+
target=text_plugin,
250+
picture=self.create_filer_image_object(),
251+
caption_text='Child plugin one',
252+
)
253+
child_plugin_2 = add_plugin(
254+
simple_placeholder,
255+
'PicturePlugin',
256+
'en',
257+
target=text_plugin,
258+
picture=self.create_filer_image_object(),
259+
caption_text='Child plugin two',
260+
)
261+
self.add_plugin_to_text(text_plugin, child_plugin_1)
262+
self.add_plugin_to_text(text_plugin, child_plugin_2)
263+
264+
def _copy_child_plugins_from_text(text_plugin_source, text_plugin_destination):
265+
for child_plugin in text_plugin_source.cmsplugin_set.all():
266+
text_plugin_destination.body += ' ' + plugin_to_tag(child_plugin)
267+
text_plugin_destination.save()
268+
_run_clean_and_copy(text_plugin_destination)
269+
270+
def _run_clean_and_copy(text_plugin):
271+
text_plugin.clean_plugins()
272+
text_plugin.copy_referenced_plugins()
273+
274+
def _get_common_children_ids(text_plugin_one, text_plugin_two):
275+
original_children_ids = set(plugin_tags_to_id_list(text_plugin_one.body))
276+
copied_children_ids = set(plugin_tags_to_id_list(text_plugin_two.body))
277+
return original_children_ids.intersection(copied_children_ids)
278+
279+
text_plugin_copy_from = _get_text_plugin_with_children()
280+
text_plugin_copy_to = _get_text_plugin_with_children()
281+
282+
_copy_child_plugins_from_text(text_plugin_copy_from, text_plugin_copy_to)
283+
self.assertEqual(text_plugin_copy_from.cmsplugin_set.count(), 2)
284+
self.assertEqual(text_plugin_copy_to.cmsplugin_set.count(), 4)
285+
286+
_run_clean_and_copy(text_plugin_copy_from)
287+
_run_clean_and_copy(text_plugin_copy_to)
288+
self.assertEqual(text_plugin_copy_from.cmsplugin_set.count(), 2)
289+
self.assertEqual(text_plugin_copy_to.cmsplugin_set.count(), 4)
290+
291+
common_children_ids = _get_common_children_ids(text_plugin_copy_from, text_plugin_copy_to)
292+
self.assertFalse(common_children_ids)
293+
226294
def test_add_and_cancel_child_plugin(self):
227295
"""
228296
Test that you can add a text plugin

0 commit comments

Comments
 (0)