From c6afb6ce918cc71388df82ea9b51a86cddf6a46f Mon Sep 17 00:00:00 2001 From: Pawel Kowalski Date: Wed, 8 Aug 2018 16:00:05 +0200 Subject: [PATCH] Use real test data where available and mix with generated testdata where missing --- server/core/management/commands/dummy_data.py | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/server/core/management/commands/dummy_data.py b/server/core/management/commands/dummy_data.py index be90883c..dc6dd3db 100644 --- a/server/core/management/commands/dummy_data.py +++ b/server/core/management/commands/dummy_data.py @@ -1,4 +1,5 @@ import os +import random import shutil import wagtail_factories @@ -106,6 +107,9 @@ class Command(BaseCommand): if not os.path.exists(path): os.makedirs(path) + def filter_data(self, input_data, filter_keyword): + return {k: v for (k, v) in input_data.items() if not (k == filter_keyword)} + def handle(self, *args, **options): with connection.cursor() as cursor: cursor.execute("DROP SCHEMA IF EXISTS public CASCADE;") @@ -130,9 +134,17 @@ class Command(BaseCommand): for i in range(0, 4): UserFactory(username='user{}'.format(i)) - book = BookFactory.create(parent=site.root_page) - for idx_topic in range(0, 11): - topic = TopicFactory.create(parent=book) + for book_idx, book_data in enumerate(data): + book = BookFactory.create(parent=site.root_page, **self.filter_data(book_data, 'topics')) - for idc_module in range(0, 5): - module = ModuleFactory.create(parent=topic) + default_topics = [{} for i in range(0, random.randint(5, 8))] + topics_data = book_data.get('topics', default_topics) + + for topic_idx, topic_data in enumerate(topics_data): + topic = TopicFactory.create(parent=book, **self.filter_data(topic_data, 'modules')) + + default_modules = [{} for i in range(0, random.randint(3, 6))] + modules_data = topic_data.get('modules', default_modules) + + for module_idx, module_data in enumerate(modules_data): + module = ModuleFactory.create(parent=topic, **module_data)