import os import random import shutil from django.conf import settings from django.core.management import BaseCommand from rooms.factories import RoomFactory, RoomEntryFactory from rooms.models import Room data = [ { 'title': 'Ein historisches Festival', 'description': 'Ein historisches Festival', 'appearance': 'red', 'entries': [ { 'title': 'Ein neues Festival auf dem Gurten – und ich bin dabei!', 'contents': [ { 'type': 'image_url_block', 'value': { 'title': 'Ein Bild sagt mehr als 1000 Worte', 'url': 'https://picsum.photos/600/400/?random' } }, { 'type': 'text_block', 'value': { 'text': """
Das folgende Interview bezieht sich auf Jugendliche, die Ihre Lehre im Sommer begonnen haben. Lesen Sie das Interview durch und bearbeiten Sie anschliessend die Aufgaben.
Aufgaben zum Interview «Das ist ein ganz markanter Wechsel»
""" } }, { 'type': 'text_block', 'value': { 'text': 'Erklären Sie, welche Informationen den Leserinnen und Lesern in der Einleitung vermittelt werden.
' } } ] }, { 'title': 'Mein Tagesblog', 'contents': [ { 'type': 'image_url_block', 'value': { 'title': 'Ein Bild sagt mehr als 1000 Worte', 'url': 'https://picsum.photos/600/400/?random' } }, { 'type': 'text_block', 'value': { 'text': """Das folgende Interview bezieht sich auf Jugendliche, die Ihre Lehre im Sommer begonnen haben. Lesen Sie das Interview durch und bearbeiten Sie anschliessend die Aufgaben.
Aufgaben zum Interview «Das ist ein ganz markanter Wechsel»
""" } }, { 'type': 'text_block', 'value': { 'text': 'Erklären Sie, welche Informationen den Leserinnen und Lesern in der Einleitung vermittelt werden.
' } } ] }, { 'title': 'Woodstock', 'contents': [ { 'type': 'image_url_block', 'value': { 'title': 'Ein Bild sagt mehr als 1000 Worte', 'url': 'https://picsum.photos/600/400/?random' } }, { 'type': 'link_block', 'value': { 'text': 'Netflix & Chill', 'url': 'https://netflix.com' } } ] }, { 'title': 'Das Festival', 'contents': [ { 'type': 'image_url_block', 'value': { 'title': 'Ein Bild sagt mehr als 1000 Worte', 'url': 'https://picsum.photos/600/400/?random' } }, { 'type': 'text_block', 'value': { 'text': """Das folgende Interview bezieht sich auf Jugendliche, die Ihre Lehre im Sommer begonnen haben. Lesen Sie das Interview durch und bearbeiten Sie anschliessend die Aufgaben.
Aufgaben zum Interview «Das ist ein ganz markanter Wechsel»
""" } }, { 'type': 'text_block', 'value': { 'text': 'Erklären Sie, welche Informationen den Leserinnen und Lesern in der Einleitung vermittelt werden.
' } } ] } ] }, { 'title': 'Erfahrungen Lehrbeginn', 'appearance': 'green', 'description': 'Eine Beschreibung', 'entries': [ { 'title': 'Ich heisse Jan und habe am 01. August 2017 meine Ausbildung begonnen. Pünktlich zum …', 'contents': [ { 'type': 'image_url_block', 'value': { 'title': 'Ein Bild sagt mehr als 1000 Worte', 'url': 'https://picsum.photos/600/400/?random' } }, { 'type': 'text_block', 'value': { 'text': """Das folgende Interview bezieht sich auf Jugendliche, die Ihre Lehre im Sommer begonnen haben. Lesen Sie das Interview durch und bearbeiten Sie anschliessend die Aufgaben.
Aufgaben zum Interview «Das ist ein ganz markanter Wechsel»
""" } }, { 'type': 'text_block', 'value': { 'text': 'Erklären Sie, welche Informationen den Leserinnen und Lesern in der Einleitung vermittelt werden.
' } } ] }, { 'title': 'Mein Lehrbeginn', 'contents': [ { 'type': 'image_url_block', 'value': { 'title': 'Ein Bild sagt mehr als 1000 Worte', 'url': 'https://picsum.photos/600/400/?random' } }, { 'type': 'text_block', 'value': { 'text': """Das folgende Interview bezieht sich auf Jugendliche, die Ihre Lehre im Sommer begonnen haben. Lesen Sie das Interview durch und bearbeiten Sie anschliessend die Aufgaben.
Aufgaben zum Interview «Das ist ein ganz markanter Wechsel»
""" } }, { 'type': 'text_block', 'value': { 'text': 'Erklären Sie, welche Informationen den Leserinnen und Lesern in der Einleitung vermittelt werden.
' } } ] } ] }, { 'title': 'Interview «Mein neues Umfeld»', 'description': 'Interview «Mein neues Umfeld»', 'appearance': 'blue' } ] 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 filter_data(self, input_data, filter_keyword): filters = [filter_keyword] if not isinstance(filter_keyword, list) else filter_keyword return {k: v for (k, v) in input_data.items() if not (k in filters)} def handle(self, *args, **options): Room.objects.all().delete() # root_page = Site.objects.get(is_default_site=True, port=80).root_page for room_idx, room_data in enumerate(data): room = RoomFactory.create(**self.filter_data(room_data, ['entries'])) default_room_entries = [{} for _ in range(0, random.randint(5, 8))] room_entries = room_data.get('entries', default_room_entries) for room_entry_idx, room_entry_data in enumerate(room_entries): room_entry = RoomEntryFactory.create(room=room, **room_entry_data) # room_entry = RoomEntryFactory.create(room=room, **self.filter_data(room_entry_data, 'contents')) # if 'contents' in room_entry_data: # # now create contents in a room entry # content_block = ContentBlockFactory.create( # parent=root_page, # title='{}_{}'.format(room_entry.title, fake_title(min_words=1, max_words=1)), # # description=room_entry.description, # contents=room_entry_data['contents'] # ) # room_entry.content = content_block # room_entry.save() # for content_block_idx, content_block_data in enumerate(content_blocks_data): # # ContentBlockFactory.create(parent=chapter, **self.filter_data(content_block_data, 'contents')) # ContentBlockFactory.create(parent=chapter, **content_block_data)