blocks.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. from django.utils.functional import cached_property
  2. from wagtail.blocks import (
  3. CharBlock,
  4. ChoiceBlock,
  5. RichTextBlock,
  6. StreamBlock,
  7. StructBlock,
  8. TextBlock,
  9. )
  10. from wagtail.embeds.blocks import EmbedBlock
  11. from wagtail.images import get_image_model
  12. from wagtail.images.blocks import ImageChooserBlock
  13. class CaptionedImageBlock(StructBlock):
  14. """
  15. Custom `StructBlock` for utilizing images with associated caption and
  16. attribution data
  17. """
  18. image = ImageChooserBlock(required=True)
  19. caption = CharBlock(required=False)
  20. attribution = CharBlock(required=False)
  21. @cached_property
  22. def preview_image(self):
  23. # Cache the image object for previews to avoid repeated queries
  24. return get_image_model().objects.last()
  25. def get_preview_value(self):
  26. return {
  27. **self.meta.preview_value,
  28. "image": self.preview_image,
  29. "caption": self.preview_image.description,
  30. }
  31. class Meta:
  32. icon = "image"
  33. template = "blocks/captioned_image_block.html"
  34. preview_value = {"attribution": "The Wagtail Bakery"}
  35. description = "An image with optional caption and attribution"
  36. class HeadingBlock(StructBlock):
  37. """
  38. Custom `StructBlock` that allows the user to select h2 - h4 sizes for headers
  39. """
  40. heading_text = CharBlock(classname="title", required=True)
  41. size = ChoiceBlock(
  42. choices=[
  43. ("", "Select a header size"),
  44. ("h2", "H2"),
  45. ("h3", "H3"),
  46. ("h4", "H4"),
  47. ],
  48. blank=True,
  49. required=False,
  50. )
  51. class Meta:
  52. icon = "title"
  53. template = "blocks/heading_block.html"
  54. preview_value = {"heading_text": "Healthy bread types", "size": "h2"}
  55. description = "A heading with level two, three, or four"
  56. class BlockQuote(StructBlock):
  57. """
  58. Custom `StructBlock` that allows the user to attribute a quote to the author
  59. """
  60. text = TextBlock()
  61. attribute_name = CharBlock(blank=True, required=False, label="e.g. Mary Berry")
  62. class Meta:
  63. icon = "openquote"
  64. template = "blocks/blockquote.html"
  65. preview_value = {
  66. "text": (
  67. "If you read a lot you're well read / "
  68. "If you eat a lot you're well bread."
  69. ),
  70. "attribute_name": "Willie Wagtail",
  71. }
  72. description = "A quote with an optional attribution"
  73. # StreamBlocks
  74. class BaseStreamBlock(StreamBlock):
  75. """
  76. Define the custom blocks that `StreamField` will utilize
  77. """
  78. heading_block = HeadingBlock()
  79. paragraph_block = RichTextBlock(
  80. icon="pilcrow",
  81. template="blocks/paragraph_block.html",
  82. preview_value=(
  83. """
  84. <h2>Our bread pledge</h2>
  85. <p>As a bakery, <b>breads</b> have <i>always</i> been in our hearts.
  86. <a href="https://en.wikipedia.org/wiki/Staple_food">Staple foods</a>
  87. are essential for society, and – bread is the tastiest of all.
  88. We love to transform batters and doughs into baked goods with a firm
  89. dry crust and fluffy center.</p>
  90. """
  91. ),
  92. description="A rich text paragraph",
  93. )
  94. image_block = CaptionedImageBlock()
  95. block_quote = BlockQuote()
  96. embed_block = EmbedBlock(
  97. help_text="Insert an embed URL e.g https://www.youtube.com/watch?v=SGJFWirQ3ks",
  98. icon="media",
  99. template="blocks/embed_block.html",
  100. preview_template="base/preview/static_embed_block.html",
  101. preview_value="https://www.youtube.com/watch?v=mwrGSfiB1Mg",
  102. description="An embedded video or other media",
  103. )