def plugins_from_data(placeholder, parent, data): for plugin_type, data, children_data in data: try: plugin_class = plugin_pool.get_plugin(plugin_type) except Exception: continue kwargs = dict(data) inlines = kwargs.pop('inlines', []) shared_glossary = kwargs.pop('shared_glossary', None) try: instance = add_plugin(placeholder, plugin_class, language, target=parent, **kwargs) except Exception: continue if isinstance(instance, CascadeElement): instance.plugin_class.add_inline_elements(instance, inlines) instance.plugin_class.add_shared_reference(instance, shared_glossary) # for some unknown reasons add_plugin sets instance.numchild to 0, # but fixing and save()-ing 'instance' executes some filters in an unwanted manner plugins_from_data(placeholder, instance, children_data) if isinstance(instance, Text): # we must convert the old plugin IDs into the new ones, # otherwise links are not displayed id_dict = dict(zip( plugin_tags_to_id_list(instance.body), (t[0] for t in instance.get_children().values_list('id')) )) instance.body = replace_plugin_tags(instance.body, id_dict) instance.save()
def plugins_from_data(placeholder, parent, data): for plugin_type, data, children_data in data: try: plugin_class = plugin_pool.get_plugin(plugin_type) except Exception: continue kwargs = dict(data) inlines = kwargs.pop('inlines', []) shared_glossary = kwargs.pop('shared_glossary', None) try: instance = add_plugin(placeholder, plugin_class, language, target=parent, **kwargs) except Exception: continue if isinstance(instance, CascadeElement): instance.plugin_class.add_inline_elements(instance, inlines) instance.plugin_class.add_shared_reference( instance, shared_glossary) # for some unknown reasons add_plugin sets instance.numchild to 0, # but fixing and save()-ing 'instance' executes some filters in an unwanted manner plugins_from_data(placeholder, instance, children_data) if isinstance(instance, Text): # we must convert the old plugin IDs into the new ones, # otherwise links are not displayed id_dict = dict( zip(plugin_tags_to_id_list(instance.body), (t[0] for t in instance.get_children().values_list('id')))) instance.body = replace_plugin_tags(instance.body, id_dict) instance.save()
def clean_plugins(self): ids = plugin_tags_to_id_list(self.body) plugins = CMSPlugin.objects.filter(parent=self) for plugin in plugins: if not plugin.pk in ids: plugin.delete( ) #delete plugins that are not referenced in the text anymore
def test_copy_plugin_integrity(self): """ Test that copying of textplugins replaces references to copied plugins """ simple_page = create_page('test page', 'page.html', u'en') simple_placeholder = simple_page.placeholders.get(slot='content') text_plugin = self._add_text_plugin(simple_placeholder) child_plugin_1 = self._add_child_plugin( text_plugin, plugin_type='LinkPlugin', ) text_plugin = self.add_plugin_to_text(text_plugin, child_plugin_1) child_plugin_2 = self._add_child_plugin( text_plugin, plugin_type='LinkPlugin', ) text_plugin = self.add_plugin_to_text(text_plugin, child_plugin_2) # create a page translation to copy plugins to translation = create_title("fr", "test-page-fr", simple_page, slug="test-page-fr") self.assertEqual(CMSPlugin.objects.filter(language='en').count(), 3) self.assertEqual( CMSPlugin.objects.filter(language=translation.language).count(), 0) data = { 'source_placeholder_id': simple_placeholder.pk, 'target_placeholder_id': simple_placeholder.pk, 'target_language': translation.language, 'source_language': 'en', } endpoint = self.get_admin_url(Page, 'copy_plugins') endpoint += '?' + urlencode({'cms_path': '/en/'}) with self.login_user_context(self.user): response = self.client.post(endpoint, data) self.assertEqual(response.status_code, 200) self.assertEqual( response.content.decode('utf8').count('"position":'), 3) self.assertEqual( CMSPlugin.objects.filter(language='en').count(), 3) self.assertEqual( CMSPlugin.objects.filter( language=translation.language).count(), 3) plugins = list(CMSPlugin.objects.all()) new_plugin = plugins[3].get_plugin_instance()[0] idlist = sorted(plugin_tags_to_id_list(new_plugin.body)) expected = sorted([plugins[4].pk, plugins[5].pk]) self.assertEqual(idlist, expected)
def test_plugin_tags_to_id_list(self): pairs = ( ('<cms-plugin id="1"></cms-plugin><cms-plugin id="2"></cms-plugin>', [1, 2]), ('<cms-plugin alt="<h1>markup</h1>" id="1"></cms-plugin><cms-plugin id="1"></cms-plugin>', [1, 1]), ) for markup, expected in pairs: self.assertEqual(plugin_tags_to_id_list(markup), expected)
def test_copy_plugin_integrity(self): """ Test that copying of textplugins replaces references to copied plugins """ simple_page = create_page('test page', 'page.html', u'en') simple_placeholder = simple_page.placeholders.get(slot='content') text_plugin = self._add_text_plugin(simple_placeholder) child_plugin_1 = self._add_child_plugin( text_plugin, plugin_type='LinkPlugin', ) text_plugin = self.add_plugin_to_text(text_plugin, child_plugin_1) child_plugin_2 = self._add_child_plugin( text_plugin, plugin_type='LinkPlugin', ) text_plugin = self.add_plugin_to_text(text_plugin, child_plugin_2) # create a page translation to copy plugins to translation = create_title( "fr", "test-page-fr", simple_page, slug="test-page-fr" ) self.assertEqual(CMSPlugin.objects.filter(language='en').count(), 3) self.assertEqual(CMSPlugin.objects.filter(language=translation.language).count(), 0) data = { 'source_placeholder_id': simple_placeholder.pk, 'target_placeholder_id': simple_placeholder.pk, 'target_language': translation.language, 'source_language': 'en', } endpoint = self.get_admin_url(Page, 'copy_plugins') with self.login_user_context(self.user): response = self.client.post(endpoint, data) self.assertEqual(response.status_code, 200) self.assertEqual(response.content.decode('utf8').count('"position":'), 3) self.assertEqual(CMSPlugin.objects.filter(language='en').count(), 3) self.assertEqual(CMSPlugin.objects.filter(language=translation.language).count(), 3) plugins = list(CMSPlugin.objects.all()) new_plugin = plugins[3].get_plugin_instance()[0] idlist = sorted(plugin_tags_to_id_list(new_plugin.body)) expected = sorted([plugins[4].pk, plugins[5].pk]) self.assertEqual(idlist, expected)
def test_copy_plugin_callback(self): simple_page = create_page('test page', 'page.html', u'en') simple_placeholder = get_page_placeholders(simple_page, 'en').get(slot='content') text_plugin_1 = self._add_text_plugin(simple_placeholder) child_plugin_1_a = self._add_child_plugin( text_plugin_1, plugin_type='LinkPlugin', ) text_plugin_1 = self.add_plugin_to_text(text_plugin_1, child_plugin_1_a) child_plugin_1_b = self._add_child_plugin( text_plugin_1, plugin_type='LinkPlugin', ) text_plugin_1 = self.add_plugin_to_text(text_plugin_1, child_plugin_1_b) text_plugin_2 = copy.copy(text_plugin_1) text_plugin_2.pk = None text_plugin_2.save() child_plugin_2_a = self._add_child_plugin( text_plugin_2, plugin_type='LinkPlugin', ) child_plugin_2_b = self._add_child_plugin( text_plugin_2, plugin_type='LinkPlugin', ) source_map = { child_plugin_1_a.pk: child_plugin_2_a, child_plugin_1_b.pk: child_plugin_2_b, } TextPlugin.do_post_copy(text_plugin_2, source_map) text_plugin_2.refresh_from_db() idlist = sorted(plugin_tags_to_id_list(text_plugin_2.body)) expected = sorted([child_plugin_2_a.pk, child_plugin_2_b.pk]) self.assertEqual(idlist, expected)
def test_copy_plugin_callback(self): simple_page = create_page('test page', 'page.html', u'en') simple_placeholder = simple_page.placeholders.get(slot='content') text_plugin_1 = self._add_text_plugin(simple_placeholder) child_plugin_1_a = self._add_child_plugin( text_plugin_1, plugin_type='LinkPlugin', ) text_plugin_1 = self.add_plugin_to_text(text_plugin_1, child_plugin_1_a) child_plugin_1_b = self._add_child_plugin( text_plugin_1, plugin_type='LinkPlugin', ) text_plugin_1 = self.add_plugin_to_text(text_plugin_1, child_plugin_1_b) text_plugin_2 = copy.copy(text_plugin_1) text_plugin_2.pk = None text_plugin_2.save() child_plugin_2_a = self._add_child_plugin( text_plugin_2, plugin_type='LinkPlugin', ) child_plugin_2_b = self._add_child_plugin( text_plugin_2, plugin_type='LinkPlugin', ) source_map = { child_plugin_1_a.pk: child_plugin_2_a, child_plugin_1_b.pk: child_plugin_2_b, } TextPlugin.do_post_copy(text_plugin_2, source_map) text_plugin_2.refresh_from_db() idlist = sorted(plugin_tags_to_id_list(text_plugin_2.body)) expected = sorted([child_plugin_2_a.pk, child_plugin_2_b.pk]) self.assertEqual(idlist, expected)
def clean_plugins(self): ids = plugin_tags_to_id_list(self.body) plugins = CMSPlugin.objects.filter(parent=self) for plugin in plugins: if not plugin.pk in ids: plugin.delete() #delete plugins that are not referenced in the text anymore
def _get_common_children_ids(text_plugin_one, text_plugin_two): original_children_ids = set( plugin_tags_to_id_list(text_plugin_one.body)) copied_children_ids = set( plugin_tags_to_id_list(text_plugin_two.body)) return original_children_ids.intersection(copied_children_ids)