blocks.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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.label,
  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. form_attrs = {
  50. "data-controller": "contextual-alt",
  51. "data-contextual-alt-image-input-value": "[data-contentpath='image'] input[type='hidden']",
  52. "data-contextual-alt-caption-input-value": "[data-contentpath='caption'] input[type='text']",
  53. "data-action": "change->contextual-alt#toggleSuggestTarget",
  54. }
  55. class HeadingBlock(StructBlock):
  56. """
  57. Custom `StructBlock` that allows the user to select h2 - h4 sizes for headers
  58. """
  59. heading_text = CharBlock(classname="title", required=True)
  60. size = ChoiceBlock(
  61. choices=[
  62. ("", "Select a header size"),
  63. ("h2", "H2"),
  64. ("h3", "H3"),
  65. ("h4", "H4"),
  66. ],
  67. blank=True,
  68. required=False,
  69. )
  70. class Meta:
  71. icon = "title"
  72. template = "blocks/heading_block.html"
  73. preview_value = {"heading_text": "Healthy bread types", "size": "h2"}
  74. description = "A heading with level two, three, or four"
  75. class BlockQuote(StructBlock):
  76. """
  77. Custom `StructBlock` that allows the user to attribute a quote to the author
  78. """
  79. text = TextBlock()
  80. attribute_name = CharBlock(blank=True, required=False, label="e.g. Mary Berry")
  81. class Meta:
  82. icon = "openquote"
  83. template = "blocks/blockquote.html"
  84. preview_value = {
  85. "text": (
  86. "If you read a lot you're well read / "
  87. "If you eat a lot you're well bread."
  88. ),
  89. "attribute_name": "Willie Wagtail",
  90. }
  91. description = "A quote with an optional attribution"
  92. # StreamBlocks
  93. class BaseStreamBlock(StreamBlock):
  94. """
  95. Define the custom blocks that `StreamField` will utilize
  96. """
  97. heading_block = HeadingBlock()
  98. paragraph_block = RichTextBlock(
  99. icon="pilcrow",
  100. template="blocks/paragraph_block.html",
  101. preview_value=(
  102. """
  103. <h2>Our bread pledge</h2>
  104. <p>As a bakery, <b>breads</b> have <i>always</i> been in our hearts.
  105. <a href="https://en.wikipedia.org/wiki/Staple_food">Staple foods</a>
  106. are essential for society, and – bread is the tastiest of all.
  107. We love to transform batters and doughs into baked goods with a firm
  108. dry crust and fluffy center.</p>
  109. """
  110. ),
  111. description="A rich text paragraph",
  112. )
  113. image_block = CaptionedImageBlock()
  114. block_quote = BlockQuote()
  115. embed_block = EmbedBlock(
  116. help_text="Insert an embed URL e.g https://www.youtube.com/watch?v=SGJFWirQ3ks",
  117. icon="media",
  118. template="blocks/embed_block.html",
  119. preview_template="base/preview/static_embed_block.html",
  120. preview_value="https://www.youtube.com/watch?v=mwrGSfiB1Mg",
  121. description="An embedded video or other media",
  122. )