diff options
author | Dejan Noveski <dr.mote@gmail.com> | 2011-11-28 20:23:36 +0100 |
---|---|---|
committer | Dejan Noveski <dr.mote@gmail.com> | 2011-11-28 20:23:36 +0100 |
commit | d03996162cf9438ffee0118e836e39473fed85ea (patch) | |
tree | 900561aeaa885d5a5fb8510161309f85658bf169 | |
parent | 26d86dd6a52261fb5f68fb1a1b1fa4b9e11d88e8 (diff) | |
download | askbot-d03996162cf9438ffee0118e836e39473fed85ea.tar.gz askbot-d03996162cf9438ffee0118e836e39473fed85ea.tar.bz2 askbot-d03996162cf9438ffee0118e836e39473fed85ea.zip |
askbot_create_text_fixture management command added
-rw-r--r-- | askbot/management/commands/askbot_create_test_fixture.py | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/askbot/management/commands/askbot_create_test_fixture.py b/askbot/management/commands/askbot_create_test_fixture.py new file mode 100644 index 00000000..da818040 --- /dev/null +++ b/askbot/management/commands/askbot_create_test_fixture.py @@ -0,0 +1,60 @@ +from django.core.management.base import NoArgsCommand +from django.core import management +import os +import askbot + + +# FULL PATH HERE +# DEFAULTS TO askbot_path/fixtures/test_data.json +FIXTURE_NAME = os.path.join(os.path.dirname(askbot.__file__), + "tests", "test_data.json") + +# The data from these apps gets auto-populated on signals, saves and updates +# We have to exclude it otherwise we get Constraint errors +EXCLUDE_APPS = ['contenttypes', + 'sites', + 'askbot.activity', + 'askbot.activityauditstatus', + 'askbot.badgedata', + 'auth.Message'] + + +class Command(NoArgsCommand): + """ + Flushes the database, fills it with test data, dumps a fixture and then + flushes the database again. + """ + + def print_if_verbose(self, text): + "Only print if user chooses verbose output" + if self.verbosity > 0: + print text + + + def handle_noargs(self, **options): + self.verbosity = int(options.get("verbosity", 1)) + + # First, flush the data + self.print_if_verbose("FLUSHING THE DATABASE") + management.call_command('flush', verbosity=0, interactive=False) + + # Then, fill the database with test content + self.print_if_verbose("FILLING THE DB WITH TEST CONTENT") + management.call_command('askbot_add_test_content', verbosity=0, + interactive=False) + + # At last, dump the data into a fixture + # Create a file object ready for writing + self.print_if_verbose("DUMPING THE DATA IN FILE '%s'" % FIXTURE_NAME) + fixture = open(FIXTURE_NAME, 'wb') + management.call_command('dumpdata', stdout = fixture, + exclude = EXCLUDE_APPS, indent = 4, natural = True) + fixture.close() + + # FLUSH AGAIN + self.print_if_verbose("FLUSHING THE DATABASE") + management.call_command('flush', verbosity=0, interactive=False) + + self.print_if_verbose("You can load this data now by invoking ./manage.py %s" % FIXTURE_NAME) + + |