本文整理汇总了Python中cms.models.pluginmodel.CMSPlugin类的典型用法代码示例。如果您正苦于以下问题:Python CMSPlugin类的具体用法?Python CMSPlugin怎么用?Python CMSPlugin使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CMSPlugin类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_inherit_plugin_with_empty_plugin
def test_inherit_plugin_with_empty_plugin(self):
inheritfrompage = create_page('page to inherit from',
'nav_playground.html',
'en', published=True)
body = inheritfrompage.placeholders.get(slot="body")
empty_plugin = CMSPlugin(
plugin_type='TextPlugin', # create an empty plugin
placeholder=body,
position=1,
language='en',
)
empty_plugin.insert_at(None, position='last-child', save=True)
other_page = create_page('other page', 'nav_playground.html', 'en', published=True)
inherited_body = other_page.placeholders.get(slot="body")
inherit_plugin = InheritPagePlaceholder(
plugin_type='InheritPagePlaceholderPlugin',
placeholder=inherited_body,
position=1,
language='en',
from_page=inheritfrompage,
from_language='en'
)
inherit_plugin.insert_at(None, position='last-child', save=True)
add_plugin(inherited_body, "TextPlugin", "en", body="foobar")
# this should not fail, even if there in an empty plugin
rendered = inherited_body.render(context=self.get_context(other_page.get_absolute_url()), width=200)
self.assertIn("foobar", rendered)
示例2: add_plugin
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)
max_pos = CMSPlugin.objects.filter(language=language,
placeholder=placeholder).aggregate(Max('position'))['position__max'] or 0
plugin_base = CMSPlugin(
plugin_type=plugin_type,
placeholder=placeholder,
position=max_pos + 1,
language=language
)
plugin_base.insert_at(target, position=position, save=False)
plugin = plugin_model(**data)
plugin_base.set_base_attr(plugin)
plugin.save()
return plugin
示例3: save_body
def save_body(self, placeholder, desc):
language = settings.LANGUAGE_CODE
try:
plugin = CMSPlugin.objects.get(placeholder=placeholder, position=0, language=language)
plugin.body = desc
except CMSPlugin.DoesNotExist:
plugin = CMSPlugin(language=language, plugin_type="TextPlugin", position=0, placeholder=placeholder)
plugin.save()
if plugin:
text = Text()
# text.set_base_attr(plugin)
text.pk = plugin.pk
text.id = plugin.pk
text.placeholder = placeholder
text.position = plugin.position
text.plugin_type = plugin.plugin_type
text.tree_id = plugin.tree_id
text.lft = plugin.lft
text.rght = plugin.rght
text.level = plugin.level
text.cmsplugin_ptr = plugin
text.publisher_public_id = None
text.public_id = None
text.published = False
text.language = language
text.body = desc
text.save()
示例4: add_plugin
def add_plugin(placeholder, plugin_type, language, position='last-child',
**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)
plugin_base = CMSPlugin(
plugin_type=plugin_type,
placeholder=placeholder,
position=1,
language=language
)
plugin_base.insert_at(None, position=position, save=False)
plugin = plugin_model(**data)
plugin_base.set_base_attr(plugin)
plugin.save()
return plugin
示例5: test_delete_with_plugins
def test_delete_with_plugins(self):
"""
Check that plugins and placeholders get correctly deleted when we delete
a page!
"""
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.insert_at(None, position='last-child', save=False)
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)
示例6: add_plugin
def add_plugin(self,
user=None,
page=None,
placeholder=None,
language='en',
body=''):
if not placeholder:
if page:
placeholder = page.placeholders.get(
slot__iexact='Right-Column')
else:
placeholder = page.placeholders.get(
slot__iexact='Right-Column')
plugin_base = CMSPlugin(
plugin_type='TextPlugin',
placeholder=placeholder,
position=1,
language=language)
plugin_base.insert_at(None, position='last-child', commit=False)
plugin = Text(body=body)
plugin_base.set_base_attr(plugin)
plugin.save()
return plugin.pk
示例7: add_plugin
def add_plugin(self, request):
# only allow POST
if request.method != "POST":
raise Http404
plugin_type = request.POST['plugin_type']
if not has_plugin_permission(request.user, plugin_type, "add"):
return HttpResponseForbidden("You don't have permission to add plugins")
placeholder_id = request.POST.get('placeholder', None)
position = None
language = get_language_from_request(request)
parent = None
# check if we got a placeholder (id)
if placeholder_id:
placeholder = get_object_or_404(Placeholder, pk=placeholder_id)
else: # else get the parent_id
parent_id = request.POST.get('parent_id', None)
if not parent_id: # if we get neither a placeholder nor a parent, bail out
raise Http404
parent = get_object_or_404(CMSPlugin, pk=parent_id)
placeholder = parent.placeholder
# check add permissions on placeholder
if not placeholder.has_add_permission(request):
return HttpResponseForbidden(_("You don't have permission to add content here."))
# check the limits defined in CMS_PLACEHOLDER_CONF for this placeholder
limits = settings.CMS_PLACEHOLDER_CONF.get(placeholder.slot, {}).get('limits', None)
if limits:
count = placeholder.cmsplugin_set.count()
global_limit = limits.get("global", None)
type_limit = limits.get(plugin_type, None)
# check the global limit first
if global_limit and count >= global_limit:
return HttpResponseBadRequest(
"This placeholder already has the maximum number of plugins."
)
elif type_limit: # then check the type specific limit
type_count = CMSPlugin.objects.filter(
language=language, placeholder=placeholder, plugin_type=plugin_type
).count()
if type_count >= type_limit:
return HttpResponseBadRequest(
"This placeholder already has the maximum number (%s) "
"of %s plugins." % (type_limit, plugin_type)
)
# position plugin at the end of the list
position = CMSPlugin.objects.filter(placeholder=placeholder).count()
# actually add the plugin
plugin = CMSPlugin(language=language, plugin_type=plugin_type,
position=position, placeholder=placeholder, parent=parent)
plugin.save()
# returns it's ID as response
return HttpResponse(str(plugin.pk))
示例8: add_plugin
def add_plugin(self, request):
if request.method != "POST":
raise Http404
plugin_type = request.POST['plugin_type']
placeholder_id = request.POST['placeholder']
placeholder = get_object_or_404(Placeholder, pk=placeholder_id)
position = None
language = get_language_from_request(request)
plugin = CMSPlugin(language=language, plugin_type=plugin_type,
position=position, placeholder=placeholder)
plugin.save()
return HttpResponse(str(plugin.pk))
示例9: test_empty_plugin_is_ignored
def test_empty_plugin_is_ignored(self):
page = create_page("page", "nav_playground.html", "en")
placeholder = page.placeholders.get(slot='body')
plugin = CMSPlugin(
plugin_type='TextPlugin',
placeholder=placeholder,
position=1,
language=self.FIRST_LANG)
plugin.insert_at(None, position='last-child', save=True)
# this should not raise any errors, but just ignore the empty plugin
out = placeholder.render(self.get_context(), width=300)
self.assertFalse(len(out))
self.assertFalse(len(placeholder._en_plugins_cache))
示例10: add_plugin
def add_plugin(placeholder, plugin_type, language, position='last-child', **data):
"""
Taken from django-cms api (in newer versions)
https://github.com/divio/django-cms/blob/b8633b42efcd137d96e1e3f42e004cb7595768fe/cms/api.py
"""
assert isinstance(placeholder, Placeholder)
plugin_model = plugin_type.model
plugin_type = plugin_type.__name__
plugin_base = CMSPlugin(
plugin_type=plugin_type,
placeholder=placeholder,
position=1,
language=language
)
plugin_base.insert_at(None, position='last-child', commit=False)
plugin = plugin_model(**data)
plugin_base.set_base_attr(plugin)
plugin.save()
return plugin
示例11: move_plugin
def move_plugin(self, request):
"""
POST request with following parameters:
-plugin_id
-placeholder_id
-plugin_language (optional)
-plugin_parent (optional)
-plugin_order (array, optional)
"""
plugin = CMSPlugin.objects.get(pk=int(request.POST['plugin_id']))
placeholder = Placeholder.objects.get(pk=request.POST['placeholder_id'])
parent_id = request.POST.get('plugin_parent', None)
language = request.POST.get('plugin_language', None)
source_placeholder = plugin.placeholder
if not parent_id:
parent_id = None
else:
parent_id = int(parent_id)
if not language and plugin.language:
language = plugin.language
order = request.POST.getlist("plugin_order[]")
if not self.has_move_plugin_permission(request, plugin, placeholder):
return HttpResponseForbidden(force_text(_("You have no permission to move this plugin")))
if not placeholder == source_placeholder:
try:
template = self.get_placeholder_template(request, placeholder)
has_reached_plugin_limit(placeholder, plugin.plugin_type, plugin.language, template=template)
except PluginLimitReached as er:
return HttpResponseBadRequest(er)
if parent_id:
if plugin.parent_id != parent_id:
parent = CMSPlugin.objects.get(pk=parent_id)
if parent.placeholder_id != placeholder.pk:
return HttpResponseBadRequest(force_text('parent must be in the same placeholder'))
if parent.language != language:
return HttpResponseBadRequest(force_text('parent must be in the same language as plugin_language'))
plugin.parent_id = parent.pk
plugin.save()
plugin = plugin.move(parent, pos='last-child')
else:
sibling = CMSPlugin.get_last_root_node()
plugin.parent_id = None
plugin.save()
plugin = plugin.move(sibling, pos='right')
for child in [plugin] + list(plugin.get_descendants()):
child.placeholder = placeholder
child.language = language
child.save()
plugins = reorder_plugins(placeholder, parent_id, language, order)
if not plugins:
return HttpResponseBadRequest('order parameter did not have all plugins of the same level in it')
self.post_move_plugin(request, source_placeholder, placeholder, plugin)
json_response = {'reload': requires_reload(PLUGIN_MOVE_ACTION, [plugin])}
return HttpResponse(json.dumps(json_response), content_type='application/json')
示例12: add_plugin
def add_plugin(self, request):
if request.method != "POST":
raise Http404
plugin_type = request.POST['plugin_type']
placeholder_id = request.POST.get('placeholder', None)
position = None
language = get_language_from_request(request)
if not placeholder_id:
parent_id = request.POST.get('parent_id', None)
if not parent_id:
raise Http404
parent = get_object_or_404(CMSPlugin, pk=parent_id)
plugin = CMSPlugin(language=language, plugin_type=plugin_type,
position=position, parent=parent, placeholder=parent.placeholder)
else:
placeholder = get_object_or_404(Placeholder, pk=placeholder_id)
plugin = CMSPlugin(language=language, plugin_type=plugin_type,
position=position, placeholder=placeholder)
plugin.save()
return HttpResponse(str(plugin.pk))
示例13: add_plugin
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':
new_pos = CMSPlugin.objects.filter(language=language, parent=target, tree_id=target.tree_id).count()
elif position == 'first-child':
new_pos = 0
elif position == 'left':
new_pos = target.position
elif position == 'right':
new_pos = target.position + 1
else:
raise Exception('position not supported: %s' % position)
for pl in CMSPlugin.objects.filter(language=language, parent=target.parent_id, tree_id=target.tree_id, position__gte=new_pos):
pl.position += 1
pl.save()
else:
new_pos = CMSPlugin.objects.filter(language=language, parent__isnull=True, placeholder=placeholder).count()
plugin_base = CMSPlugin(
plugin_type=plugin_type,
placeholder=placeholder,
position=new_pos,
language=language
)
plugin_base.insert_at(target, position=position, save=False)
plugin = plugin_model(**data)
plugin_base.set_base_attr(plugin)
plugin.save()
return plugin
示例14: add_plugin
def add_plugin(self, request):
"""
POST request should have the following data:
- placeholder_id
- plugin_type
- plugin_language
- plugin_parent (optional)
"""
parent = None
plugin_type = request.POST['plugin_type']
placeholder_id = request.POST.get('placeholder_id', None)
placeholder = get_object_or_404(Placeholder, pk=placeholder_id)
parent_id = request.POST.get('plugin_parent', None)
language = request.POST.get('plugin_language') or get_language_from_request(request)
if not self.has_add_plugin_permission(request, placeholder, plugin_type):
return HttpResponseForbidden(force_unicode(_('You do not have permission to add a plugin')))
try:
has_reached_plugin_limit(placeholder, plugin_type, language,
template=self.get_placeholder_template(request, placeholder))
except PluginLimitReached as er:
return HttpResponseBadRequest(er)
# page add-plugin
if not parent_id:
position = request.POST.get('plugin_order',
CMSPlugin.objects.filter(language=language, placeholder=placeholder).count())
# in-plugin add-plugin
else:
parent = get_object_or_404(CMSPlugin, pk=parent_id)
placeholder = parent.placeholder
position = request.POST.get('plugin_order',
CMSPlugin.objects.filter(language=language, parent=parent).count())
# placeholder (non-page) add-plugin
# Sanity check to make sure we're not getting bogus values from JavaScript:
if settings.USE_I18N:
if not language or not language in [lang[0] for lang in settings.LANGUAGES]:
return HttpResponseBadRequest(force_unicode(_("Language must be set to a supported language!")))
if parent and parent.language != language:
return HttpResponseBadRequest(force_unicode(_("Parent plugin language must be same as language!")))
else:
language = settings.LANGUAGE_CODE
plugin = CMSPlugin(language=language, plugin_type=plugin_type, position=position, placeholder=placeholder)
if parent:
plugin.position = CMSPlugin.objects.filter(parent=parent).count()
plugin.parent_id = parent.pk
plugin.save()
self.post_add_plugin(request, placeholder, plugin)
response = {
'url': force_unicode(
admin_reverse("%s_%s_edit_plugin" % (self.model._meta.app_label, self.model._meta.model_name),
args=[plugin.pk])),
'delete': force_unicode(
admin_reverse("%s_%s_delete_plugin" % (self.model._meta.app_label, self.model._meta.model_name),
args=[plugin.pk])),
'breadcrumb': plugin.get_breadcrumb(),
}
return HttpResponse(json.dumps(response), content_type='application/json')
示例15: test_list_plugins
def test_list_plugins(self):
out = StringIO()
apps = ["cms", "menus", "sekizai", "cms.test_utils.project.sampleapp"]
with SettingsOverride(INSTALLED_APPS=apps):
placeholder = Placeholder.objects.create(slot="test")
add_plugin(placeholder, TextPlugin, "en", body="en body")
add_plugin(placeholder, TextPlugin, "en", body="en body")
link_plugin = add_plugin(placeholder, "LinkPlugin", "en", name="A Link", url="https://www.django-cms.org")
self.assertEqual(CMSPlugin.objects.filter(plugin_type=PLUGIN).count(), 2)
self.assertEqual(CMSPlugin.objects.filter(plugin_type="LinkPlugin").count(), 1)
# create a CMSPlugin with an unsaved instance
instanceless_plugin = CMSPlugin(language="en", plugin_type="TextPlugin")
instanceless_plugin.save()
# create a bogus CMSPlugin to simulate one which used to exist but
# is no longer installed
bogus_plugin = CMSPlugin(language="en", plugin_type="BogusPlugin")
bogus_plugin.save()
report = plugin_report()
# there should be reports for three plugin types
self.assertEqual(len(report), 3)
# check the bogus plugin
bogus_plugins_report = report[0]
self.assertEqual(bogus_plugins_report["model"], None)
self.assertEqual(bogus_plugins_report["type"], u"BogusPlugin")
self.assertEqual(bogus_plugins_report["instances"][0], bogus_plugin)
# check the link plugin
link_plugins_report = report[1]
self.assertEqual(link_plugins_report["model"], link_plugin.__class__)
self.assertEqual(link_plugins_report["type"], u"LinkPlugin")
self.assertEqual(link_plugins_report["instances"][0].get_plugin_instance()[0], link_plugin)
# check the text plugins
text_plugins_report = report[2]
self.assertEqual(text_plugins_report["model"], TextPlugin.model)
self.assertEqual(text_plugins_report["type"], u"TextPlugin")
self.assertEqual(len(text_plugins_report["instances"]), 3)
self.assertEqual(text_plugins_report["instances"][2], instanceless_plugin)
self.assertEqual(text_plugins_report["unsaved_instances"], [instanceless_plugin])