def test_check_plugin_instances(self): self.assertCheck(True, warnings=0, errors=0) placeholder = Placeholder.objects.create(slot="test") add_plugin(placeholder, TextPlugin, "en", body="en body") add_plugin(placeholder, TextPlugin, "en", body="en body") add_plugin(placeholder, "LinkPlugin", "en", name="A Link", external_link="https://www.django-cms.org") # create a CMSPlugin with an unsaved instance instanceless_plugin = CMSPlugin(language="en", plugin_type="TextPlugin") instanceless_plugin.save() self.assertCheck(False, warnings=0, errors=2) # 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() self.assertCheck(False, warnings=0, errors=3)
def test_list_plugins(self): 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])
def test_check_plugin_instances(self): self.assertCheck(True, warnings=0, errors=0) 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") # create a CMSPlugin with an unsaved instance instanceless_plugin = CMSPlugin(language="en", plugin_type="TextPlugin") instanceless_plugin.save() self.assertCheck(False, warnings=0, errors=2) # 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() self.assertCheck(False, warnings=0, errors=3)
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()
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)) # 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))
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()
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_text(_('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_text(_("Language must be set to a supported language!"))) if parent and parent.language != language: return HttpResponseBadRequest(force_text(_("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_text( admin_reverse("%s_%s_edit_plugin" % (self.model._meta.app_label, self.model._meta.model_name), args=[plugin.pk])), 'delete': force_text( 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')
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')
def test_delete_orphaned_plugins(self): 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") 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(len(bogus_plugins_report["instances"]), 1) # check the link plugin link_plugins_report = report[1] self.assertEqual(len(link_plugins_report["instances"]), 1) # check the text plugins text_plugins_report = report[2] self.assertEqual(len(text_plugins_report["instances"]), 3) self.assertEqual(len(text_plugins_report["unsaved_instances"]), 1) management.call_command('cms', 'delete_orphaned_plugins', stdout=StringIO(), interactive=False) report = plugin_report() # there should be reports for two plugin types (one should have been deleted) self.assertEqual(len(report), 2) # check the link plugin link_plugins_report = report[0] self.assertEqual(len(link_plugins_report["instances"]), 1) # check the text plugins text_plugins_report = report[1] self.assertEqual(len(text_plugins_report["instances"]), 2) self.assertEqual(len(text_plugins_report["unsaved_instances"]), 0)
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))
def test_delete_orphaned_plugins(self): placeholder = Placeholder.objects.create(slot="test") add_plugin(placeholder, TextPlugin, "en", body="en body") add_plugin(placeholder, TextPlugin, "en", body="en body") add_plugin(placeholder, "LinkPlugin", "en", name="A Link", external_link="https://www.django-cms.org") 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(len(bogus_plugins_report["instances"]), 1) # check the link plugin link_plugins_report = report[1] self.assertEqual(len(link_plugins_report["instances"]), 1) # check the text plugins text_plugins_report = report[2] self.assertEqual(len(text_plugins_report["instances"]), 3) self.assertEqual(len(text_plugins_report["unsaved_instances"]), 1) out = io.StringIO() management.call_command('cms', 'delete-orphaned-plugins', interactive=False, stdout=out) report = plugin_report() # there should be reports for two plugin types (one should have been deleted) self.assertEqual(len(report), 2) # check the link plugin link_plugins_report = report[0] self.assertEqual(len(link_plugins_report["instances"]), 1) # check the text plugins text_plugins_report = report[1] self.assertEqual(len(text_plugins_report["instances"]), 2) self.assertEqual(len(text_plugins_report["unsaved_instances"]), 0)
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))
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])
def test_delete_orphaned_plugins(self): 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") 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(len(bogus_plugins_report["instances"]), 1) # check the link plugin link_plugins_report = report[1] self.assertEqual(len(link_plugins_report["instances"]), 1) # check the text plugins text_plugins_report = report[2] self.assertEqual(len(text_plugins_report["instances"]), 3) self.assertEqual(len(text_plugins_report["unsaved_instances"]), 1) management.call_command("cms", "delete_orphaned_plugins", stdout=StringIO(), interactive=False) report = plugin_report() # there should be reports for two plugin types (one should have been deleted) self.assertEqual(len(report), 2) # check the link plugin link_plugins_report = report[0] self.assertEqual(len(link_plugins_report["instances"]), 1) # check the text plugins text_plugins_report = report[1] self.assertEqual(len(text_plugins_report["instances"]), 2) self.assertEqual(len(text_plugins_report["unsaved_instances"]), 0)
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))
def test_check_plugin_instances(self): self.assertCheck(True, warnings=0, errors=0 ) placeholder = Placeholder.objects.create(slot="test") add_plugin(placeholder, TextPlugin, "en", body="en body") add_plugin(placeholder, TextPlugin, "en", body="en body") add_plugin(placeholder, "LinkPlugin", "en", name="A Link", external_link="https://www.django-cms.org") # create a CMSPlugin with an unsaved instance instanceless_plugin = CMSPlugin(language="en", plugin_type="TextPlugin") instanceless_plugin.save() self.assertCheck(False, warnings=0, errors=2) # 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() self.assertCheck(False, warnings=0, errors=3)
def test_check_plugin_instances(self): self.assertCheck(True, warnings=0, errors=0 ) apps = ["cms", "menus", "sekizai", "cms.test_utils.project.sampleapp", "treebeard"] with self.settings(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") add_plugin(placeholder, "LinkPlugin", "en", name="A Link", url="https://www.django-cms.org") # create a CMSPlugin with an unsaved instance instanceless_plugin = CMSPlugin(language="en", plugin_type="TextPlugin") instanceless_plugin.save() self.assertCheck(False, warnings=0, errors=2) # 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() self.assertCheck(False, warnings=0, errors=3)
def test_list_plugins(self): out = StringIO() 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() management.call_command('cms', 'list', 'plugins', interactive=False, stdout=out) 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])
def test_list_plugins(self): out = io.StringIO() 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", external_link="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() with mock.patch( 'cms.management.commands.subcommands.list.plugin_report' ) as report_fn: management.call_command('cms', 'list', 'plugins', interactive=False, stdout=out) report_fn.assert_called_once() 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])
def test_delete_orphaned_plugins(self): placeholder = Placeholder.objects.create(slot="test") add_plugin(placeholder, TextPlugin, "en", body="en body") add_plugin(placeholder, TextPlugin, "en", body="en body") add_plugin(placeholder, "LinkPlugin", "en", name="A Link", url="https://www.django-cms.org") 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( len(bogus_plugins_report["instances"]), 1) # check the link plugin link_plugins_report = report[1] self.assertEqual( len(link_plugins_report["instances"]), 1) # check the text plugins text_plugins_report = report[2] self.assertEqual( len(text_plugins_report["instances"]), 3) self.assertEqual( len(text_plugins_report["unsaved_instances"]), 1) out = StringIO() management.call_command('cms', 'delete-orphaned-plugins', interactive=False, stdout=out) report = plugin_report() # there should be reports for two plugin types (one should have been deleted) self.assertEqual( len(report), 2) # check the link plugin link_plugins_report = report[0] self.assertEqual( len(link_plugins_report["instances"]), 1) # check the text plugins text_plugins_report = report[1] self.assertEqual( len(text_plugins_report["instances"]), 2) self.assertEqual( len(text_plugins_report["unsaved_instances"]), 0)
def add_plugin(self, request): """ POST request should have the following data: - placeholder_id - plugin_type - plugin_language - plugin_parent (optional) """ plugin_type = request.POST["plugin_type"] placeholder_id = request.POST.get("placeholder_id", None) parent_id = request.POST.get("parent_id", None) if parent_id: warnings.warn( "parent_id is deprecated and will be removed in 3.1, use plugin_parent instead", DeprecationWarning ) if not parent_id: parent_id = request.POST.get("plugin_parent", None) placeholder = get_object_or_404(Placeholder, pk=placeholder_id) if not self.has_add_plugin_permission(request, placeholder, plugin_type): return HttpResponseForbidden(force_unicode(_("You do not have permission to add a plugin"))) parent = None language = request.POST.get("plugin_language") or get_language_from_request(request) 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.insert_at(parent, position="last-child", save=False) plugin.save() self.post_add_plugin(request, placeholder, plugin) response = { "url": force_unicode( reverse( "admin:%s_%s_edit_plugin" % (self.model._meta.app_label, self.model._meta.module_name), args=[plugin.pk], ) ), "delete": force_unicode( reverse( "admin:%s_%s_delete_plugin" % (self.model._meta.app_label, self.model._meta.module_name), args=[plugin.pk], ) ), "breadcrumb": plugin.get_breadcrumb(), } return HttpResponse(json.dumps(response), content_type="application/json")