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),
    ])
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
示例#3
0
    def clean(self, value):
        result = []  # build up a list of (name, value) tuples to be passed to the StructValue constructor
        errors = {}
        for name, val in value.items():
            try:
                result.append((name, self.child_blocks[name].clean(val)))
                if name == 'image':
                    if val:
                        if self.max_width:
                            if val.width > self.max_width:
                                errors['image'] = ['Image size exceeds maximum width ({}px)'.format(self.max_width)]
                        if self.max_height:
                            if val.height > self.max_height:
                                errors['image'] = ['Image size exceeds maximum height ({}px)'.format(self.max_height)]
                    else:
                        if self.image_required:
                            errors['image'] = ['This field is required.']

            except ValidationError as e:
                errors[name] = ErrorList([e])

        if errors:
            # The message here is arbitrary - StructBlock.render_form will suppress it
            # and delegate the errors contained in the 'params' dict to the child blocks instead
            raise ValidationError('Validation error in StructBlock', params=errors)

        return StructValue(self, result)
示例#4
0
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[0].block_type == "char_array"
    assert page.body[0].value == ["foo", "bar"]

    assert page.body[1].block_type == "struct"
    assert page.body[1].value == StructValue(
        None,
        [
            ("title", "My Title"),
            (
                "item",
                StructValue(None, [("label", "my-label"), ("value", 100)]),
            ),
            ("items", []),
            ("image", None),
        ],
    )

    assert page.body[2].block_type == "int_array"
    assert page.body[2].value == [100]

    assert page.body[3].block_type == "image"
    assert page.body[3].value == image

    content = str(page.body)
    assert "block-image" in content
示例#5
0
    def test_that_article_should_be_properly_rendered_if_image_has_no_caption(
            self):
        body_block = StreamBlock([("image", CaptionedImageBlock())])
        block_value = StructValue(CaptionedImageBlock,
                                  [("image", ImageFactory()), ("caption", "")])
        body = StreamValue(body_block, [("image", block_value)])
        blog_article_page = self._create_blog_article_page(body=body)

        response = self.client.get(path=blog_article_page.get_absolute_url())

        self.assertEqual(response.status_code, 200)
示例#6
0
    def test_that_image_caption_should_be_displayed_when_provided(self):
        caption_text = "Test caption"
        body_block = StreamBlock([("image", CaptionedImageBlock())])
        block_value = StructValue(CaptionedImageBlock,
                                  [("image", ImageFactory()),
                                   ("caption", caption_text)])
        body = StreamValue(body_block, [("image", block_value)])
        blog_article_page = self._create_blog_article_page(body=body)

        response = self.client.get(path=blog_article_page.get_absolute_url())

        self.assertContains(response, caption_text)
示例#7
0
def test_construct_live_post_block():
    live_post_block = construct_live_post_block(
        message_id="1234",
        created=datetime(1970, 1, 1, 12, 00),
    )
    assert live_post_block == StructValue(
        LivePostBlock(),
        {
            "message_id": "1234",
            "created": datetime(1970, 1, 1, 12, 00),
            "modified": None,
            "show": True,
            "content": StreamValue(ContentBlock(), []),
        },
    )
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),
        ],
    )