blocks.py 1.8 KB

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