120 lines
4.6 KiB
Python
120 lines
4.6 KiB
Python
import random
|
|
|
|
import factory
|
|
import wagtail_factories
|
|
from factory import CREATE_STRATEGY
|
|
from wagtail.core.rich_text import RichText
|
|
|
|
from book.blocks import BasicKnowledgeBlock, StudentEntryBlock
|
|
from book.models import Book, Topic, Module, Chapter, ContentBlock, TextBlock
|
|
from core.factories import BasePageFactory, fake, DummyImageFactory, fake_paragraph
|
|
|
|
|
|
class BookFactory(BasePageFactory):
|
|
class Meta:
|
|
model = Book
|
|
|
|
|
|
class TopicFactory(BasePageFactory):
|
|
class Meta:
|
|
model = Topic
|
|
|
|
order = 0
|
|
teaser = factory.LazyAttribute(lambda x: fake.sentence(nb_words=random.randint(8, 12)))
|
|
description = factory.LazyAttribute(lambda x: fake.text(max_nb_chars=200))
|
|
|
|
|
|
class ModuleFactory(BasePageFactory):
|
|
class Meta:
|
|
model = Module
|
|
|
|
order = 0
|
|
meta_title = factory.LazyAttribute(lambda x: fake.text(max_nb_chars=20))
|
|
teaser = factory.LazyAttribute(lambda x: fake.sentence(nb_words=random.randint(8, 12)))
|
|
intro = factory.LazyAttribute(lambda x: fake.text(max_nb_chars=200))
|
|
|
|
hero_image = factory.SubFactory(DummyImageFactory)
|
|
|
|
|
|
class ChapterFactory(BasePageFactory):
|
|
class Meta:
|
|
model = Chapter
|
|
|
|
|
|
class TextBlockFactory(wagtail_factories.StructBlockFactory):
|
|
text = factory.LazyAttribute(fake_paragraph)
|
|
|
|
class Meta:
|
|
model = TextBlock
|
|
|
|
|
|
class BasicKnowledgeBlockFactory(wagtail_factories.StructBlockFactory):
|
|
description = factory.LazyAttribute(fake_paragraph)
|
|
url = factory.LazyAttribute(lambda x: fake.uri())
|
|
|
|
class Meta:
|
|
model = BasicKnowledgeBlock
|
|
|
|
|
|
class StudentEntryBlockFactory(wagtail_factories.StructBlockFactory):
|
|
|
|
class Meta:
|
|
model = StudentEntryBlock
|
|
|
|
|
|
block_types = ['text_block', 'basic_knowledge', 'student_entry', 'image_block', 'task']
|
|
|
|
|
|
class ContentBlockFactory(BasePageFactory):
|
|
class Meta:
|
|
model = ContentBlock
|
|
|
|
# parent = factory.LazyAttribute(lambda x: Site.objects.get(is_default_site=True).root_page)
|
|
type = factory.LazyAttribute(lambda x: random.choice(['plain', 'yellow', 'green', 'blue']))
|
|
|
|
contents = wagtail_factories.StreamFieldFactory({
|
|
'text_block': TextBlockFactory,
|
|
'basic_knowledge': BasicKnowledgeBlockFactory,
|
|
'student_entry': StudentEntryBlockFactory,
|
|
'image_block': wagtail_factories.ImageChooserBlockFactory,
|
|
'task': TextBlockFactory
|
|
})
|
|
|
|
@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):
|
|
block_type = resource['type']
|
|
if block_type == 'text_block':
|
|
kwargs['{}__{}__{}__{}'.format(stream_field_name, idx, block_type, field)] = RichText(value[field])
|
|
if block_type == 'task':
|
|
kwargs['{}__{}__{}__{}'.format(stream_field_name, idx, block_type, field)] = RichText(value[field])
|
|
elif block_type == 'image_block':
|
|
kwargs['{}__{}__{}__{}'.format(stream_field_name, idx, block_type, 'image__title')] = fake_paragraph()
|
|
# kwargs['{}__{}__{}__{}'.format(stream_field_name, i, 'image_block', )] = fake_paragraph()
|
|
#
|
|
# image file
|
|
|
|
del kwargs[stream_field_name]
|
|
else:
|
|
for i in range(0, random.randint(3, 7)):
|
|
block_type = random.choice(block_types)
|
|
if block_type == 'text_block':
|
|
kwargs['{}__{}__{}__{}'.format(stream_field_name, i, 'text_block', 'text')] = RichText(fake_paragraph())
|
|
elif block_type == 'basic_knowledge':
|
|
kwargs['{}__{}__{}__{}'.format(stream_field_name, i, 'basic_knowledge', 'description')] = RichText(fake_paragraph())
|
|
# kwargs['{}__{}__{}__{}'.format(stream_field_name, i, 'basic_knowledge', 'description')] = ..url..
|
|
elif block_type == 'student_entry':
|
|
kwargs['{}__{}__{}__{}'.format(stream_field_name, i, 'student_entry', 'task_text')] = RichText(fake_paragraph())
|
|
elif block_type == 'image_block':
|
|
kwargs['{}__{}__{}__{}'.format(stream_field_name, i, 'image_block', 'image__title')] = fake_paragraph()
|
|
elif block_type == 'task':
|
|
kwargs['{}__{}__{}__{}'.format(stream_field_name, i, 'task', 'text')] = RichText(fake_paragraph())
|
|
|
|
@classmethod
|
|
def create(cls, **kwargs):
|
|
cls.stream_field_magic(kwargs, 'contents')
|
|
return cls._generate(CREATE_STRATEGY, kwargs)
|