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 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 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 check_plugin_instances(output): from cms.management.commands.subcommands.list import plugin_report with output.section("Plugin instances") as section: # get the report report = plugin_report() section.success("Plugin instances of %s types found in the database" % len(report)) # loop over plugin types in the report for plugin_type in report: # warn about those that are not installed if not plugin_type["model"]: section.error("%s has instances but is no longer installed" % plugin_type["type"]) # warn about those that have unsaved instances if plugin_type["unsaved_instances"]: section.error("%s has %s unsaved instances" % (plugin_type["type"], len(plugin_type["unsaved_instances"]))) if section.successful: section.finish_success( "The plugins in your database are in good order") else: section.finish_error( "There are potentially serious problems with the plugins in your database. \nEven if " "your site works, you should run the 'manage.py cms list plugins' \ncommand and then " "the 'manage.py cms delete-orphaned-plugins' command. \nThis will alter your " "database; read the documentation before using it.")
def handle_noargs(self, **options): """ Obtains a plugin report - cms.management.commands.subcommands.list.plugin_report - and uses it to delete orphaned plugins from the database, i.e. ones that are no longer installed, and ones that have no corresponding saved plugin instances (as will happen if a plugin is inserted into a placeholder, but not saved). """ self.stdout.write("Obtaining plugin report\n") report = plugin_report() uninstalled_instances = [] unsaved_instances = [] # delete items whose plugin is uninstalled and items with unsaved instances self.stdout.write( "... deleting any instances of uninstalled plugins and unsaved plugin instances\n" ) for plugin in report: if not plugin["model"]: for instance in plugin["instances"]: uninstalled_instances.append(instance) instance.delete() for instance in plugin["unsaved_instances"]: unsaved_instances.append(instance) instance.delete() self.stdout.write( "Deleted instances of: \n %s uninstalled plugins \n %s plugins with unsaved instances\n" % (len(uninstalled_instances), len(unsaved_instances))) self.stdout.write("all done\n")
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 handle_noargs(self, **options): """ Obtains a plugin report - cms.management.commands.subcommands.list.plugin_report - and uses it to delete orphaned plugins from the database, i.e. ones that are no longer installed, and ones that have no corresponding saved plugin instances (as will happen if a plugin is inserted into a placeholder, but not saved). """ self.stdout.write("Obtaining plugin report\n") report = plugin_report() uninstalled_instances = [] unsaved_instances = [] # delete items whose plugin is uninstalled and items with unsaved instances self.stdout.write("... deleting any instances of uninstalled plugins and unsaved plugin instances\n") for plugin in report: if not plugin["model"]: for instance in plugin["instances"]: uninstalled_instances.append(instance) instance.delete() for instance in plugin["unsaved_instances"]: unsaved_instances.append(instance) instance.delete() self.stdout.write("Deleted instances of: \n %s uninstalled plugins \n %s plugins with unsaved instances\n" % (len(uninstalled_instances), len(unsaved_instances))) self.stdout.write("all done\n")
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 handle(self, *args, **options): """ Obtains a plugin report - cms.management.commands.subcommands.list.plugin_report - and uses it to delete orphaned plugins from the database, i.e. ones that are no longer installed, and ones that have no corresponding saved plugin instances (as will happen if a plugin is inserted into a placeholder, but not saved). """ self.stdout.write('Obtaining plugin report\n') uninstalled_instances = [] unsaved_instances = [] for plugin in plugin_report(): if not plugin['model']: for instance in plugin['instances']: uninstalled_instances.append(instance) for instance in plugin['unsaved_instances']: unsaved_instances.append(instance) if options.get('interactive'): confirm = input( """ You have requested to delete any instances of uninstalled plugins and empty plugin instances. There are %d uninstalled plugins and %d empty plugins. Are you sure you want to do this? Type 'yes' to continue, or 'no' to cancel: """ % (len(uninstalled_instances), len(unsaved_instances))) else: confirm = 'yes' if confirm == 'yes': # delete items whose plugin is uninstalled and items with unsaved instances self.stdout.write( '... deleting any instances of uninstalled plugins and empty plugin instances\n' ) for instance in uninstalled_instances: instance.delete() for instance in unsaved_instances: instance.delete() self.stdout.write( 'Deleted instances of: \n %s uninstalled plugins \n %s plugins with unsaved instances\n' % (len(uninstalled_instances), len(unsaved_instances))) self.stdout.write('all done\n')
def check_plugin_instances(output): with output.section("Plugin instances") as section: # get the report report = plugin_report() section.success("Plugin instances of %s types found in the database" % len(report)) # loop over plugin types in the report for plugin_type in report: # warn about those that are not installed if not plugin_type["model"]: section.error("%s has instances but is no longer installed" % plugin_type["type"] ) # warn about those that have unsaved instances if plugin_type["unsaved_instances"]: section.error("%s has %s unsaved instances" % (plugin_type["type"], len(plugin_type["unsaved_instances"]))) if section.successful: section.finish_success("The plugins in your database are in good order") else: section.finish_error("There are potentially serious problems with the plugins in your database. \nEven if your site works, you should run the 'manage.py cms list plugins' \ncommand and then the 'manage.py cms delete_orphaned_plugins' command. \nThis will alter your database; read the documentation before using it.")
def handle_noargs(self, **options): """ Obtains a plugin report - cms.management.commands.subcommands.list.plugin_report - and uses it to delete orphaned plugins from the database, i.e. ones that are no longer installed, and ones that have no corresponding saved plugin instances (as will happen if a plugin is inserted into a placeholder, but not saved). """ self.stdout.write(u"Obtaining plugin report\n") uninstalled_instances = [] unsaved_instances = [] for plugin in plugin_report(): if not plugin["model"]: for instance in plugin["instances"]: uninstalled_instances.append(instance) for instance in plugin["unsaved_instances"]: unsaved_instances.append(instance) if options.get('interactive'): confirm = raw_input(""" You have requested to delete any instances of uninstalled plugins and unsaved plugin instances. There are %d uninstalled plugins and %d unsaved_plugins. Are you sure you want to do this? Type 'yes' to continue, or 'no' to cancel: """ % (len(uninstalled_instances), len(unsaved_instances))) else: confirm = 'yes' if confirm == 'yes': # delete items whose plugin is uninstalled and items with unsaved instances self.stdout.write(u"... deleting any instances of uninstalled plugins and unsaved plugin instances\n") for instance in uninstalled_instances: instance.delete() for instance in unsaved_instances: instance.delete() self.stdout.write(u"Deleted instances of: \n %s uninstalled plugins \n %s plugins with unsaved instances\n" % (len(uninstalled_instances), len(unsaved_instances))) self.stdout.write(u"all done\n")
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 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])