|
@@ -1,3 +1,4 @@
|
|
|
|
+from django.utils.functional import cached_property
|
|
from wagtail.blocks import (
|
|
from wagtail.blocks import (
|
|
CharBlock,
|
|
CharBlock,
|
|
ChoiceBlock,
|
|
ChoiceBlock,
|
|
@@ -7,6 +8,7 @@ from wagtail.blocks import (
|
|
TextBlock,
|
|
TextBlock,
|
|
)
|
|
)
|
|
from wagtail.embeds.blocks import EmbedBlock
|
|
from wagtail.embeds.blocks import EmbedBlock
|
|
|
|
+from wagtail.images import get_image_model
|
|
from wagtail.images.blocks import ImageChooserBlock
|
|
from wagtail.images.blocks import ImageChooserBlock
|
|
|
|
|
|
|
|
|
|
@@ -20,9 +22,23 @@ class ImageBlock(StructBlock):
|
|
caption = CharBlock(required=False)
|
|
caption = CharBlock(required=False)
|
|
attribution = CharBlock(required=False)
|
|
attribution = CharBlock(required=False)
|
|
|
|
|
|
|
|
+ @cached_property
|
|
|
|
+ def preview_image(self):
|
|
|
|
+ # Cache the image object for previews to avoid repeated queries
|
|
|
|
+ return get_image_model().objects.last()
|
|
|
|
+
|
|
|
|
+ def get_preview_value(self):
|
|
|
|
+ return {
|
|
|
|
+ **self.meta.preview_value,
|
|
|
|
+ "image": self.preview_image,
|
|
|
|
+ "caption": self.preview_image.description,
|
|
|
|
+ }
|
|
|
|
+
|
|
class Meta:
|
|
class Meta:
|
|
icon = "image"
|
|
icon = "image"
|
|
template = "blocks/image_block.html"
|
|
template = "blocks/image_block.html"
|
|
|
|
+ preview_value = {"attribution": "The Wagtail Bakery"}
|
|
|
|
+ description = "An image with optional caption and attribution"
|
|
|
|
|
|
|
|
|
|
class HeadingBlock(StructBlock):
|
|
class HeadingBlock(StructBlock):
|
|
@@ -45,6 +61,8 @@ class HeadingBlock(StructBlock):
|
|
class Meta:
|
|
class Meta:
|
|
icon = "title"
|
|
icon = "title"
|
|
template = "blocks/heading_block.html"
|
|
template = "blocks/heading_block.html"
|
|
|
|
+ preview_value = {"heading_text": "Healthy bread types", "size": "h2"}
|
|
|
|
+ description = "A heading with level two, three, or four"
|
|
|
|
|
|
|
|
|
|
class BlockQuote(StructBlock):
|
|
class BlockQuote(StructBlock):
|
|
@@ -58,6 +76,14 @@ class BlockQuote(StructBlock):
|
|
class Meta:
|
|
class Meta:
|
|
icon = "openquote"
|
|
icon = "openquote"
|
|
template = "blocks/blockquote.html"
|
|
template = "blocks/blockquote.html"
|
|
|
|
+ preview_value = {
|
|
|
|
+ "text": (
|
|
|
|
+ "If you read a lot you're well read / "
|
|
|
|
+ "If you eat a lot you're well bread."
|
|
|
|
+ ),
|
|
|
|
+ "attribute_name": "Willie Wagtail",
|
|
|
|
+ }
|
|
|
|
+ description = "A quote with an optional attribution"
|
|
|
|
|
|
|
|
|
|
# StreamBlocks
|
|
# StreamBlocks
|
|
@@ -68,7 +94,19 @@ class BaseStreamBlock(StreamBlock):
|
|
|
|
|
|
heading_block = HeadingBlock()
|
|
heading_block = HeadingBlock()
|
|
paragraph_block = RichTextBlock(
|
|
paragraph_block = RichTextBlock(
|
|
- icon="pilcrow", template="blocks/paragraph_block.html"
|
|
|
|
|
|
+ icon="pilcrow",
|
|
|
|
+ template="blocks/paragraph_block.html",
|
|
|
|
+ preview_value=(
|
|
|
|
+ """
|
|
|
|
+ <h2>Our bread pledge</h2>
|
|
|
|
+ <p>As a bakery, <b>breads</b> have <i>always</i> been in our hearts.
|
|
|
|
+ <a href="https://en.wikipedia.org/wiki/Staple_food">Staple foods</a>
|
|
|
|
+ are essential for society, and – bread is the tastiest of all.
|
|
|
|
+ We love to transform batters and doughs into baked goods with a firm
|
|
|
|
+ dry crust and fluffy center.</p>
|
|
|
|
+ """
|
|
|
|
+ ),
|
|
|
|
+ description="A rich text paragraph",
|
|
)
|
|
)
|
|
image_block = ImageBlock()
|
|
image_block = ImageBlock()
|
|
block_quote = BlockQuote()
|
|
block_quote = BlockQuote()
|
|
@@ -76,4 +114,7 @@ class BaseStreamBlock(StreamBlock):
|
|
help_text="Insert an embed URL e.g https://www.youtube.com/watch?v=SGJFWirQ3ks",
|
|
help_text="Insert an embed URL e.g https://www.youtube.com/watch?v=SGJFWirQ3ks",
|
|
icon="media",
|
|
icon="media",
|
|
template="blocks/embed_block.html",
|
|
template="blocks/embed_block.html",
|
|
|
|
+ preview_template="base/preview/static_embed_block.html",
|
|
|
|
+ preview_value="https://www.youtube.com/watch?v=mwrGSfiB1Mg",
|
|
|
|
+ description="An embedded video or other media",
|
|
)
|
|
)
|