86 lines
2.5 KiB
Python
86 lines
2.5 KiB
Python
import random
|
|
|
|
import factory
|
|
import wagtail_factories
|
|
|
|
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):
|
|
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_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)
|