blocks.py 4.4 KB

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