def render_placeholder_toolbar(placeholder, context, content, name_fallback=None): from cms.plugin_pool import plugin_pool request = context['request'] page = get_page_from_placeholder_if_exists(placeholder) if not page: page = getattr(request, 'current_page', None) if page: template = page.template if name_fallback and not placeholder: placeholder = Placeholder.objects.create(slot=name_fallback) page.placeholders.add(placeholder) else: template = None if placeholder: slot = placeholder.slot else: slot = None installed_plugins = plugin_pool.get_all_plugins(slot, page) name = get_placeholder_conf(slot, template, "name", title(slot)) name = _(name) context.push() context.update({ 'installed_plugins': installed_plugins, 'language': get_language_from_request(request), 'placeholder_label': name, 'placeholder': placeholder, 'page': page, }) toolbar = render_to_string("cms/toolbar/placeholder.html", context) context.pop() return "".join([toolbar, content])
def content_finaliser(instance, placeholder, rendered_content, original_context): ''' This plugin processor displays the tag legend on the allowed plugins specified below in the admin interface. ''' # This plugin processor only acts in the admin site .. if not re.match(in_admin_regex, original_context['request'].path): return rendered_content taggable_plugins = ['TemplateTextPlugin', 'TemplateSnippetPlugin'] # .. and only on certain plugins. if not instance.plugin_type in taggable_plugins: return rendered_content # OK, so we're in the admin mode of a templatable plugin - so we wrap the plugin # interface with a handy tag legend. tag_list_template = 'cms/admin/tag_list_box.html' context_dictionary = { 'content': rendered_content, 'tagList': get_tag_list(get_page_from_placeholder_if_exists(instance.placeholder)) } return render_to_string(tag_list_template, context_dictionary)
def render_placeholder_toolbar(placeholder, context, content, name_fallback=None): from cms.plugin_pool import plugin_pool request = context['request'] page = get_page_from_placeholder_if_exists(placeholder) if not page: page = getattr(request, 'current_page', None) if page: template = page.template if name_fallback and not placeholder: placeholder = Placeholder.objects.create(slot=name_fallback) page.placeholders.add(placeholder) else: template = None if placeholder: slot = placeholder.slot else: slot = None installed_plugins = plugin_pool.get_all_plugins(slot, page) name = get_placeholder_conf(slot, template, "name", title(slot)) name = _(name) context.push() context.update({ 'installed_plugins': installed_plugins, 'language': get_language_from_request(request), 'placeholder_label': name, 'placeholder': placeholder, 'page': page, }) toolbar = render_to_string("cms/toolbar/placeholder.html", context) context.pop() return "".join([toolbar, content])
def render(self, context, instance, placeholder): context.update({ 'placeholder':placeholder, 'object':instance, 'original_page':get_page_from_placeholder_if_exists(instance.anchor.placeholder), }) return context
def render_placeholder_toolbar(placeholder, context, content): from cms.plugin_pool import plugin_pool request = context['request'] page = get_page_from_placeholder_if_exists(placeholder) if page: template = page.template else: template = None installed_plugins = plugin_pool.get_all_plugins(placeholder.slot, page) name = settings.CMS_PLACEHOLDER_CONF.get( "%s %s" % (template, placeholder.slot), {}).get("name", None) if not name: name = settings.CMS_PLACEHOLDER_CONF.get(placeholder.slot, {}).get("name", None) if not name: name = placeholder.slot name = title(name) toolbar = render_to_string( "cms/toolbar/add_plugins.html", { 'installed_plugins': installed_plugins, 'language': get_language_from_request(request), 'placeholder_label': name, 'placeholder': placeholder, 'page': page, }) return "".join([toolbar, content])
def content_finaliser(instance, placeholder, rendered_content, original_context): ''' This plugin processor displays the tag legend on the allowed plugins specified below in the admin interface. ''' # This plugin processor only acts in the admin site .. if not re.match(in_admin_regex, original_context['request'].path): return rendered_content taggable_plugins = ['TemplateTextPlugin', 'TemplateSnippetPlugin'] # .. and only on certain plugins. if not instance.plugin_type in taggable_plugins: return rendered_content # OK, so we're in the admin mode of a templatable plugin - so we wrap the plugin # interface with a handy tag legend. tag_list_template = 'cms/admin/tag_list_box.html' context_dictionary = { 'content': rendered_content, 'tagList': get_tag_list(get_page_from_placeholder_if_exists(instance.placeholder)) } return render_to_string(tag_list_template, context_dictionary)
def render_placeholder_toolbar(placeholder, context, content, name_fallback=None): from cms.plugin_pool import plugin_pool request = context['request'] page = get_page_from_placeholder_if_exists(placeholder) if not page: page = getattr(request, 'current_page', None) if page: template = page.template if name_fallback and not placeholder: placeholder = Placeholder.objects.create(slot=name_fallback) page.placeholders.add(placeholder) else: template = None if placeholder: slot = placeholder.slot else: slot = None installed_plugins = plugin_pool.get_all_plugins(slot, page) mixed_key = "%s %s" % (template, slot) name = settings.CMS_PLACEHOLDER_CONF.get(mixed_key, {}).get("name", None) if not name: name = settings.CMS_PLACEHOLDER_CONF.get(slot, {}).get("name", None) if not name: name = slot if not name: name = name_fallback name = title(name) toolbar = render_to_string("cms/toolbar/add_plugins.html", { 'installed_plugins': installed_plugins, 'language': get_language_from_request(request), 'placeholder_label': name, 'placeholder': placeholder, 'page': page, }) return "".join([toolbar, content])
def render_placeholder_toolbar(placeholder, context, content, name_fallback=None): from cms.plugin_pool import plugin_pool request = context["request"] page = get_page_from_placeholder_if_exists(placeholder) if not page: page = getattr(request, "current_page", None) if page: template = page.template if name_fallback and not placeholder: placeholder = Placeholder.objects.create(slot=name_fallback) page.placeholders.add(placeholder) else: template = None if placeholder: slot = placeholder.slot else: slot = None installed_plugins = plugin_pool.get_all_plugins(slot, page) name = get_placeholder_conf(slot, template, "name", title(slot)) name = _(name) toolbar = render_to_string( "cms/toolbar/placeholder.html", { "installed_plugins": installed_plugins, "language": get_language_from_request(request), "placeholder_label": name, "placeholder": placeholder, "page": page, }, ) return "".join([toolbar, content])
def page(self): warnings.warn( "Don't use the page attribute on CMSPlugins! CMSPlugins are not " "guaranteed to have a page associated with them!", DontUsePageAttributeWarning, ) return get_page_from_placeholder_if_exists(self.placeholder)
def has_change_permission(self, request): page = get_page_from_placeholder_if_exists(self.placeholder) if page: return page.has_change_permission(request) elif self.placeholder: return self.placeholder.has_change_permission(request) else: return self.parent.has_change_permission(request)
def render_placeholder(placeholder, context_to_copy, name_fallback="Placeholder"): """ Renders plugins for a placeholder on the given page using shallow copies of the given context, and returns a string containing the rendered output. """ from cms.plugins.utils import get_plugins context = context_to_copy context.push() request = context["request"] plugins = [plugin for plugin in get_plugins(request, placeholder)] page = get_page_from_placeholder_if_exists(placeholder) if page: template = page.template else: template = None fallback = getattr(django_settings, "CMS_PLACEHOLDER_LANG_FALLBACK", None) if not plugins and fallback: plugins = [plugin for plugin in get_plugins(request, placeholder, lang=fallback)] # Add extra context as defined in settings, but do not overwrite existing context variables, # since settings are general and database/template are specific # TODO this should actually happen as a plugin context processor, but these currently overwrite # existing context -- maybe change this order? slot = getattr(placeholder, "slot", None) extra_context = {} if slot: extra_context = settings.CMS_PLACEHOLDER_CONF.get("%s %s" % (template, slot), {}).get("extra_context", None) if not extra_context: extra_context = settings.CMS_PLACEHOLDER_CONF.get(slot, {}).get("extra_context", {}) for key, value in extra_context.items(): if not key in context: context[key] = value c = [] # Prepend frontedit toolbar output if applicable edit = False if ( ("edit" in request.GET or request.session.get("cms_edit", False)) and "cms.middleware.toolbar.ToolbarMiddleware" in django_settings.MIDDLEWARE_CLASSES and request.user.is_staff and request.user.is_authenticated() and (not page or page.has_change_permission(request)) ): edit = True if edit: from cms.middleware.toolbar import toolbar_plugin_processor processors = (toolbar_plugin_processor,) else: processors = None c.extend(render_plugins(plugins, context, placeholder, processors)) content = "".join(c) if edit: content = render_placeholder_toolbar(placeholder, context, content, name_fallback) context.pop() return content
def has_change_permission(self, request): page = get_page_from_placeholder_if_exists(self.placeholder) if page: return page.has_change_permission(request) elif self.placeholder: return self.placeholder.has_change_permission(request) elif self.parent: return self.parent.has_change_permission(request) return False
def render_placeholder(placeholder, context_to_copy, name_fallback="Placeholder"): """ Renders plugins for a placeholder on the given page using shallow copies of the given context, and returns a string containing the rendered output. """ from cms.plugins.utils import get_plugins context = context_to_copy context.push() request = context['request'] lang = get_language_from_request(request) plugins = [plugin for plugin in get_plugins(request, placeholder)] if not plugins and settings.CMS_LANGUAGE_FALLBACK and lang != get_default_language(): fallbacks = get_fallback_languages(lang) for l in fallbacks: plugins = [plugin for plugin in get_plugins(request, placeholder, l)] if plugins: break page = get_page_from_placeholder_if_exists(placeholder) if page: template = page.template else: template = None # Add extra context as defined in settings, but do not overwrite existing context variables, # since settings are general and database/template are specific # TODO this should actually happen as a plugin context processor, but these currently overwrite # existing context -- maybe change this order? slot = getattr(placeholder, 'slot', None) extra_context = {} if slot: extra_context = get_placeholder_conf("extra_context", slot, template, {}) for key, value in extra_context.items(): if not key in context: context[key] = value c = [] # Prepend frontedit toolbar output if applicable edit = False toolbar = getattr(request, 'toolbar', None) if (getattr(toolbar, 'edit_mode', False) and (not page or page.has_change_permission(request))): edit = True if edit: from cms.middleware.toolbar import toolbar_plugin_processor processors = (toolbar_plugin_processor,) else: processors = None c.extend(render_plugins(plugins, context, placeholder, processors)) content = "".join(c) if edit: content = render_placeholder_toolbar(placeholder, context, content, name_fallback) context.pop() return content
def render_placeholder(placeholder, context_to_copy, name_fallback="Placeholder"): """ Renders plugins for a placeholder on the given page using shallow copies of the given context, and returns a string containing the rendered output. """ from cms.plugins.utils import get_plugins context = context_to_copy context.push() request = context['request'] lang = get_language_from_request(request) page = get_page_from_placeholder_if_exists(placeholder) if page: template = page.template else: template = None plugins = [plugin for plugin in get_plugins(request, placeholder, lang)] if (len(plugins)==0 and placeholder and lang != get_default_language() and get_placeholder_conf("language_fallback", placeholder.slot, template, False)): fallbacks = get_fallback_languages(lang) for l in fallbacks: plugins = [plugin for plugin in get_plugins(request, placeholder, l)] if plugins: break # Add extra context as defined in settings, but do not overwrite existing context variables, # since settings are general and database/template are specific # TODO this should actually happen as a plugin context processor, but these currently overwrite # existing context -- maybe change this order? slot = getattr(placeholder, 'slot', None) extra_context = {} if slot: extra_context = get_placeholder_conf("extra_context", slot, template, {}) for key, value in extra_context.items(): if not key in context: context[key] = value content = [] # Prepend frontedit toolbar output if applicable edit = False toolbar = getattr(request, 'toolbar', None) if (getattr(toolbar, 'edit_mode', False) and (not page or page.has_change_permission(request))): edit = True if edit: from cms.middleware.toolbar import toolbar_plugin_processor processors = (toolbar_plugin_processor,) else: processors = None content.extend(render_plugins(plugins, context, placeholder, processors)) content = "".join(content) if edit: content = render_placeholder_toolbar(placeholder, context, content, name_fallback) context.pop() return content
def edit_mode(self, placeholder, context): from cms.utils.placeholder import get_page_from_placeholder_if_exists request = context['request'] page = get_page_from_placeholder_if_exists(placeholder) if ("edit" in request.GET or request.session.get("cms_edit", False)) and \ 'cms.middleware.toolbar.ToolbarMiddleware' in settings.MIDDLEWARE_CLASSES and \ request.user.is_staff and request.user.is_authenticated() and \ (not page or page.has_change_permission(request)): return True return False
def render_placeholder(placeholder, context_to_copy, name_fallback="Placeholder"): """ Renders plugins for a placeholder on the given page using shallow copies of the given context, and returns a string containing the rendered output. """ from cms.plugins.utils import get_plugins context = context_to_copy context.push() request = context['request'] plugins = [plugin for plugin in get_plugins(request, placeholder)] page = get_page_from_placeholder_if_exists(placeholder) if page: template = page.template else: template = None # Add extra context as defined in settings, but do not overwrite existing context variables, # since settings are general and database/template are specific # TODO this should actually happen as a plugin context processor, but these currently overwrite # existing context -- maybe change this order? slot = getattr(placeholder, 'slot', None) extra_context = {} if slot: extra_context = settings.CMS_PLACEHOLDER_CONF.get( "%s %s" % (template, slot), {}).get("extra_context", None) if not extra_context: extra_context = settings.CMS_PLACEHOLDER_CONF.get(slot, {}).get( "extra_context", {}) for key, value in extra_context.items(): if not key in context: context[key] = value c = [] # Prepend frontedit toolbar output if applicable edit = False if ("edit" in request.GET or request.session.get("cms_edit", False)) and \ 'cms.middleware.toolbar.ToolbarMiddleware' in django_settings.MIDDLEWARE_CLASSES and \ request.user.is_staff and request.user.is_authenticated() and \ (not page or page.has_change_permission(request)): edit = True if edit: from cms.middleware.toolbar import toolbar_plugin_processor processors = (toolbar_plugin_processor, ) else: processors = None c.extend(render_plugins(plugins, context, placeholder, processors)) content = "".join(c) if edit: content = render_placeholder_toolbar(placeholder, context, content, name_fallback) context.pop() return content
def delete_with_public(self): """ Delete the public copy of this plugin if it exists, then delete the draft """ position = self.position slot = self.placeholder.slot page = get_page_from_placeholder_if_exists(self.placeholder) if page and getattr(page, 'publisher_public'): try: placeholder = Placeholder.objects.get(page=page.publisher_public, slot=slot) except Placeholder.DoesNotExist: pass else: public_plugin = CMSPlugin.objects.filter(placeholder=placeholder, position=position) public_plugin.delete() self.placeholder = None self.delete()
def delete_with_public(self): """ Delete the public copy of this plugin if it exists, then delete the draft """ position = self.position slot = self.placeholder.slot page = get_page_from_placeholder_if_exists(self.placeholder) if page and getattr(page, 'publisher_public'): try: placeholder = Placeholder.objects.get(page=page.publisher_public, slot=slot) except Placeholder.DoesNotExist: pass else: public_plugin = CMSPlugin.objects.filter(placeholder=placeholder, position=position) public_plugin.delete() self.placeholder = None self.delete()
def render_placeholder_toolbar(placeholder, context, content, name_fallback=None): from cms.plugin_pool import plugin_pool request = context['request'] page = get_page_from_placeholder_if_exists(placeholder) if not page: page = getattr(request, 'current_page', None) if page: template = page.template if name_fallback and not placeholder: placeholder = Placeholder.objects.create(slot=name_fallback) page.placeholders.add(placeholder) else: template = None if placeholder: slot = placeholder.slot else: slot = None installed_plugins = plugin_pool.get_all_plugins(slot, page) mixed_key = "%s %s" % (template, slot) name = settings.CMS_PLACEHOLDER_CONF.get(mixed_key, {}).get("name", None) if not name: name = settings.CMS_PLACEHOLDER_CONF.get(slot, {}).get("name", None) if name: name = _(name) elif slot: name = title(slot) if not name: name = name_fallback toolbar = render_to_string( "cms/toolbar/add_plugins.html", { 'installed_plugins': installed_plugins, 'language': get_language_from_request(request), 'placeholder_label': name, 'placeholder': placeholder, 'page': page, }) return "".join([toolbar, content])
def render_placeholder_toolbar(placeholder, context, content): from cms.plugin_pool import plugin_pool request = context['request'] page = get_page_from_placeholder_if_exists(placeholder) if page: template = page.template else: template = None installed_plugins = plugin_pool.get_all_plugins(placeholder.slot, page) name = settings.CMS_PLACEHOLDER_CONF.get("%s %s" % (template, placeholder.slot), {}).get("name", None) if not name: name = settings.CMS_PLACEHOLDER_CONF.get(placeholder.slot, {}).get("name", None) if not name: name = placeholder.slot name = title(name) toolbar = render_to_string("cms/toolbar/add_plugins.html", { 'installed_plugins': installed_plugins, 'language': get_language_from_request(request), 'placeholder_label': name, 'placeholder': placeholder, 'page': page, }) return "".join([toolbar, content])
def render_placeholder(placeholder, context_to_copy): """ Renders plugins for a placeholder on the given page using shallow copies of the given context, and returns a string containing the rendered output. """ from cms.plugins.utils import get_plugins context = copy.copy(context_to_copy) request = context["request"] plugins = [plugin for plugin in get_plugins(request, placeholder)] page = get_page_from_placeholder_if_exists(placeholder) if page: template = page.template else: template = None # Add extra context as defined in settings, but do not overwrite existing context variables, # since settings are general and database/template are specific # TODO this should actually happen as a plugin context processor, but these currently overwrite # existing context -- maybe change this order? extra_context = settings.CMS_PLACEHOLDER_CONF.get("%s %s" % (template, placeholder.slot), {}).get( "extra_context", None ) if not extra_context: extra_context = settings.CMS_PLACEHOLDER_CONF.get(placeholder.slot, {}).get("extra_context", {}) for key, value in extra_context.items(): if not key in context: context[key] = value c = [] # Prepend frontedit toolbar output if applicable edit = False if ( ("edit" in request.GET or request.session.get("cms_edit", False)) and "cms.middleware.toolbar.ToolbarMiddleware" in django_settings.MIDDLEWARE_CLASSES and request.user.is_staff and request.user.is_authenticated() and (not page or page.has_change_permission(request)) ): edit = True if edit: from cms.plugin_pool import plugin_pool installed_plugins = plugin_pool.get_all_plugins(placeholder, page) name = settings.CMS_PLACEHOLDER_CONF.get("%s %s" % (template, placeholder.slot), {}).get("name", None) if not name: name = settings.CMS_PLACEHOLDER_CONF.get(placeholder.slot, {}).get("name", None) if not name: name = placeholder.slot name = title(name) c.append( render_to_string( "cms/toolbar/add_plugins.html", { "installed_plugins": installed_plugins, "language": get_language_from_request(request), "placeholder_label": name, "placeholder": placeholder, "page": page, }, ) ) from cms.middleware.toolbar import toolbar_plugin_processor processors = (toolbar_plugin_processor,) else: processors = None c.extend(render_plugins(plugins, context, placeholder, processors)) return "".join(c)
def test_copy_page_nested_plugin(self): """ Test to verify that page copy with a nested plugin works page one - 3 placeholder col_sidebar: 1 text plugin col_left: 1 text plugin with nested link plugin col_right: no plugin page two (copy target) Verify copied page, placeholders, plugins and body text """ with SettingsOverride(CMS_MODERATOR=False, CMS_PERMISSION=False): templates = [] # setup page 1 page_one = create_page(u"Three Placeholder", u"col_three.html", u"en", position=u"last-child", published=True, in_navigation=True) page_one_ph_one = page_one.placeholders.get(slot=u"col_sidebar") page_one_ph_two = page_one.placeholders.get(slot=u"col_left") page_one_ph_three = page_one.placeholders.get(slot=u"col_right") # add the text plugin to placeholder one text_plugin_en = add_plugin(page_one_ph_one, u"TextPlugin", u"en", body="Hello World") self.assertEquals(text_plugin_en.id, CMSPlugin.objects.all()[0].id) self.assertEquals(text_plugin_en.get_children().count(), 0) pre_add_plugin_count = CMSPlugin.objects.count() self.assertEqual(pre_add_plugin_count, 1) ### # add a plugin to placeholder two ### pre_nesting_body = u"<p>the nested text plugin with a link inside</p>" text_plugin_two = add_plugin(page_one_ph_two, u"TextPlugin", u"en", body=pre_nesting_body) text_plugin_two = self.reload(text_plugin_two) # prepare nesting plugin page_one_ph_two = self.reload(page_one_ph_two) text_plugin_two = self.reload(text_plugin_two) link_plugin = add_plugin(page_one_ph_two, u"LinkPlugin", u"en", target=text_plugin_two) link_plugin.name = u"django-cms Link" link_plugin.url = u"https://www.django-cms.org" link_plugin.parent = text_plugin_two link_plugin.save() link_plugin = self.reload(link_plugin) text_plugin_two = self.reload(text_plugin_two) in_txt = """<img id="plugin_obj_%s" title="Link" alt="Link" src="/static/cms/images/plugins/link.png">""" nesting_body = "%s<p>%s</p>" % (text_plugin_two.body, (in_txt % (link_plugin.id))) # emulate the editor in admin that adds some txt for the nested plugin text_plugin_two.body = nesting_body text_plugin_two.save() text_plugin_two = self.reload(text_plugin_two) # the link is attached as a child? self.assertEquals(text_plugin_two.get_children().count(), 1) post_add_plugin_count = CMSPlugin.objects.count() self.assertEqual(post_add_plugin_count, 3) page_one.save() # get the plugins from the original page page_one = self.reload(page_one) page_one_ph_one = page_one.placeholders.get(slot=u"col_sidebar") page_one_ph_two = page_one.placeholders.get(slot=u"col_left") page_one_ph_three = page_one.placeholders.get(slot=u"col_right") # verifiy the plugins got created org_placeholder_one_plugins = page_one_ph_one.get_plugins() self.assertEquals(len(org_placeholder_one_plugins), 1) org_placeholder_two_plugins = page_one_ph_two.get_plugins() self.assertEquals(len(org_placeholder_two_plugins), 2) org_placeholder_three_plugins = page_one_ph_three.get_plugins() self.assertEquals(len(org_placeholder_three_plugins), 0) self.assertEquals(page_one.placeholders.count(), 3) placeholder_count = Placeholder.objects.count() self.assertEquals(placeholder_count, 3) self.assertEquals(CMSPlugin.objects.count(), 3) page_one_plugins = CMSPlugin.objects.all() ## # setup page_copy_target page ## page_copy_target = create_page( "Three Placeholder - page copy target", "col_three.html", "en", position="last-child", published=True, in_navigation=True) all_page_count = Page.objects.all().count() pre_copy_placeholder_count = Placeholder.objects.count() self.assertEquals(pre_copy_placeholder_count, 6) # copy the page superuser = self.get_superuser() with self.login_user_context(superuser): page_two = self.copy_page(page_one, page_copy_target) # validate the expected pages,placeholders,plugins,pluginbodies after_copy_page_plugin_count = CMSPlugin.objects.count() self.assertEquals(after_copy_page_plugin_count, 6) # check the amount of copied stuff after_copy_page_count = Page.objects.all().count() after_copy_placeholder_count = Placeholder.objects.count() self.assertTrue((after_copy_page_count > all_page_count), msg=u"no new page after copy") self.assertTrue( (after_copy_page_plugin_count > post_add_plugin_count), msg=u"plugin count is not grown") self.assertTrue( (after_copy_placeholder_count > pre_copy_placeholder_count), msg=u"placeholder count is not grown") self.assertTrue((after_copy_page_count == 3), msg=u"no new page after copy") # orginal placeholder page_one = self.reload(page_one) page_one_ph_one = page_one.placeholders.get(slot=u"col_sidebar") page_one_ph_two = page_one.placeholders.get(slot=u"col_left") page_one_ph_three = page_one.placeholders.get(slot=u"col_right") # check if there are multiple pages assigned to this placeholders found_page = get_page_from_placeholder_if_exists(page_one_ph_one) self.assertEqual(found_page, page_one) found_page = get_page_from_placeholder_if_exists(page_one_ph_two) self.assertEqual(found_page, page_one) found_page = get_page_from_placeholder_if_exists(page_one_ph_three) self.assertEqual(found_page, page_one) page_two = self.reload(page_two) page_two_ph_one = page_two.placeholders.get(slot=u"col_sidebar") page_two_ph_two = page_two.placeholders.get(slot=u"col_left") page_two_ph_three = page_two.placeholders.get(slot=u"col_right") # check if there are multiple pages assigned to this placeholders found_page = get_page_from_placeholder_if_exists(page_two_ph_one) self.assertEqual(found_page, page_two) found_page = get_page_from_placeholder_if_exists(page_two_ph_two) self.assertEqual(found_page, page_two) found_page = get_page_from_placeholder_if_exists(page_two_ph_three) self.assertEqual(found_page, page_two) # check the stored placeholders org vs copy msg = 'placehoder ids copy:%s org:%s copied page %s are identical - tree broken' % ( page_two_ph_one.pk, page_one_ph_one.pk, page_two.pk) self.assertNotEquals(page_two_ph_one.pk, page_one_ph_one.pk, msg) msg = 'placehoder ids copy:%s org:%s copied page %s are identical - tree broken' % ( page_two_ph_two.pk, page_one_ph_two.pk, page_two.pk) self.assertNotEquals(page_two_ph_two.pk, page_one_ph_two.pk, msg) msg = 'placehoder ids copy:%s org:%s copied page %s are identical - tree broken' % ( page_two_ph_three.pk, page_one_ph_three.pk, page_two.pk) self.assertNotEquals(page_two_ph_three.pk, page_one_ph_three.pk, msg) # get the plugins from the original page org_placeholder_one_plugins = page_one_ph_one.get_plugins() self.assertEquals(len(org_placeholder_one_plugins), 1) org_placeholder_two_plugins = page_one_ph_two.get_plugins() self.assertEquals(len(org_placeholder_two_plugins), 2) org_placeholder_three_plugins = page_one_ph_three.get_plugins() self.assertEquals(len(org_placeholder_three_plugins), 0) # get the plugins from the copied page copied_placeholder_one_plugins = page_two_ph_one.get_plugins() self.assertEquals(len(copied_placeholder_one_plugins), 1) copied_placeholder_two_plugins = page_two_ph_two.get_plugins() self.assertEquals(len(copied_placeholder_two_plugins), 2) copied_placeholder_three_plugins = page_two_ph_three.get_plugins() self.assertEquals(len(copied_placeholder_three_plugins), 0) # verify the plugins got copied # placeholder 1 count_plugins_copied = len(copied_placeholder_one_plugins) count_plugins_org = len(org_placeholder_one_plugins) msg = u"plugin count %s %s for placeholder one not equal" % ( count_plugins_copied, count_plugins_org) self.assertEquals(count_plugins_copied, count_plugins_org, msg) # placeholder 2 count_plugins_copied = len(copied_placeholder_two_plugins) count_plugins_org = len(org_placeholder_two_plugins) msg = u"plugin count %s %s for placeholder two not equal" % ( count_plugins_copied, count_plugins_org) self.assertEquals(count_plugins_copied, count_plugins_org, msg) # placeholder 3 count_plugins_copied = len(copied_placeholder_three_plugins) count_plugins_org = len(org_placeholder_three_plugins) msg = u"plugin count %s %s for placeholder three not equal" % ( count_plugins_copied, count_plugins_org) self.assertEquals(count_plugins_copied, count_plugins_org, msg) # verify the body of text plugin with nested link plugin # org to copied org_nested_text_plugin = None # do this iteration to find the real text plugin with the attached link # the inheritance mechanism for the cmsplugins works through # (tuple)get_plugin_instance() for x in org_placeholder_two_plugins: if x.plugin_type == u"TextPlugin": instance = x.get_plugin_instance()[0] if instance.body.startswith(pre_nesting_body): org_nested_text_plugin = instance break copied_nested_text_plugin = None for x in copied_placeholder_two_plugins: if x.plugin_type == u"TextPlugin": instance = x.get_plugin_instance()[0] if instance.body.startswith(pre_nesting_body): copied_nested_text_plugin = instance break msg = u"orginal nested text plugin not found" self.assertNotEquals(org_nested_text_plugin, None, msg=msg) msg = u"copied nested text plugin not found" self.assertNotEquals(copied_nested_text_plugin, None, msg=msg) # get the children ids of the texplugin with a nested link # to check if the body of the text is genrated correctly org_link_child_plugin = org_nested_text_plugin.get_children()[0] copied_link_child_plugin = copied_nested_text_plugin.get_children( )[0] # validate the textplugin body texts msg = u"org plugin and copied plugin are the same" self.assertTrue( org_link_child_plugin.id != copied_link_child_plugin.id, msg) needle = u"plugin_obj_%s" msg = u"child plugin id differs to parent in body plugin_obj_id" # linked child is in body self.assertTrue( org_nested_text_plugin.body.find( needle % (org_link_child_plugin.id)) != -1, msg) msg = u"copy: child plugin id differs to parent in body plugin_obj_id" self.assertTrue( copied_nested_text_plugin.body.find( needle % (copied_link_child_plugin.id)) != -1, msg) # really nothing else msg = u"child link plugin id differs to parent body plugin_obj_id" self.assertTrue( org_nested_text_plugin.body.find( needle % (copied_link_child_plugin.id)) == -1, msg) msg = u"copy: child link plugin id differs to parent body plugin_obj_id" self.assertTrue( copied_nested_text_plugin.body.find( needle % (org_link_child_plugin.id)) == -1, msg) # now reverse lookup the placeholders from the plugins org_placeholder = org_link_child_plugin.placeholder copied_placeholder = copied_link_child_plugin.placeholder msg = u"placeholder of the orginal plugin and copied plugin are the same" ok = ((org_placeholder.id != copied_placeholder.id)) self.assertTrue(ok, msg)
def page(self): warnings.warn( "Don't use the page attribute on CMSPlugins! CMSPlugins are not " "guaranteed to have a page associated with them!", DontUsePageAttributeWarning) return get_page_from_placeholder_if_exists(self.placeholder)
def test_copy_page_nested_plugin(self): """ Test to verify that page copy with a nested plugin works page one - 3 placeholder col_sidebar: 1 text plugin col_left: 1 text plugin with nested link plugin col_right: no plugin page two (copy target) Verify copied page, placeholders, plugins and body text """ with SettingsOverride(CMS_MODERATOR=False, CMS_PERMISSION=False): templates = [] # setup page 1 page_one = create_page(u"Three Placeholder", u"col_three.html", u"en", position=u"last-child", published=True, in_navigation=True) page_one_ph_one = page_one.placeholders.get(slot=u"col_sidebar") page_one_ph_two = page_one.placeholders.get(slot=u"col_left") page_one_ph_three = page_one.placeholders.get(slot=u"col_right") # add the text plugin to placeholder one text_plugin_en = add_plugin(page_one_ph_one, u"TextPlugin", u"en", body="Hello World") self.assertEquals(text_plugin_en.id, CMSPlugin.objects.all()[0].id) self.assertEquals(text_plugin_en.get_children().count(), 0) pre_add_plugin_count = CMSPlugin.objects.count() self.assertEqual(pre_add_plugin_count, 1) ### # add a plugin to placeholder two ### pre_nesting_body = u"<p>the nested text plugin with a link inside</p>" text_plugin_two = add_plugin(page_one_ph_two, u"TextPlugin", u"en", body=pre_nesting_body) text_plugin_two = self.reload(text_plugin_two) # prepare nesting plugin page_one_ph_two = self.reload(page_one_ph_two) text_plugin_two = self.reload(text_plugin_two) link_plugin = add_plugin(page_one_ph_two, u"LinkPlugin", u"en", target=text_plugin_two) link_plugin.name = u"django-cms Link" link_plugin.url = u"https://www.django-cms.org" link_plugin.parent = text_plugin_two link_plugin.save() link_plugin = self.reload(link_plugin) text_plugin_two = self.reload(text_plugin_two) in_txt = """<img id="plugin_obj_%s" title="Link" alt="Link" src="/static/cms/images/plugins/link.png">""" nesting_body = "%s<p>%s</p>" % (text_plugin_two.body, (in_txt % (link_plugin.id))) # emulate the editor in admin that adds some txt for the nested plugin text_plugin_two.body = nesting_body text_plugin_two.save() text_plugin_two = self.reload(text_plugin_two) # the link is attached as a child? self.assertEquals(text_plugin_two.get_children().count(), 1) post_add_plugin_count = CMSPlugin.objects.count() self.assertEqual(post_add_plugin_count, 3) page_one.save() # get the plugins from the original page page_one = self.reload(page_one) page_one_ph_one = page_one.placeholders.get(slot = u"col_sidebar") page_one_ph_two = page_one.placeholders.get(slot = u"col_left") page_one_ph_three = page_one.placeholders.get(slot = u"col_right") # verifiy the plugins got created org_placeholder_one_plugins = page_one_ph_one.get_plugins() self.assertEquals(len(org_placeholder_one_plugins), 1) org_placeholder_two_plugins = page_one_ph_two.get_plugins() self.assertEquals(len(org_placeholder_two_plugins), 2) org_placeholder_three_plugins = page_one_ph_three.get_plugins() self.assertEquals(len(org_placeholder_three_plugins), 0) self.assertEquals(page_one.placeholders.count(), 3) placeholder_count = Placeholder.objects.count() self.assertEquals(placeholder_count, 3) self.assertEquals(CMSPlugin.objects.count(), 3) page_one_plugins = CMSPlugin.objects.all() ## # setup page_copy_target page ## page_copy_target = create_page("Three Placeholder - page copy target", "col_three.html", "en", position="last-child", published=True, in_navigation=True) all_page_count = Page.objects.all().count() pre_copy_placeholder_count = Placeholder.objects.count() self.assertEquals(pre_copy_placeholder_count, 6) # copy the page superuser = self.get_superuser() with self.login_user_context(superuser): page_two = self.copy_page(page_one, page_copy_target) # validate the expected pages,placeholders,plugins,pluginbodies after_copy_page_plugin_count = CMSPlugin.objects.count() self.assertEquals(after_copy_page_plugin_count, 6) # check the amount of copied stuff after_copy_page_count = Page.objects.all().count() after_copy_placeholder_count = Placeholder.objects.count() self.assertTrue((after_copy_page_count > all_page_count), msg = u"no new page after copy") self.assertTrue((after_copy_page_plugin_count > post_add_plugin_count), msg = u"plugin count is not grown") self.assertTrue((after_copy_placeholder_count > pre_copy_placeholder_count), msg = u"placeholder count is not grown") self.assertTrue((after_copy_page_count == 3), msg = u"no new page after copy") # orginal placeholder page_one = self.reload(page_one) page_one_ph_one = page_one.placeholders.get(slot = u"col_sidebar") page_one_ph_two = page_one.placeholders.get(slot = u"col_left") page_one_ph_three = page_one.placeholders.get(slot = u"col_right") # check if there are multiple pages assigned to this placeholders found_page = get_page_from_placeholder_if_exists(page_one_ph_one) self.assertEqual(found_page, page_one) found_page = get_page_from_placeholder_if_exists(page_one_ph_two) self.assertEqual(found_page, page_one) found_page = get_page_from_placeholder_if_exists(page_one_ph_three) self.assertEqual(found_page, page_one) page_two = self.reload(page_two) page_two_ph_one = page_two.placeholders.get(slot = u"col_sidebar") page_two_ph_two = page_two.placeholders.get(slot = u"col_left") page_two_ph_three = page_two.placeholders.get(slot = u"col_right") # check if there are multiple pages assigned to this placeholders found_page = get_page_from_placeholder_if_exists(page_two_ph_one) self.assertEqual(found_page, page_two) found_page = get_page_from_placeholder_if_exists(page_two_ph_two) self.assertEqual(found_page, page_two) found_page = get_page_from_placeholder_if_exists(page_two_ph_three) self.assertEqual(found_page, page_two) # check the stored placeholders org vs copy msg = 'placehoder ids copy:%s org:%s copied page %s are identical - tree broken' % (page_two_ph_one.pk, page_one_ph_one.pk, page_two.pk) self.assertNotEquals(page_two_ph_one.pk, page_one_ph_one.pk, msg) msg = 'placehoder ids copy:%s org:%s copied page %s are identical - tree broken' % (page_two_ph_two.pk, page_one_ph_two.pk, page_two.pk) self.assertNotEquals(page_two_ph_two.pk, page_one_ph_two.pk, msg) msg = 'placehoder ids copy:%s org:%s copied page %s are identical - tree broken' % (page_two_ph_three.pk, page_one_ph_three.pk, page_two.pk) self.assertNotEquals(page_two_ph_three.pk, page_one_ph_three.pk, msg) # get the plugins from the original page org_placeholder_one_plugins = page_one_ph_one.get_plugins() self.assertEquals(len(org_placeholder_one_plugins), 1) org_placeholder_two_plugins = page_one_ph_two.get_plugins() self.assertEquals(len(org_placeholder_two_plugins), 2) org_placeholder_three_plugins = page_one_ph_three.get_plugins() self.assertEquals(len(org_placeholder_three_plugins), 0) # get the plugins from the copied page copied_placeholder_one_plugins = page_two_ph_one.get_plugins() self.assertEquals(len(copied_placeholder_one_plugins), 1) copied_placeholder_two_plugins = page_two_ph_two.get_plugins() self.assertEquals(len(copied_placeholder_two_plugins), 2) copied_placeholder_three_plugins = page_two_ph_three.get_plugins() self.assertEquals(len(copied_placeholder_three_plugins), 0) # verify the plugins got copied # placeholder 1 count_plugins_copied = len(copied_placeholder_one_plugins) count_plugins_org = len(org_placeholder_one_plugins) msg = u"plugin count %s %s for placeholder one not equal" % (count_plugins_copied, count_plugins_org) self.assertEquals(count_plugins_copied, count_plugins_org, msg) # placeholder 2 count_plugins_copied = len(copied_placeholder_two_plugins) count_plugins_org = len(org_placeholder_two_plugins) msg = u"plugin count %s %s for placeholder two not equal" % (count_plugins_copied, count_plugins_org) self.assertEquals(count_plugins_copied, count_plugins_org, msg) # placeholder 3 count_plugins_copied = len(copied_placeholder_three_plugins) count_plugins_org = len(org_placeholder_three_plugins) msg = u"plugin count %s %s for placeholder three not equal" % (count_plugins_copied, count_plugins_org) self.assertEquals(count_plugins_copied, count_plugins_org, msg) # verify the body of text plugin with nested link plugin # org to copied org_nested_text_plugin = None # do this iteration to find the real text plugin with the attached link # the inheritance mechanism for the cmsplugins works through # (tuple)get_plugin_instance() for x in org_placeholder_two_plugins: if x.plugin_type == u"TextPlugin": instance = x.get_plugin_instance()[0] if instance.body.startswith(pre_nesting_body): org_nested_text_plugin = instance break copied_nested_text_plugin = None for x in copied_placeholder_two_plugins: if x.plugin_type == u"TextPlugin": instance = x.get_plugin_instance()[0] if instance.body.startswith(pre_nesting_body): copied_nested_text_plugin = instance break msg = u"orginal nested text plugin not found" self.assertNotEquals(org_nested_text_plugin, None, msg=msg) msg = u"copied nested text plugin not found" self.assertNotEquals(copied_nested_text_plugin, None, msg=msg) # get the children ids of the texplugin with a nested link # to check if the body of the text is genrated correctly org_link_child_plugin = org_nested_text_plugin.get_children()[0] copied_link_child_plugin = copied_nested_text_plugin.get_children()[0] # validate the textplugin body texts msg = u"org plugin and copied plugin are the same" self.assertTrue(org_link_child_plugin.id != copied_link_child_plugin.id, msg) needle = u"plugin_obj_%s" msg = u"child plugin id differs to parent in body plugin_obj_id" # linked child is in body self.assertTrue(org_nested_text_plugin.body.find(needle % (org_link_child_plugin.id)) != -1, msg) msg = u"copy: child plugin id differs to parent in body plugin_obj_id" self.assertTrue(copied_nested_text_plugin.body.find(needle % (copied_link_child_plugin.id)) != -1, msg) # really nothing else msg = u"child link plugin id differs to parent body plugin_obj_id" self.assertTrue(org_nested_text_plugin.body.find(needle % (copied_link_child_plugin.id)) == -1, msg) msg = u"copy: child link plugin id differs to parent body plugin_obj_id" self.assertTrue(copied_nested_text_plugin.body.find(needle % (org_link_child_plugin.id)) == -1, msg) # now reverse lookup the placeholders from the plugins org_placeholder = org_link_child_plugin.placeholder copied_placeholder = copied_link_child_plugin.placeholder msg = u"placeholder of the orginal plugin and copied plugin are the same" ok = ((org_placeholder.id != copied_placeholder.id)) self.assertTrue(ok, msg)
def page(self): return get_page_from_placeholder_if_exists(self.placeholder)