def clean_plugins(self): ids = plugin_tags_to_id_list(self.body) plugins = CMSPlugin.objects.filter(parent=self) for plugin in plugins: if not str(plugin.pk) in ids: plugin.delete( ) #delete plugins that are not referenced in the text anymore
def plugin_iterator_from_text_plugin(text_plugin): plugin_pk_list = plugin_tags_to_id_list(text_plugin.body) for pk in plugin_pk_list: try: yield CMSPlugin.objects.get(pk=pk) except CMSPlugin.DoesNotExist, e: if CMSPLUGIN_FOOTNOTE_DEBUG: raise e
def get_footnotes_for_page(request, page): """ Gets the Footnote instances for `page`, with the correct order. """ plugins = get_cmsplugin_queryset(request) footnote_and_text_plugins = plugins.filter( placeholder__page=page, plugin_type__in=('FootnotePlugin', 'TextPlugin'), language=translation.get_language(), ).order_by('position').values('parent', 'plugin_type', 'pk') pks = [p['pk'] for p in footnote_and_text_plugins] footnote_dict = Footnote.objects.in_bulk(pks) text_dict = Text.objects.in_bulk(pks) def get_footnote_or_text(plugin_pk, plugin_type): d = footnote_dict if plugin_type == 'FootnotePlugin' else text_dict try: return d[plugin_pk] except KeyError: if CMSPLUGIN_FOOTNOTE_DEBUG: raise root_footnote_and_text_plugins = [ p for p in footnote_and_text_plugins if p['parent'] is None ] footnotes = [] for plugin in root_footnote_and_text_plugins: footnote_or_text = get_footnote_or_text(plugin['pk'], plugin['plugin_type']) if footnote_or_text is None: continue if plugin['plugin_type'] == 'FootnotePlugin': footnotes.append(footnote_or_text) else: for pk in plugin_tags_to_id_list(footnote_or_text.body): footnote = get_footnote_or_text(pk, 'FootnotePlugin') if footnote is not None: footnotes.append(footnote) return footnotes
def flattenAnchors(plugins): # Generates a list of anchors from a list of plugins # Make sure they all start on the same level # i.e. Kill all plugins in placeholder without position from cms.models import CMSPlugin from cms.plugins.text import utils from cms.plugins.text.cms_plugins import TextPlugin for plugin in plugins: if plugin.get_plugin_class() is AnchorPlugin: if plugin.anchor.in_contents: yield plugin.anchor elif plugin.get_descendant_count() != 0: # Children won't necessarily be in the correct order so we check # for TextPlugin as we know how to get correct order from this if plugin.get_plugin_class() is TextPlugin: children = [CMSPlugin.objects.get(pk=key) for key in utils.plugin_tags_to_id_list(plugin.text.body)] else: children = plugin.get_children() for anchor in flattenAnchors(children): # We already know these are valid :) yield anchor
def get_footnotes_for_page(request, page): """ Gets the Footnote instances for `page`, with the correct order. """ plugins = get_cmsplugin_queryset(request) footnote_and_text_plugins = plugins.filter( placeholder__page=page, plugin_type__in=('FootnotePlugin', 'TextPlugin'), language=translation.get_language(), ).order_by('position').values('parent', 'plugin_type', 'pk') pks = [p['pk'] for p in footnote_and_text_plugins] footnote_dict = Footnote.objects.in_bulk(pks) text_dict = Text.objects.in_bulk(pks) def get_footnote_or_text(plugin_pk, plugin_type): d = footnote_dict if plugin_type == 'FootnotePlugin' else text_dict try: return d[plugin_pk] except KeyError: if CMSPLUGIN_FOOTNOTE_DEBUG: raise root_footnote_and_text_plugins = [p for p in footnote_and_text_plugins if p['parent'] is None] footnotes = [] for plugin in root_footnote_and_text_plugins: footnote_or_text = get_footnote_or_text(plugin['pk'], plugin['plugin_type']) if footnote_or_text is None: continue if plugin['plugin_type'] == 'FootnotePlugin': footnotes.append(footnote_or_text) else: for pk in plugin_tags_to_id_list(footnote_or_text.body): footnote = get_footnote_or_text(pk, 'FootnotePlugin') if footnote is not None: footnotes.append(footnote) return footnotes
def test_11_copy_textplugin(self): """ Test that copying of textplugins replaces references to copied plugins """ page = self.create_page() placeholder = page.placeholders.get(slot='body') plugin_base = CMSPlugin( plugin_type='TextPlugin', placeholder=placeholder, position=1, language=self.FIRST_LANG) plugin_base.insert_at(None, position='last-child', commit=False) plugin = Text(body='') plugin_base.set_base_attr(plugin) plugin.save() plugin_ref_1_base = CMSPlugin( plugin_type='TextPlugin', placeholder=placeholder, position=1, language=self.FIRST_LANG) plugin_ref_1_base.insert_at(plugin_base, position='last-child', commit=False) plugin_ref_1 = Text(body='') plugin_ref_1_base.set_base_attr(plugin_ref_1) plugin_ref_1.save() plugin_ref_2_base = CMSPlugin( plugin_type='TextPlugin', placeholder=placeholder, position=2, language=self.FIRST_LANG) plugin_ref_2_base.insert_at(plugin_base, position='last-child', commit=False) plugin_ref_2 = Text(body='') plugin_ref_2_base.set_base_attr(plugin_ref_2) plugin_ref_2.save() plugin.body = plugin_tags_to_admin_html(' {{ plugin_object %s }} {{ plugin_object %s }} ' % (str(plugin_ref_1.pk), str(plugin_ref_2.pk))) plugin.save() self.assertEquals(plugin.pk, 1) page_data = self.get_new_page_data() #create 2nd language page page_data.update({ 'language': self.SECOND_LANG, 'title': "%s %s" % (page.get_title(), self.SECOND_LANG), }) response = self.client.post(URL_CMS_PAGE_CHANGE % page.pk + "?language=%s" % self.SECOND_LANG, page_data) self.assertRedirects(response, URL_CMS_PAGE) self.assertEquals(CMSPlugin.objects.filter(language=self.FIRST_LANG).count(), 3) self.assertEquals(CMSPlugin.objects.filter(language=self.SECOND_LANG).count(), 0) self.assertEquals(CMSPlugin.objects.count(), 3) self.assertEquals(Page.objects.all().count(), 1) copy_data = { 'placeholder': placeholder.pk, 'language': self.SECOND_LANG, 'copy_from': self.FIRST_LANG, } response = self.client.post(URL_CMS_PAGE + "copy-plugins/", copy_data) self.assertEquals(response.status_code, 200) self.assertEqual(response.content.count('<li '), 3) # assert copy success self.assertEquals(CMSPlugin.objects.filter(language=self.FIRST_LANG).count(), 3) self.assertEquals(CMSPlugin.objects.filter(language=self.SECOND_LANG).count(), 3) self.assertEquals(CMSPlugin.objects.count(), 6) new_plugin = Text.objects.get(pk=6) self.assertEquals(plugin_tags_to_id_list(new_plugin.body), [u'4', u'5'])
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