From d03996162cf9438ffee0118e836e39473fed85ea Mon Sep 17 00:00:00 2001 From: Dejan Noveski Date: Mon, 28 Nov 2011 20:23:36 +0100 Subject: askbot_create_text_fixture management command added --- .../commands/askbot_create_test_fixture.py | 60 ++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 askbot/management/commands/askbot_create_test_fixture.py 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) + + -- cgit v1.2.3-1-g7c22 From 62019aef06cdb41deb4ba78a49b7c0b68d2644c6 Mon Sep 17 00:00:00 2001 From: Dejan Noveski Date: Mon, 28 Nov 2011 20:25:08 +0100 Subject: askbot_create_text_fixture management command added; fixed default path --- askbot/management/commands/askbot_create_test_fixture.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/askbot/management/commands/askbot_create_test_fixture.py b/askbot/management/commands/askbot_create_test_fixture.py index da818040..72baa7d2 100644 --- a/askbot/management/commands/askbot_create_test_fixture.py +++ b/askbot/management/commands/askbot_create_test_fixture.py @@ -5,7 +5,7 @@ import askbot # FULL PATH HERE -# DEFAULTS TO askbot_path/fixtures/test_data.json +# DEFAULTS TO askbot_path/tests/test_data.json FIXTURE_NAME = os.path.join(os.path.dirname(askbot.__file__), "tests", "test_data.json") -- cgit v1.2.3-1-g7c22