kopia lustrzana https://github.com/wagtail/wagtail
Require block previews to override the global template if only preview value is provided
rodzic
7055155183
commit
3b8e15561d
|
@ -1,3 +1,5 @@
|
|||
from unittest import mock
|
||||
|
||||
from django.contrib.auth.models import Permission
|
||||
from django.http import HttpRequest
|
||||
from django.test import TestCase
|
||||
|
@ -17,6 +19,17 @@ class TestStreamFieldBlockPreviewView(WagtailTestUtils, TestCase):
|
|||
|
||||
def setUp(self):
|
||||
self.user = self.login()
|
||||
# Pretend the global template has been overridden, since we're happy
|
||||
# with the default preview template not having the static assets.
|
||||
self.template_override_mock = mock.patch(
|
||||
"wagtail.blocks.base.template_is_overridden",
|
||||
return_value=True,
|
||||
)
|
||||
self.template_override_mock.start()
|
||||
|
||||
def tearDown(self):
|
||||
super().tearDown()
|
||||
self.template_override_mock.stop()
|
||||
|
||||
def test_simple(self):
|
||||
block = blocks.CharBlock(
|
||||
|
|
|
@ -20,6 +20,7 @@ from wagtail.admin.staticfiles import versioned_static
|
|||
from wagtail.coreutils import accepts_kwarg
|
||||
from wagtail.telepath import JSContext
|
||||
from wagtail.utils.deprecation import RemovedInWagtail70Warning
|
||||
from wagtail.utils.templates import template_is_overridden
|
||||
|
||||
__all__ = [
|
||||
"BaseBlock",
|
||||
|
@ -297,20 +298,27 @@ class Block(metaclass=BaseBlock):
|
|||
def is_previewable(self):
|
||||
# To prevent showing a broken preview if the block preview has not been
|
||||
# configured, consider the block to be previewable if either:
|
||||
# - a preview template, preview value, or default value is defined
|
||||
# - a preview method is overridden
|
||||
# - a specific preview template is configured for the block
|
||||
# - a preview value is provided and the global template has been overridden
|
||||
# which are the intended ways to configure block previews.
|
||||
#
|
||||
# If a block is made previewable by other means, the `is_previewable`
|
||||
# property should be overridden to return `True`.
|
||||
return (
|
||||
has_specific_template = (
|
||||
hasattr(self.meta, "preview_template")
|
||||
or hasattr(self.meta, "preview_value")
|
||||
or self.__class__.get_preview_template is not Block.get_preview_template
|
||||
)
|
||||
has_preview_value = (
|
||||
hasattr(self.meta, "preview_value")
|
||||
or getattr(self.meta, "default", None) is not None
|
||||
or self.__class__.get_preview_context is not Block.get_preview_context
|
||||
or self.__class__.get_preview_template is not Block.get_preview_template
|
||||
or self.__class__.get_preview_value is not Block.get_preview_value
|
||||
)
|
||||
has_global_template = template_is_overridden(
|
||||
"wagtailcore/shared/block_preview.html",
|
||||
"templates",
|
||||
)
|
||||
return has_specific_template or (has_preview_value and has_global_template)
|
||||
|
||||
def get_description(self):
|
||||
return getattr(self.meta, "description", "")
|
||||
|
|
|
@ -3,6 +3,7 @@ import collections
|
|||
import copy
|
||||
import json
|
||||
import unittest
|
||||
import unittest.mock
|
||||
from decimal import Decimal
|
||||
|
||||
# non-standard import name for gettext_lazy, to prevent strings from being picked up for translation
|
||||
|
@ -75,18 +76,70 @@ class TestBlock(SimpleTestCase):
|
|||
def get_preview_value(self):
|
||||
return "foo"
|
||||
|
||||
variants = {
|
||||
"no_config": [
|
||||
blocks.Block(),
|
||||
],
|
||||
"specific_template": [
|
||||
blocks.Block(preview_template="foo.html"),
|
||||
CustomTemplateBlock(),
|
||||
],
|
||||
"custom_value": [
|
||||
blocks.Block(preview_value="foo"),
|
||||
blocks.Block(default="bar"),
|
||||
CustomContextBlock(),
|
||||
CustomValueBlock(),
|
||||
],
|
||||
"specific_template_and_custom_value": [
|
||||
blocks.Block(preview_template="foo.html", preview_value="bar"),
|
||||
],
|
||||
}
|
||||
|
||||
# Test without a global template override
|
||||
cases = [
|
||||
(blocks.Block(), False),
|
||||
(blocks.Block(preview_template="foo.html"), True),
|
||||
(blocks.Block(preview_value="foo"), True),
|
||||
(blocks.Block(default="bar"), True),
|
||||
(CustomContextBlock(), True),
|
||||
(CustomTemplateBlock(), True),
|
||||
(CustomValueBlock(), True),
|
||||
# Unconfigured block should not be previewable
|
||||
("no_config", False),
|
||||
# Providing a specific preview template should make the block
|
||||
# previewable even without a custom preview value, as the content
|
||||
# may be hardcoded in the template
|
||||
("specific_template", True),
|
||||
# Providing a preview value without a custom template should not
|
||||
# make the block previewable, as it may be missing the static assets
|
||||
("custom_value", False),
|
||||
# Providing both a preview template and value also makes the block
|
||||
# previewable, this is the same as providing a custom template only
|
||||
("specific_template_and_custom_value", True),
|
||||
]
|
||||
for block, is_previewable in cases:
|
||||
with self.subTest(block=block):
|
||||
self.assertEqual(block.is_previewable, is_previewable)
|
||||
for variant, is_previewable in cases:
|
||||
with self.subTest(variant=variant, custom_global_template=False):
|
||||
for block in variants[variant]:
|
||||
self.assertIs(block.is_previewable, is_previewable)
|
||||
|
||||
# Test with a global template override
|
||||
with unittest.mock.patch(
|
||||
"wagtail.blocks.base.template_is_overridden",
|
||||
return_value=True,
|
||||
):
|
||||
cases = [
|
||||
# Global template override + no preview value = not previewable,
|
||||
# since it's unlikely the global template alone will provide a
|
||||
# useful preview
|
||||
("no_config", False),
|
||||
# Unchanged – specific template always makes the block previewable
|
||||
("specific_template", True),
|
||||
# Global template override + custom preview value = previewable.
|
||||
# We assume the global template will provide the static assets,
|
||||
# and the custom value (and the block's real template via
|
||||
# {% include_block %}) will provide the content.
|
||||
("custom_value", True),
|
||||
# Unchanged – providing both also makes the block previewable
|
||||
("specific_template_and_custom_value", True),
|
||||
]
|
||||
for variant, is_previewable in cases:
|
||||
with self.subTest(variant=variant, custom_global_template=True):
|
||||
for block in variants[variant]:
|
||||
del block.is_previewable # Clear cached_property
|
||||
self.assertIs(block.is_previewable, is_previewable)
|
||||
|
||||
|
||||
class TestFieldBlock(WagtailTestUtils, SimpleTestCase):
|
||||
|
|
Ładowanie…
Reference in New Issue