blocks.py 4.2 KB

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