本文整理汇总了Python中cms.models.pluginmodel.CMSPlugin.add_root方法的典型用法代码示例。如果您正苦于以下问题:Python CMSPlugin.add_root方法的具体用法?Python CMSPlugin.add_root怎么用?Python CMSPlugin.add_root使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cms.models.pluginmodel.CMSPlugin
的用法示例。
在下文中一共展示了CMSPlugin.add_root方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_delete_with_plugins
# 需要导入模块: from cms.models.pluginmodel import CMSPlugin [as 别名]
# 或者: from cms.models.pluginmodel.CMSPlugin import add_root [as 别名]
def test_delete_with_plugins(self):
"""
Check that plugins and placeholders get correctly deleted when we delete
a page!
"""
Text = self.get_plugin_model('TextPlugin')
home = create_page("home", "nav_playground.html", "en")
page = create_page("page", "nav_playground.html", "en")
page.rescan_placeholders() # create placeholders
placeholder = page.placeholders.all()[0]
plugin_base = CMSPlugin(
plugin_type='TextPlugin',
placeholder=placeholder,
position=1,
language=settings.LANGUAGES[0][0]
)
plugin_base = plugin_base.add_root(instance=plugin_base)
plugin = Text(body='')
plugin_base.set_base_attr(plugin)
plugin.save()
self.assertEqual(CMSPlugin.objects.count(), 1)
self.assertEqual(Text.objects.count(), 1)
self.assertTrue(Placeholder.objects.count() > 2)
page.delete()
home.delete()
self.assertEqual(CMSPlugin.objects.count(), 0)
self.assertEqual(Text.objects.count(), 0)
self.assertEqual(Placeholder.objects.count(), 0)
self.assertEqual(Page.objects.count(), 0)
示例2: add_plugin
# 需要导入模块: from cms.models.pluginmodel import CMSPlugin [as 别名]
# 或者: from cms.models.pluginmodel.CMSPlugin import add_root [as 别名]
def add_plugin(placeholder, plugin_type, language, position='last-child',
target=None, **data):
"""
Add a plugin to a placeholder
See docs/extending_cms/api_reference.rst for more info
"""
# validate placeholder
assert isinstance(placeholder, Placeholder)
# validate and normalize plugin type
plugin_model, plugin_type = _verify_plugin_type(plugin_type)
if target:
if position == 'last-child':
if CMSPlugin.node_order_by:
position = 'sorted-child'
new_pos = CMSPlugin.objects.filter(parent=target).count()
parent_id = target.pk
elif position == 'first-child':
new_pos = 0
if CMSPlugin.node_order_by:
position = 'sorted-child'
parent_id = target.pk
elif position == 'left':
new_pos = target.position
if CMSPlugin.node_order_by:
position = 'sorted-sibling'
parent_id = target.parent_id
elif position == 'right':
new_pos = target.position + 1
if CMSPlugin.node_order_by:
position = 'sorted-sibling'
parent_id = target.parent_id
else:
raise Exception('position not supported: %s' % position)
if position == 'last-child' or position == 'first-child':
qs = CMSPlugin.objects.filter(language=language, parent=target, position__gte=new_pos,
placeholder=placeholder)
else:
qs = CMSPlugin.objects.filter(language=language, parent=target.parent_id, position__gte=new_pos,
placeholder=placeholder)
for pl in qs:
pl.position += 1
pl.save()
else:
if position == 'last-child':
new_pos = CMSPlugin.objects.filter(language=language, parent__isnull=True, placeholder=placeholder).count()
else:
new_pos = 0
for pl in CMSPlugin.objects.filter(language=language, parent__isnull=True, position__gte=new_pos,
placeholder=placeholder):
pl.position += 1
pl.save()
parent_id = None
plugin_base = CMSPlugin(
plugin_type=plugin_type,
placeholder=placeholder,
position=new_pos,
language=language,
parent_id=parent_id,
)
plugin_base = plugin_base.add_root(instance=plugin_base)
if target:
plugin_base = plugin_base.move(target, pos=position)
plugin = plugin_model(**data)
plugin_base.set_base_attr(plugin)
plugin.save()
return plugin