blocks.py 4.7 KB

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