blocks.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. from wagtail.blocks import (
  2. CharBlock,
  3. ChoiceBlock,
  4. RichTextBlock,
  5. StreamBlock,
  6. StructBlock,
  7. TextBlock,
  8. )
  9. from wagtail.embeds.blocks import EmbedBlock
  10. from wagtail.images.blocks import ImageChooserBlock
  11. class ImageBlock(StructBlock):
  12. """
  13. Custom `StructBlock` for utilizing images with associated caption and
  14. attribution data
  15. """
  16. image = ImageChooserBlock(required=True)
  17. caption = CharBlock(required=False)
  18. attribution = CharBlock(required=False)
  19. class Meta:
  20. icon = "image"
  21. template = "blocks/image_block.html"
  22. class HeadingBlock(StructBlock):
  23. """
  24. Custom `StructBlock` that allows the user to select h2 - h4 sizes for headers
  25. """
  26. heading_text = CharBlock(classname="title", required=True)
  27. size = ChoiceBlock(
  28. choices=[
  29. ("", "Select a header size"),
  30. ("h2", "H2"),
  31. ("h3", "H3"),
  32. ("h4", "H4"),
  33. ],
  34. blank=True,
  35. required=False,
  36. )
  37. class Meta:
  38. icon = "title"
  39. template = "blocks/heading_block.html"
  40. class BlockQuote(StructBlock):
  41. """
  42. Custom `StructBlock` that allows the user to attribute a quote to the author
  43. """
  44. text = TextBlock()
  45. attribute_name = CharBlock(blank=True, required=False, label="e.g. Mary Berry")
  46. class Meta:
  47. icon = "openquote"
  48. template = "blocks/blockquote.html"
  49. # StreamBlocks
  50. class BaseStreamBlock(StreamBlock):
  51. """
  52. Define the custom blocks that `StreamField` will utilize
  53. """
  54. heading_block = HeadingBlock()
  55. paragraph_block = RichTextBlock(
  56. icon="pilcrow", template="blocks/paragraph_block.html"
  57. )
  58. image_block = ImageBlock()
  59. block_quote = BlockQuote()
  60. embed_block = EmbedBlock(
  61. help_text="Insert an embed URL e.g https://www.youtube.com/watch?v=SGJFWirQ3ks",
  62. icon="media",
  63. template="blocks/embed_block.html",
  64. )