Skip to main content

Factory boy classes for wagtail

Project description

Factory boy classes for Wagtail CMS

Installation

pip install wagtail-factories

Usage

Documentation is still in progress, but see the tests for more examples.

import wagtail_factories
from . import models


class MyCarouselItemFactory(wagtail_factories.StructBlockFactory):
    label = 'my-label'
    image = factory.SubFactory(
        wagtail_factories.ImageChooserBlockFactory)

    class Meta:
        model = models.MyBlockItem


class MyCarouselFactory(wagtail_factories.StructBlockFactory):
    title = "Carousel title"
    items = wagtail_factories.ListBlockFactory(
        MyCarouselItemFactory)

    class Meta:
        model = models.MyCarousel


class MyNewsPageFactory(wagtail_factories.PageFactory):
    class Meta:
        model = models.MyNewsPage


class MyNewsPageChooserBlockFactory(wagtail_factories.PageChooserBlockFactory):
    page = factory.SubFactory(MyNewsPageFactory)


class MyTestPageFactory(wagtail_factories.PageFactory):
    body = wagtail_factories.StreamFieldFactory({
        'carousel': factory.SubFactory(MyCarouselFactory),
        'news_page': factory.SubFactory(MyNewsPageChooserBlockFactory),
    })

    class Meta:
        model = models.MyTestPage


def test_my_page():
    root_page = wagtail_factories.PageFactory(parent=None)
    my_page = MyTestPageFactory(
        parent=root_page,
        body__0__carousel__items__0__label='Slide 1',
        body__0__carousel__items__0__image__image__title='Image Slide 1',
        body__0__carousel__items__1__label='Slide 2',
        body__0__carousel__items__1__image__image__title='Image Slide 2',
        body__0__carousel__items__2__label='Slide 3',
        body__0__carousel__items__2__image__image__title='Image Slide 3',
        body__1__news_page__page__title="News",
    )

Using StreamBlockFactory

StreamBlockFactory can be used in conjunction with the other block factory types to create complex, nested StreamValues, much like how StreamBlock can be used to declare the blocks for a complex StreamField.

First, define your StreamBlockFactory subclass, using factory.SubFactory to wrap child block declarations. Be sure to include your StreamBlock subclass as the model attribute on the inner Meta class.

class MyStreamBlockFactory(wagtail_factories.StreamBlockFactory):
    my_struct_block = factory.SubFactory(MyStructBlockFactory)

    class Meta:
        model = MyStreamBlock

Then include your StreamBlockFactory subclass on a model factory as the argument to a StreamFieldFactory.

class MyPageFactory(wagtail_factories.PageFactory):
    body = wagtail_factories.StreamFieldFactory(MyStreamBlockFactory)

    class Meta:
        model = MyPage

You can then use a modified version of factory_boy’s deep object declaration syntax to build up StreamValues on the fly.

MyPageFactory(
    body__0__my_struct_block__some_field="some value",
    body__0__my_struct_block__some_other_field="some other value",
)

To generate the default value for a block factory, terminate your declaration at the index and provide the block name as the value.

MyPageFactory(body__0="my_struct_block")

Alternative StreamFieldFactory declaration syntax

Prior to version 3.0, StreamFieldFactory could only be used by providing a dict mapping block names to block factory classes as the single argument, for example:

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
            ),
            "struct": MyBlockFactory,
            "image": wagtail_factories.ImageChooserBlockFactory,
        }
    )

    class Meta:
        model = models.MyTestPage

This style of declaration is still supported, with the caveat that nested stream blocks are not supported for this approach. From version 3.0, all BlockFactory values in a StreamFieldFactory definition of this style must be wrapped in factory_boy SubFactories. For example, the above example must be updated to the following for 3.0 compatibility.

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
            ),
            "struct": factory.SubFactory(MyBlockFactory),
            "image": factory.SubFactory(wagtail_factories.ImageChooserBlockFactory),
        }
    )

    class Meta:
        model = models.MyTestPage

This requirement does not apply to ListBlockFactory, which is a subclass of SubFactory.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wagtail_factories-4.1.0.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

wagtail_factories-4.1.0-py2.py3-none-any.whl (10.4 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file wagtail_factories-4.1.0.tar.gz.

File metadata

  • Download URL: wagtail_factories-4.1.0.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for wagtail_factories-4.1.0.tar.gz
Algorithm Hash digest
SHA256 067e3e1a30721a90eaa1514c8214aae7a171490bbedf026eeaa904a2ed9abcd0
MD5 dedc77d4f5b51a2ff0b25f9bec05d215
BLAKE2b-256 26b30635fd66623ed5c0d943d8911a0ea5adff63df2c7ab1cfb1802003ba362c

See more details on using hashes here.

Provenance

File details

Details for the file wagtail_factories-4.1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for wagtail_factories-4.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 6abd435518fdf0cdd3d9920acb2abbd9005936d6fecfdf6fdde3e404834d4b0c
MD5 ff0bf8ba7d2f3f7493c596455bed46c4
BLAKE2b-256 3a2c6c56ccc0c4cef8cbaeda48c13f28300a5cfe6c9c3c4eb6da6dc744ab4297

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page