Skip to content

Commit f7f769a

Browse files
author
Arne Tarara - Automated
committed
Merge remote-tracking branch 'upstream/main' into gmt-pinned-versions
2 parents b2c17f6 + f85f074 commit f7f769a

File tree

10 files changed

+507
-78
lines changed

10 files changed

+507
-78
lines changed

bakerydemo/base/blocks.py

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from django.utils.functional import cached_property
12
from wagtail.blocks import (
23
CharBlock,
34
ChoiceBlock,
@@ -7,10 +8,11 @@
78
TextBlock,
89
)
910
from wagtail.embeds.blocks import EmbedBlock
11+
from wagtail.images import get_image_model
1012
from wagtail.images.blocks import ImageChooserBlock
1113

1214

13-
class ImageBlock(StructBlock):
15+
class CaptionedImageBlock(StructBlock):
1416
"""
1517
Custom `StructBlock` for utilizing images with associated caption and
1618
attribution data
@@ -20,9 +22,23 @@ class ImageBlock(StructBlock):
2022
caption = CharBlock(required=False)
2123
attribution = CharBlock(required=False)
2224

25+
@cached_property
26+
def preview_image(self):
27+
# Cache the image object for previews to avoid repeated queries
28+
return get_image_model().objects.last()
29+
30+
def get_preview_value(self):
31+
return {
32+
**self.meta.preview_value,
33+
"image": self.preview_image,
34+
"caption": self.preview_image.description,
35+
}
36+
2337
class Meta:
2438
icon = "image"
25-
template = "blocks/image_block.html"
39+
template = "blocks/captioned_image_block.html"
40+
preview_value = {"attribution": "The Wagtail Bakery"}
41+
description = "An image with optional caption and attribution"
2642

2743

2844
class HeadingBlock(StructBlock):
@@ -45,6 +61,8 @@ class HeadingBlock(StructBlock):
4561
class Meta:
4662
icon = "title"
4763
template = "blocks/heading_block.html"
64+
preview_value = {"heading_text": "Healthy bread types", "size": "h2"}
65+
description = "A heading with level two, three, or four"
4866

4967

5068
class BlockQuote(StructBlock):
@@ -58,6 +76,14 @@ class BlockQuote(StructBlock):
5876
class Meta:
5977
icon = "openquote"
6078
template = "blocks/blockquote.html"
79+
preview_value = {
80+
"text": (
81+
"If you read a lot you're well read / "
82+
"If you eat a lot you're well bread."
83+
),
84+
"attribute_name": "Willie Wagtail",
85+
}
86+
description = "A quote with an optional attribution"
6187

6288

6389
# StreamBlocks
@@ -68,12 +94,27 @@ class BaseStreamBlock(StreamBlock):
6894

6995
heading_block = HeadingBlock()
7096
paragraph_block = RichTextBlock(
71-
icon="pilcrow", template="blocks/paragraph_block.html"
97+
icon="pilcrow",
98+
template="blocks/paragraph_block.html",
99+
preview_value=(
100+
"""
101+
<h2>Our bread pledge</h2>
102+
<p>As a bakery, <b>breads</b> have <i>always</i> been in our hearts.
103+
<a href="https://en.wikipedia.org/wiki/Staple_food">Staple foods</a>
104+
are essential for society, and – bread is the tastiest of all.
105+
We love to transform batters and doughs into baked goods with a firm
106+
dry crust and fluffy center.</p>
107+
"""
108+
),
109+
description="A rich text paragraph",
72110
)
73-
image_block = ImageBlock()
111+
image_block = CaptionedImageBlock()
74112
block_quote = BlockQuote()
75113
embed_block = EmbedBlock(
76114
help_text="Insert an embed URL e.g https://www.youtube.com/watch?v=SGJFWirQ3ks",
77115
icon="media",
78116
template="blocks/embed_block.html",
117+
preview_template="base/preview/static_embed_block.html",
118+
preview_value="https://www.youtube.com/watch?v=mwrGSfiB1Mg",
119+
description="An embedded video or other media",
79120
)

bakerydemo/base/fixtures/bakerydemo.json

Lines changed: 186 additions & 68 deletions
Large diffs are not rendered by default.
66 KB
Loading

bakerydemo/recipes/blocks.py

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
from wagtail.contrib.table_block.blocks import TableBlock
1212
from wagtail.contrib.typed_table_block.blocks import TypedTableBlock
1313
from wagtail.embeds.blocks import EmbedBlock
14-
from wagtail.images.blocks import ImageChooserBlock
14+
from wagtail.images.blocks import ImageBlock
1515

16-
from bakerydemo.base.blocks import BlockQuote, HeadingBlock, ImageBlock
16+
from bakerydemo.base.blocks import BlockQuote, HeadingBlock
1717

1818

1919
class RecipeStepBlock(StructBlock):
@@ -39,15 +39,69 @@ class RecipeStreamBlock(StreamBlock):
3939
icon="pilcrow", template="blocks/paragraph_block.html", group="Content"
4040
)
4141
block_quote = BlockQuote(group="Content")
42-
table_block = TableBlock(group="Content")
42+
table_block = TableBlock(
43+
group="Content",
44+
description="A table of data with plain text cells",
45+
preview_value={
46+
"first_row_is_table_header": "True",
47+
"data": [
48+
["Bread type", "Origin"],
49+
["Anpan", "Japan"],
50+
["Crumpet", "United Kingdom"],
51+
["Roti buaya", "Indonesia"],
52+
],
53+
},
54+
)
4355
typed_table_block = TypedTableBlock(
4456
[
4557
("text", CharBlock()),
4658
("numeric", FloatBlock()),
4759
("rich_text", RichTextBlock()),
48-
("image", ImageChooserBlock()),
60+
("image", ImageBlock()),
4961
],
5062
group="Content",
63+
description=(
64+
"A table of data with cells that can include "
65+
"text, numbers, rich text, and images"
66+
),
67+
preview_value={
68+
"caption": "Nutritional information for 100g of bread",
69+
"columns": [
70+
{"type": "rich_text", "heading": "Nutrient"},
71+
{"type": "numeric", "heading": "White bread"},
72+
{"type": "numeric", "heading": "Brown bread"},
73+
{"type": "numeric", "heading": "Wholemeal bread"},
74+
],
75+
"rows": [
76+
{
77+
"values": [
78+
'<p><a href="https://en.wikipedia.org/wiki/Protein">'
79+
"Protein</a> <b>(g)</b></p>",
80+
7.9,
81+
7.9,
82+
9.4,
83+
]
84+
},
85+
{
86+
"values": [
87+
'<p><a href="https://en.wikipedia.org/wiki/Carbohydrate">'
88+
"Carbohydrate</a> <b>(g)</b></p>",
89+
46.1,
90+
42.1,
91+
42,
92+
]
93+
},
94+
{
95+
"values": [
96+
'<p><a href="https://en.wikipedia.org/wiki/Sugar">'
97+
"Total sugars</a> <b>(g)</b></p>",
98+
3.4,
99+
3.4,
100+
2.8,
101+
]
102+
},
103+
],
104+
},
51105
)
52106

53107
image_block = ImageBlock(group="Media")
@@ -56,6 +110,8 @@ class RecipeStreamBlock(StreamBlock):
56110
icon="media",
57111
template="blocks/embed_block.html",
58112
group="Media",
113+
preview_value="https://www.youtube.com/watch?v=mwrGSfiB1Mg",
114+
description="An embedded video or other media",
59115
)
60116

61117
ingredients_list = ListBlock(
@@ -64,11 +120,25 @@ class RecipeStreamBlock(StreamBlock):
64120
max_num=10,
65121
icon="list-ol",
66122
group="Cooking",
123+
preview_value=["<p>200g flour</p>", "<p>1 egg</p>", "<p>1 cup of sugar</p>"],
124+
description=(
125+
"A list of ingredients to use in the recipe "
126+
"with optional bold, italic, and link options"
127+
),
67128
)
68129
steps_list = ListBlock(
69130
RecipeStepBlock(),
70131
min_num=2,
71132
max_num=10,
72133
icon="tasks",
73134
group="Cooking",
135+
preview_value=[
136+
{"text": "<p>An easy step</p>", "difficulty": "S"},
137+
{"text": "<p>A difficult step</p>", "difficulty": "L"},
138+
{"text": "<p>A medium step</p>", "difficulty": "M"},
139+
],
140+
description=(
141+
"A list of steps to follow in the recipe, "
142+
"with a difficulty rating for each step"
143+
),
74144
)
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
# Generated by Django 6.0.dev20250116121251 on 2025-02-11 16:44
2+
3+
import wagtail.fields
4+
from django.db import migrations
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
("recipes", "0001_initial"),
11+
]
12+
13+
operations = [
14+
migrations.AlterField(
15+
model_name="recipepage",
16+
name="body",
17+
field=wagtail.fields.StreamField(
18+
[
19+
("heading_block", 2),
20+
("paragraph_block", 3),
21+
("block_quote", 6),
22+
("table_block", 7),
23+
("typed_table_block", 12),
24+
("image_block", 13),
25+
("embed_block", 14),
26+
("ingredients_list", 16),
27+
("steps_list", 19),
28+
],
29+
blank=True,
30+
block_lookup={
31+
0: (
32+
"wagtail.blocks.CharBlock",
33+
(),
34+
{"form_classname": "title", "required": True},
35+
),
36+
1: (
37+
"wagtail.blocks.ChoiceBlock",
38+
[],
39+
{
40+
"blank": True,
41+
"choices": [
42+
("", "Select a header size"),
43+
("h2", "H2"),
44+
("h3", "H3"),
45+
("h4", "H4"),
46+
],
47+
"required": False,
48+
},
49+
),
50+
2: (
51+
"wagtail.blocks.StructBlock",
52+
[[("heading_text", 0), ("size", 1)]],
53+
{"group": "Content"},
54+
),
55+
3: (
56+
"wagtail.blocks.RichTextBlock",
57+
(),
58+
{
59+
"group": "Content",
60+
"icon": "pilcrow",
61+
"template": "blocks/paragraph_block.html",
62+
},
63+
),
64+
4: ("wagtail.blocks.TextBlock", (), {}),
65+
5: (
66+
"wagtail.blocks.CharBlock",
67+
(),
68+
{"blank": True, "label": "e.g. Mary Berry", "required": False},
69+
),
70+
6: (
71+
"wagtail.blocks.StructBlock",
72+
[[("text", 4), ("attribute_name", 5)]],
73+
{"group": "Content"},
74+
),
75+
7: (
76+
"wagtail.contrib.table_block.blocks.TableBlock",
77+
(),
78+
{"group": "Content"},
79+
),
80+
8: ("wagtail.blocks.CharBlock", (), {}),
81+
9: ("wagtail.blocks.FloatBlock", (), {}),
82+
10: ("wagtail.blocks.RichTextBlock", (), {}),
83+
11: ("wagtail.images.blocks.ImageBlock", [], {}),
84+
12: (
85+
"wagtail.contrib.typed_table_block.blocks.TypedTableBlock",
86+
[
87+
[
88+
("text", 8),
89+
("numeric", 9),
90+
("rich_text", 10),
91+
("image", 11),
92+
]
93+
],
94+
{"group": "Content"},
95+
),
96+
13: ("wagtail.images.blocks.ImageBlock", [], {"group": "Media"}),
97+
14: (
98+
"wagtail.embeds.blocks.EmbedBlock",
99+
(),
100+
{
101+
"group": "Media",
102+
"help_text": "Insert an embed URL e.g https://www.youtube.com/watch?v=SGJFWirQ3ks",
103+
"icon": "media",
104+
"template": "blocks/embed_block.html",
105+
},
106+
),
107+
15: (
108+
"wagtail.blocks.RichTextBlock",
109+
(),
110+
{"features": ["bold", "italic", "link"]},
111+
),
112+
16: (
113+
"wagtail.blocks.ListBlock",
114+
(15,),
115+
{
116+
"group": "Cooking",
117+
"icon": "list-ol",
118+
"max_num": 10,
119+
"min_num": 2,
120+
},
121+
),
122+
17: (
123+
"wagtail.blocks.ChoiceBlock",
124+
[],
125+
{"choices": [("S", "Small"), ("M", "Medium"), ("L", "Large")]},
126+
),
127+
18: (
128+
"wagtail.blocks.StructBlock",
129+
[[("text", 15), ("difficulty", 17)]],
130+
{},
131+
),
132+
19: (
133+
"wagtail.blocks.ListBlock",
134+
(18,),
135+
{
136+
"group": "Cooking",
137+
"icon": "tasks",
138+
"max_num": 10,
139+
"min_num": 2,
140+
},
141+
),
142+
},
143+
help_text="The recipe’s step-by-step instructions and any other relevant information.",
144+
),
145+
),
146+
]

bakerydemo/static/css/main.css

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2092,3 +2092,16 @@ input[type='radio'] {
20922092
white-space: nowrap;
20932093
border: 0;
20942094
}
2095+
2096+
/* Block previews */
2097+
2098+
.block-preview {
2099+
padding: 1rem;
2100+
}
2101+
2102+
.static-block-preview {
2103+
width: initial;
2104+
max-width: 100vw;
2105+
max-height: 100vh;
2106+
margin-inline: auto;
2107+
}

0 commit comments

Comments
 (0)