Skip to content

Commit

Permalink
Modify get_field_blocks to return children of streamblocks
Browse files Browse the repository at this point in the history
  • Loading branch information
virginiacc authored and chosak committed May 10, 2018
1 parent ba6c337 commit 9664dce
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
4 changes: 2 additions & 2 deletions wagtailinventory/helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from itertools import chain

from wagtail.wagtailcore.blocks import ListBlock, StructBlock
from wagtail.wagtailcore.blocks import ListBlock, StreamBlock, StructBlock
from wagtail.wagtailcore.fields import StreamField

from wagtailinventory.models import PageBlock
Expand Down Expand Up @@ -34,7 +34,7 @@ def get_field_blocks(value):
child_blocks = value.bound_blocks.values()
else:
child_blocks = [value.value]
elif isinstance(block, ListBlock):
elif isinstance(block, (ListBlock, StreamBlock)):
child_blocks = value.value
else:
child_blocks = []
Expand Down
27 changes: 26 additions & 1 deletion wagtailinventory/tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

from wagtailinventory.helpers import get_page_blocks
from wagtailinventory.tests.testapp.models import (
MultipleStreamFieldsPage, NoStreamFieldsPage, SingleStreamFieldPage
MultipleStreamFieldsPage, NestedStreamBlockPage, NoStreamFieldsPage,
SingleStreamFieldPage
)


Expand Down Expand Up @@ -121,3 +122,27 @@ def test_multiple_streamfields(self):
'wagtailinventory.tests.testapp.blocks.Organism',
]
)

def test_streamfield_with_child_stream_block(self):
page = self.make_page_with_streamfields(
NestedStreamBlockPage,
content=[
{
'type': 'streamblock',
'value': [
{
'type': 'text',
'value': 'Test content',
},
],
}
]
)

self.assertEqual(
get_page_blocks(page),
[
'wagtail.wagtailcore.blocks.field_block.CharBlock',
'wagtail.wagtailcore.blocks.stream_block.StreamBlock',
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion
import wagtail.wagtailcore.blocks
import wagtail.wagtailcore.fields


class Migration(migrations.Migration):

dependencies = [
('testapp', '0001_initial'),
]

operations = [
migrations.CreateModel(
name='NestedStreamBlockPage',
fields=[
('page_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='wagtailcore.Page')),
('content', wagtail.wagtailcore.fields.StreamField([(b'streamblock', wagtail.wagtailcore.blocks.StreamBlock([(b'text', wagtail.wagtailcore.blocks.CharBlock()), (b'atom', wagtail.wagtailcore.blocks.StructBlock([(b'title', wagtail.wagtailcore.blocks.CharBlock())]))]))])),
],
options={
'abstract': False,
},
bases=('wagtailcore.page',),
),
]
13 changes: 13 additions & 0 deletions wagtailinventory/tests/testapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,16 @@ class MultipleStreamFieldsPage(Page):
FieldPanel('first'),
FieldPanel('second'),
]


class NestedStreamBlockPage(Page):
content = StreamField([
('streamblock', wagtail_blocks.StreamBlock([
('text', wagtail_blocks.CharBlock()),
('atom', blocks.Atom()),
])),
])

content_panels = [
FieldPanel('content'),
]

0 comments on commit 9664dce

Please sign in to comment.