Skip to content

Commit

Permalink
Add handling for StructBlock classes with custom StructValues
Browse files Browse the repository at this point in the history
  • Loading branch information
jams2 committed Feb 19, 2024
1 parent 6109d79 commit e44e8be
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/wagtail_factories/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class Meta:

@classmethod
def _construct_struct_value(cls, block_class, params):
return blocks.StructValue(
return block_class._meta_class.value_class(
block_class(),
list(params.items()),
)
Expand Down
26 changes: 24 additions & 2 deletions tests/test_blocks.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from collections import OrderedDict

import pytest
import wagtail_factories
from wagtail.blocks import StructValue
from wagtail.blocks import CharBlock, StructBlock, StructValue
from wagtail.documents.models import Document
from wagtail.images.models import Image
from wagtail.models import Page
Expand Down Expand Up @@ -250,3 +249,26 @@ def test_chooser_block_strategy(Model, ModelChooserBlockFactory):
# Object is saved in database when the strategy is create
ModelChooserBlockFactory.create()
assert Model.objects.count() == objects_count + 1


def test_custom_struct_value_used():
BAR_DEFAULT = "baz"

class CustomStructValue(StructValue):
def foo(self):
return self["bar"]

class CustomStructBlock(StructBlock):
bar = CharBlock()

class Meta:
value_class = CustomStructValue

class CustomStructBlockFactory(wagtail_factories.StructBlockFactory):
bar = BAR_DEFAULT

class Meta:
model = CustomStructBlock

instance = CustomStructBlockFactory.build()
assert instance.foo() == BAR_DEFAULT

0 comments on commit e44e8be

Please # to comment.