Add ContentBlockFactories and test data
This commit is contained in:
parent
80b46a4781
commit
051796a34f
|
|
@ -1,9 +1,13 @@
|
|||
import random
|
||||
|
||||
import factory
|
||||
import wagtail_factories
|
||||
|
||||
from book.models import Book, Topic, Module, Chapter, ContentBlock
|
||||
from core.factories import BasePageFactory, fake, DummyImageFactory
|
||||
from factory import CREATE_STRATEGY
|
||||
|
||||
from book.blocks import DocumentBlock, ModalTextBlock
|
||||
from book.models import Book, Topic, Module, Chapter, ContentBlock, TextBlock
|
||||
from core.factories import BasePageFactory, fake, DummyImageFactory, fake_title
|
||||
|
||||
|
||||
class BookFactory(BasePageFactory):
|
||||
|
|
@ -37,8 +41,45 @@ class ChapterFactory(BasePageFactory):
|
|||
model = Chapter
|
||||
|
||||
|
||||
class TextBlockFactory(wagtail_factories.StructBlockFactory):
|
||||
text = factory.LazyAttribute(fake_title)
|
||||
|
||||
class Meta:
|
||||
model = TextBlock
|
||||
|
||||
|
||||
class ModalTextBlockFactory(wagtail_factories.StructBlockFactory):
|
||||
description = factory.LazyAttribute(fake_title)
|
||||
modal_content = factory.LazyAttribute(fake_title)
|
||||
|
||||
class Meta:
|
||||
model = ModalTextBlock
|
||||
|
||||
|
||||
class ContentBlockFactory(BasePageFactory):
|
||||
class Meta:
|
||||
model = ContentBlock
|
||||
|
||||
type = factory.LazyAttribute(lambda x: random.choice(['plain', 'yellow', 'green', 'blue']))
|
||||
|
||||
contents = wagtail_factories.StreamFieldFactory({
|
||||
'text_block': TextBlockFactory,
|
||||
'modal_text': ModalTextBlockFactory,
|
||||
})
|
||||
|
||||
@classmethod
|
||||
def stream_field_magic(cls, kwargs, stream_field_name):
|
||||
if stream_field_name in kwargs:
|
||||
for idx, resource in enumerate(kwargs[stream_field_name]):
|
||||
value = resource['value']
|
||||
for jdx, field in enumerate(value):
|
||||
kwargs['{}__{}__{}__{}'.format(stream_field_name, idx, resource['type'], field)] = value[field]
|
||||
del kwargs[stream_field_name]
|
||||
# else:
|
||||
# for i in range(0, random.randint(3, 7)):
|
||||
# kwargs['{}__{}__{}__b'.format(stream_field_name, i, random.choice(['link', 'document']))] = None
|
||||
|
||||
@classmethod
|
||||
def create(cls, **kwargs):
|
||||
cls.stream_field_magic(kwargs, 'contents')
|
||||
return cls._generate(CREATE_STRATEGY, kwargs)
|
||||
|
|
|
|||
|
|
@ -56,17 +56,72 @@ data = [
|
|||
{
|
||||
'title': '1.1 Lehrbeginn',
|
||||
'content_blocks': [
|
||||
{'type': 'plain', 'title': 'Auftrag 1', 'content': 'Sie haben diesen Sommer ihre Lehre begonnen. Was bedeutet dieser neue Abschnitt für Sie?'},
|
||||
{'type': 'yellow', 'title': 'Auftrag 2', 'content': 'Sie haben diesen Sommer ihre Lehre begonnen. Was bedeutet dieser neue Abschnitt für Sie?'},
|
||||
{'type': 'plain', 'title': 'Auftrag 3', 'content': 'Sie haben diesen Sommer ihre Lehre begonnen. Was bedeutet dieser neue Abschnitt für Sie?'}
|
||||
{
|
||||
'type': 'plain',
|
||||
'title': 'Auftrag 1',
|
||||
'contents': [
|
||||
# {
|
||||
# 'text_block': 'text_block',
|
||||
# 'value': 'value',
|
||||
# 'id-123': 'id-123'
|
||||
# },
|
||||
{
|
||||
'type': 'text_block',
|
||||
'value': {
|
||||
'type': 'text_block',
|
||||
'text': 'Sie haben diesen Sommer ihre Lehre begonnen. Was bedeutet dieser neue Abschnitt für Sie?'
|
||||
}
|
||||
},
|
||||
# {
|
||||
# 'type': 'text_block',
|
||||
# 'value': {
|
||||
# 'text': 'Trello: Task-Management',
|
||||
# }
|
||||
# },
|
||||
# {
|
||||
# 'type': 'text_block',
|
||||
# 'value': {
|
||||
# 'text': 'Trello: Task-Management',
|
||||
# }
|
||||
# },
|
||||
# {
|
||||
# 'type': 'text_block',
|
||||
# 'value': {
|
||||
# 'text': 'Trello: Task-Management',
|
||||
# }
|
||||
# }
|
||||
]
|
||||
},
|
||||
{
|
||||
'type': 'yellow',
|
||||
'title': 'Auftrag 2',
|
||||
# 'content': 'Sie haben diesen Sommer ihre Lehre begonnen. Was bedeutet dieser neue Abschnitt für Sie?'
|
||||
},
|
||||
{
|
||||
'type': 'plain',
|
||||
'title': 'Auftrag 3',
|
||||
# 'content': 'Sie haben diesen Sommer ihre Lehre begonnen. Was bedeutet dieser neue Abschnitt für Sie?'
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
'title': '1.2 Die drei Lernorte',
|
||||
'content_blocks': [
|
||||
{'type': 'plain', 'title': 'Auftrag 1', 'content': 'Sie haben diesen Sommer ihre Lehre begonnen. Was bedeutet dieser neue Abschnitt für Sie?'},
|
||||
{'type': 'yellow', 'title': 'Auftrag 2', 'content': 'Sie haben diesen Sommer ihre Lehre begonnen. Was bedeutet dieser neue Abschnitt für Sie?'},
|
||||
{'type': 'blue', 'title': 'Auftrag 3', 'content': 'Sie haben diesen Sommer ihre Lehre begonnen. Was bedeutet dieser neue Abschnitt für Sie?'}
|
||||
{
|
||||
'type': 'plain',
|
||||
'title': 'Auftrag 1',
|
||||
# 'content': 'Sie haben diesen Sommer ihre Lehre begonnen. Was bedeutet dieser neue Abschnitt für Sie?'
|
||||
},
|
||||
{
|
||||
'type': 'yellow',
|
||||
'title': 'Auftrag 2',
|
||||
# 'content': 'Sie haben diesen Sommer ihre Lehre begonnen. Was bedeutet dieser neue Abschnitt für Sie?'
|
||||
},
|
||||
{
|
||||
'type': 'blue',
|
||||
'title': 'Auftrag 3',
|
||||
# 'content': 'Sie haben diesen Sommer ihre Lehre begonnen. Was bedeutet dieser neue Abschnitt für Sie?'
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
|
|
@ -219,4 +274,5 @@ class Command(BaseCommand):
|
|||
content_blocks_data = chapter_data.get('content_blocks', default_content_blocks)
|
||||
|
||||
for content_block_idx, content_block_data in enumerate(content_blocks_data):
|
||||
ContentBlockFactory.create(parent=chapter, **self.filter_data(content_block_data, 'content'))
|
||||
# ContentBlockFactory.create(parent=chapter, **self.filter_data(content_block_data, 'contents'))
|
||||
ContentBlockFactory.create(parent=chapter, **content_block_data)
|
||||
|
|
|
|||
Loading…
Reference in New Issue