def test_custom_page_streamfield_data_complex():
    assert Image.objects.count() == 0

    root_page = wagtail_factories.PageFactory(parent=None)
    page = MyTestPageWithStreamFieldFactory(
        parent=root_page,
        body__0__char_array__0='foo',
        body__0__char_array__1='bar',
        body__2__int_array__0=100,
        body__1__struct__title='My Title',
        body__1__struct__item__value=100,
        body__1__struct__image__image=None,
        body__3__image__image__title='Blub',
    )
    assert Image.objects.count() == 1
    image = Image.objects.first()

    assert page.body.stream_data == [
        ('char_array', ['foo', 'bar']),
        ('struct', StructValue(None, [
            ('title', 'My Title'),
            ('item', StructValue(None, [
                ('label', 'my-label'),
                ('value', 100),
            ])),
            ('items', []),
            ('image', None),
        ])),
        ('int_array', [100]),
        ('image', image),
    ]
    content = str(page.body)
    assert 'block-image' in content
def test_list_block_factory():
    value = MyBlockFactory(
        items__0__label='label-1',
        items__0__value=1,
        items__1__label='label-2',
        items__1__value=2,
        image__image=None)

    assert value == StructValue(None, [
        ('title', 'my title'),
        ('item', OrderedDict([
            ('label', 'my-label'),
            ('value', 100),
        ])),
        ('items', [
            StructValue(None, [
                ('label', 'label-1'),
                ('value', 1),
            ]),
            StructValue(None, [
                ('label', 'label-2'),
                ('value', 2),
            ]),
        ]),
        ('image', None),
    ])
Exemple #3
0
 def remove_empty_documents(value: StructValue) -> list:
     """
     Remove empty documents from a StructValue object. Empty documents
     (actually empty DocumentBlocks) can be created in the CMS by adding new
     document blocks without selecting a document. They can also appear
     because an authorized user deleted a document on the website, which just
     deletes the document object but leaves an empty DocumentBlock in the
     CMS.
     """
     return [doc for doc in value.get('documents', []) if doc is not None]
 def to_python(self, value):
     # This is where the rendition needs to be injected.
     # Blocks used by RenditionStructBlock must have a to_python method
     # that can either accept **kwargs or an extra kwarg named `rendition`
     struct_value_list = []
     for name, child_block in self.child_blocks.items():
         if name in value:
             child_block.rendition = self._rendition_set_config.get(
                 value['render_as'])
             to_append = child_block.to_python(value[name])
         else:
             to_append = child_block.get_default()
         struct_value_list.append((name, to_append))
     return StructValue(self, struct_value_list)
Exemple #5
0
def test_custom_page_streamfield_data_dict():
    assert Image.objects.count() == 0

    root_page = wagtail_factories.PageFactory(parent=None)
    params = OrderedDict({
        'parent':
        root_page,
        'body': [
            {
                'char_array': [
                    'foo',
                    'bar',
                ],
            },
            {
                'struct': {
                    'title': 'My Title',
                    'item': {
                        'value': 100,
                    },
                    'image': {
                        'image': None,
                    },
                },
            },
            {
                'int_array': [
                    100,
                ],
            },
            {
                'image': {
                    'image': {
                        'title': 'Blub',
                    },
                },
            },
        ],
    })

    page = DictToParameteredAttribute(MyTestPageWithStreamFieldFactory,
                                      params=params)

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

    assert page.body.stream_data == [
        ('char_array', ['foo', 'bar']),
        ('struct',
         StructValue(None, [
             ('title', 'My Title'),
             ('item',
              StructValue(None, [
                  ('label', 'my-label'),
                  ('value', 100),
              ])),
             ('items', []),
             ('image', None),
         ])),
        ('int_array', [100]),
        ('image', image),
    ]
    content = str(page.body)
    assert 'block-image' in content
Exemple #6
0
 def clean(self, value):
     # Before saving, remove empty documents (no document selected).
     cleaned_value = super().clean(value)
     return StructValue(
         self, {'documents': self.remove_empty_documents(cleaned_value)})