Skip to content

Commit

Permalink
cms command
Browse files Browse the repository at this point in the history
  • Loading branch information
fivethreeo committed May 27, 2011
1 parent 7b836a8 commit 1d0750e
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 5 deletions.
75 changes: 75 additions & 0 deletions cms/management/commands/cms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from optparse import make_option
from django.core.management.base import BaseCommand, LabelCommand, CommandError
from cms.models.pluginmodel import CMSPlugin
from cms.models.titlemodels import Title

class SubcommandsCommand(BaseCommand):

def __init__(self):
super(SubcommandsCommand, self).__init__()
extra_options = []
for subcommand, command in self.subcommands.items():
extra_options.append(
make_option('--%s' % subcommand,
action='store_true',
dest=subcommand,
default=False,
help=command.help)
)
for option in command.option_list:
extra_options.append(option)

self.option_list = BaseCommand.option_list + tuple(extra_options)

def handle(self, *args, **options):

handle_command = None
for subcommand, command in self.subcommands.items():
if options[subcommand] == True:
handle_command = command()
del options[subcommand]

if handle_command:
handle_command.execute(*args, **options)
else:
raise CommandError('No cms command in options')

class UninstallApphooksCommand(LabelCommand):

option_list = tuple()
args = "APPHOK_NAME"
label = 'apphook name (SampleApp)'
help = 'Uninstalls (sets to null) specified apphooks for all pages'

def handle_label(self, label, **options):
queryset = Title.objects.filter(application_urls=label)
number_of_apphooks = queryset.count()

if number_of_apphooks > 0:
queryset.update(application_urls=None)
self.stdout.write('%d "%s" apphooks uninstalled' % (number_of_apphooks, label))
else:
self.stdout.write('no "%s" apphooks found' % label)

class UninstallPluginsCommand(LabelCommand):

option_list = tuple()
args = "PLUGIN_NAME"
label = 'plugin name (SamplePlugin)'
help = 'Uninstalls (deletes) specified plugins from the CMSPlugin model'

def handle_label(self, label, **options):
queryset = CMSPlugin.objects.filter(type=label)
number_of_plugins = queryset.count()

if number_of_apphooks > 0:
queryset.delete()
self.stdout.write('%d "%s" plugins uninstalled' % (number_of_plugins, label))
else:
self.stdout.write('no "%s" plugins found' % label)

class Command(SubcommandsCommand):
subcommands = {
'uninstall_apphooks': UninstallApphooksCommand,
'uninstall_plugins': UninstallPluginsCommand
}
4 changes: 2 additions & 2 deletions cms/management/commands/uninstall_apphooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ def handle_label(self, label, **options):

if number_of_apphooks > 0:
queryset.update(application_urls=None)
print '%d "%s" apphooks uninstalled' % (number_of_apphooks, label)
self.stdout.write('%d "%s" apphooks uninstalled' % (number_of_apphooks, label))
else:
print 'no "%s" apphooks found' % label
self.stdout.write('no "%s" apphooks found' % label)
11 changes: 8 additions & 3 deletions cms/tests/management.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from __future__ import with_statement
from cms.api import create_page, create_title
from StringIO import StringIO
from cms.api import create_page
from cms.test_utils.testcases import CMSTestCase
from cms.test_utils.util.context_managers import SettingsOverride
from cms.api import create_page
Expand All @@ -13,12 +14,16 @@
class ManagementTestCase(CMSTestCase):

def test_no_apphook(self):
call_command('uninstall_apphooks', APPHOOK, interactive=False)
out = StringIO()
call_command('cms', APPHOOK, uninstall_apphooks=True, interactive=False, stout=out)
self.assertEqual(out.getvalue(), "")

def test_with_apphook(self):
out = StringIO()
apps = ['cms', 'menus', 'sekizai', 'project.sampleapp']
with SettingsOverride(INSTALLED_APPS=apps):
create_page("Hello Title", 'nav_playground.html', 'en', apphook=APPHOOK)
self.assertEqual(Title.objects.filter(application_urls=APPHOOK).count(), 1)
call_command('uninstall_apphooks', APPHOOK, interactive=False)
call_command('cms', APPHOOK, uninstall_apphooks=True, interactive=False, stout=out)
self.assertEqual(out.getvalue(), "")
self.assertEqual(Title.objects.filter(application_urls=APPHOOK).count(), 0)

0 comments on commit 1d0750e

Please sign in to comment.