Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove debug code #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 40 additions & 17 deletions src/wagtail_factories/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class StreamFieldFactory(ParameteredAttribute):
"""
Syntax:
<streamfield>__<index>__<block_name>__<key>='foo',
or
<streamfield>__<index>__<block_name>='foo',

"""
def __init__(self, factories, **kwargs):
Expand All @@ -39,14 +41,20 @@ def generate(self, step, params):
result = defaultdict(lambda: defaultdict(lambda: defaultdict()))

for key, value in params.items():
try:
index, block_name, param = key.split('__', 2)
except ValueError:
continue
if not index.isdigit():
parts = key.split('__', 2)
index = parts[0]
if index.isdigit():
index = int(index)
else:
continue

index = int(index)
block_name = parts[1]

if len(parts) == 2:
param = 'value'
if len(parts) == 3:
param = parts[2]

result[index][block_name][param] = value

retval = []
Expand All @@ -60,7 +68,6 @@ def generate(self, step, params):

value = block_factory(**block_params)
retval.append((block_name, value))
print("\nRETVAL\n", retval)
return retval


Expand Down Expand Up @@ -98,17 +105,33 @@ def __call__(self, **kwargs):

def generate(self, step, params):
subfactory = self.get_factory()
result = defaultdict(dict)

result = defaultdict(lambda: defaultdict(lambda: defaultdict()))

for key, value in params.items():
index, block_name = key.split('__', 1)
result[int(index)] = (block_name, value)
parts = key.split('__', 2)
index = parts[0]
if index.isdigit():
index = int(index)
else:
continue

_retval = []
for index, (block_name, value) in sorted(result.items()):
value = subfactory(block_name=block_name, value=value)
_retval.append((block_name, value))
block_name = parts[1]

return blocks.StreamValue(subfactory._meta.model(), _retval)
if len(parts) == 2:
param = 'value'
if len(parts) == 3:
param = parts[2]

result[index][block_name][param] = value

retval = []
for index, block_items in sorted(result.items()):
for block_name, block_params in block_items.items():
value = subfactory(block_name=block_name, **block_params)
retval.append((block_name, value))

return blocks.StreamValue(subfactory._meta.model(), retval)


class StreamBlockFactory(factory.Factory):
Expand All @@ -120,8 +143,8 @@ def _build(cls, model_class, block_name, value, *args, **kwargs):
return child_block.to_python(value)

@classmethod
def _create(cls, model_class, *args, **kwargs):
return cls._build(model_class, *args, **kwargs)
def _create(cls, model_class, block_name, value, *args, **kwargs):
return cls._build(model_class, block_name, value, *args, **kwargs)


class BlockFactory(factory.Factory):
Expand Down
13 changes: 10 additions & 3 deletions tests/test_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
from wagtail_factories.wrapper import DictToParameteredAttribute

try:
from wagtail.wagtailcore.blocks import StructValue
from wagtail.wagtailcore.blocks import StructValue, StreamValue
from wagtail.wagtailimages.models import Image
except ImportError:
from wagtail.core.blocks import StructValue
from wagtail.core.blocks import StructValue, StreamValue
from wagtail.images.models import Image

import wagtail_factories
Expand Down Expand Up @@ -113,8 +113,10 @@ def test_custom_page_streamfield_data_complex():
body__1__struct__image__image=None,
body__3__image__image__title='Blub',
body__4__stream__0__title="Stream title",
body__4__stream__1__subtitle="Stream subtitle",
body__4__stream__1__subtitle__value="Stream subtitle",
body__5__char="A char",
)

assert Image.objects.count() == 1
image = Image.objects.first()

Expand All @@ -131,6 +133,11 @@ def test_custom_page_streamfield_data_complex():
])),
('int_array', [100]),
('image', image),
('stream', StreamValue(None, [
('title', 'Stream title'),
('subtitle', 'Stream subtitle'),
])),
('char', 'A char'),
]
content = str(page.body)
assert 'block-image' in content
Expand Down
7 changes: 7 additions & 0 deletions tests/testapp/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,20 @@ class Meta:
model = models.MyStreamBlock


class MyCharBlockFactory(wagtail_factories.CharBlockFactory):

class Meta:
model = models.MyCharBlock


class MyTestPageWithStreamFieldFactory(wagtail_factories.PageFactory):

body = wagtail_factories.StreamFieldFactory({
'char_array': wagtail_factories.ListBlockFactory(
wagtail_factories.CharBlockFactory),
'int_array': wagtail_factories.ListBlockFactory(
wagtail_factories.IntegerBlockFactory),
'char': MyCharBlockFactory,
'struct': MyBlockFactory,
'image': wagtail_factories.ImageChooserBlockFactory,
'stream': wagtail_factories.StreamBlockSubFactory(
Expand Down
5 changes: 5 additions & 0 deletions tests/testapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,16 @@ class MyStreamBlock(blocks.StreamBlock):
subtitle = blocks.CharBlock()


class MyCharBlock(blocks.CharBlock):
pass


class MyTestPage(Page):
body = StreamField([
('char_array', blocks.ListBlock(blocks.CharBlock())),
('int_array', blocks.ListBlock(blocks.IntegerBlock())),
('struct', MyBlock()),
('image', ImageChooserBlock()),
('stream', MyStreamBlock()),
('char', MyCharBlock()),
])