blocks.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. from django import forms
  2. from wagtail.blocks import (
  3. CharBlock,
  4. ChoiceBlock,
  5. FloatBlock,
  6. ListBlock,
  7. RichTextBlock,
  8. StreamBlock,
  9. StructBlock,
  10. )
  11. from wagtail.contrib.table_block.blocks import TableBlock
  12. from wagtail.contrib.typed_table_block.blocks import TypedTableBlock
  13. from wagtail.embeds.blocks import EmbedBlock
  14. from wagtail.images.blocks import ImageBlock
  15. from bakerydemo.base.blocks import BlockQuote, HeadingBlock
  16. class RecipeStepBlock(StructBlock):
  17. text = RichTextBlock(features=["bold", "italic", "link"])
  18. difficulty = ChoiceBlock(
  19. widget=forms.RadioSelect,
  20. choices=[("S", "Small"), ("M", "Medium"), ("L", "Large")],
  21. default="S",
  22. )
  23. class Meta:
  24. template = "blocks/recipe_step_block.html"
  25. icon = "tick"
  26. class RecipeStreamBlock(StreamBlock):
  27. """
  28. Define the custom blocks that `StreamField` will utilize
  29. """
  30. heading_block = HeadingBlock(group="Content")
  31. paragraph_block = RichTextBlock(
  32. icon="pilcrow", template="blocks/paragraph_block.html", group="Content"
  33. )
  34. block_quote = BlockQuote(group="Content")
  35. table_block = TableBlock(
  36. group="Content",
  37. description="A table of data with plain text cells",
  38. preview_value={
  39. "first_row_is_table_header": "True",
  40. "data": [
  41. ["Bread type", "Origin"],
  42. ["Anpan", "Japan"],
  43. ["Crumpet", "United Kingdom"],
  44. ["Roti buaya", "Indonesia"],
  45. ],
  46. },
  47. )
  48. typed_table_block = TypedTableBlock(
  49. [
  50. ("text", CharBlock()),
  51. ("numeric", FloatBlock()),
  52. ("rich_text", RichTextBlock()),
  53. ("image", ImageBlock()),
  54. ],
  55. group="Content",
  56. description=(
  57. "A table of data with cells that can include "
  58. "text, numbers, rich text, and images"
  59. ),
  60. preview_value={
  61. "caption": "Nutritional information for 100g of bread",
  62. "columns": [
  63. {"type": "rich_text", "heading": "Nutrient"},
  64. {"type": "numeric", "heading": "White bread"},
  65. {"type": "numeric", "heading": "Brown bread"},
  66. {"type": "numeric", "heading": "Wholemeal bread"},
  67. ],
  68. "rows": [
  69. {
  70. "values": [
  71. '<p><a href="https://en.wikipedia.org/wiki/Protein">'
  72. "Protein</a> <b>(g)</b></p>",
  73. 7.9,
  74. 7.9,
  75. 9.4,
  76. ]
  77. },
  78. {
  79. "values": [
  80. '<p><a href="https://en.wikipedia.org/wiki/Carbohydrate">'
  81. "Carbohydrate</a> <b>(g)</b></p>",
  82. 46.1,
  83. 42.1,
  84. 42,
  85. ]
  86. },
  87. {
  88. "values": [
  89. '<p><a href="https://en.wikipedia.org/wiki/Sugar">'
  90. "Total sugars</a> <b>(g)</b></p>",
  91. 3.4,
  92. 3.4,
  93. 2.8,
  94. ]
  95. },
  96. ],
  97. },
  98. )
  99. image_block = ImageBlock(group="Media")
  100. embed_block = EmbedBlock(
  101. help_text="Insert an embed URL e.g https://www.youtube.com/watch?v=SGJFWirQ3ks",
  102. icon="media",
  103. template="blocks/embed_block.html",
  104. group="Media",
  105. preview_value="https://www.youtube.com/watch?v=mwrGSfiB1Mg",
  106. description="An embedded video or other media",
  107. )
  108. ingredients_list = ListBlock(
  109. RichTextBlock(features=["bold", "italic", "link"]),
  110. min_num=2,
  111. max_num=10,
  112. icon="list-ol",
  113. group="Cooking",
  114. preview_value=["<p>200g flour</p>", "<p>1 egg</p>", "<p>1 cup of sugar</p>"],
  115. description=(
  116. "A list of ingredients to use in the recipe "
  117. "with optional bold, italic, and link options"
  118. ),
  119. )
  120. steps_list = ListBlock(
  121. RecipeStepBlock(),
  122. min_num=2,
  123. max_num=10,
  124. icon="tasks",
  125. group="Cooking",
  126. preview_value=[
  127. {"text": "<p>An easy step</p>", "difficulty": "S"},
  128. {"text": "<p>A difficult step</p>", "difficulty": "L"},
  129. {"text": "<p>A medium step</p>", "difficulty": "M"},
  130. ],
  131. description=(
  132. "A list of steps to follow in the recipe, "
  133. "with a difficulty rating for each step"
  134. ),
  135. )