skillbox/server/core/management/commands/dummy_data.py

57 lines
1.7 KiB
Python

import os
import shutil
import wagtail_factories
from django.conf import settings
from django.core import management
from django.core.management import BaseCommand
from django.db import connection
from wagtail.core.models import Page
from book.factories import BookFactory, TopicFactory, ModuleFactory
from core.factories import UserFactory
data = [
]
class Command(BaseCommand):
def ensure_clean_dir(self, folder):
path = os.path.join(settings.MEDIA_ROOT, folder)
if os.path.exists(path):
shutil.rmtree(path)
if not os.path.exists(path):
os.makedirs(path)
def handle(self, *args, **options):
with connection.cursor() as cursor:
cursor.execute("DROP SCHEMA IF EXISTS public CASCADE;")
cursor.execute(
"CREATE SCHEMA IF NOT EXISTS public AUTHORIZATION {};".format(settings.DATABASES['default']['USER']))
cursor.execute("GRANT ALL ON SCHEMA public TO postgres;")
management.call_command('migrate', verbosity=0, interactive=False)
self.ensure_clean_dir('images')
self.ensure_clean_dir('original_images')
self.ensure_clean_dir('documents')
site = wagtail_factories.SiteFactory.create(is_default_site=True)
Page.objects.filter(title='Root').delete()
u = UserFactory(
username='test',
is_staff=True,
is_superuser=True
)
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 idc_module in range(0, 5):
module = ModuleFactory.create(parent=topic)