blocks.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. from wagtail_editable_help.models import HelpText
  12. class ImageBlock(StructBlock):
  13. """
  14. Custom `StructBlock` for utilizing images with associated caption and
  15. attribution data
  16. """
  17. image = ImageChooserBlock(required=True)
  18. caption = CharBlock(required=False)
  19. attribution = CharBlock(required=False)
  20. class Meta:
  21. icon = "image"
  22. template = "blocks/image_block.html"
  23. class HeadingBlock(StructBlock):
  24. """
  25. Custom `StructBlock` that allows the user to select h2 - h4 sizes for headers
  26. """
  27. heading_text = CharBlock(classname="title", required=True)
  28. size = ChoiceBlock(
  29. choices=[
  30. ("", "Select a header size"),
  31. ("h2", "H2"),
  32. ("h3", "H3"),
  33. ("h4", "H4"),
  34. ],
  35. blank=True,
  36. required=False,
  37. )
  38. class Meta:
  39. icon = "title"
  40. template = "blocks/heading_block.html"
  41. class BlockQuote(StructBlock):
  42. """
  43. Custom `StructBlock` that allows the user to attribute a quote to the author
  44. """
  45. text = TextBlock()
  46. attribute_name = CharBlock(blank=True, required=False, label="e.g. Mary Berry")
  47. class Meta:
  48. icon = "fa-quote-left"
  49. template = "blocks/blockquote.html"
  50. # StreamBlocks
  51. class BaseStreamBlock(StreamBlock):
  52. """
  53. Define the custom blocks that `StreamField` will utilize
  54. """
  55. heading_block = HeadingBlock()
  56. paragraph_block = RichTextBlock(
  57. icon="fa-paragraph", template="blocks/paragraph_block.html"
  58. )
  59. image_block = ImageBlock()
  60. block_quote = BlockQuote()
  61. embed_block = EmbedBlock(
  62. help_text=HelpText(
  63. "Common",
  64. "embed block",
  65. default="Insert an embed URL e.g https://www.youtube.com/embed/SGJFWirQ3ks",
  66. ),
  67. icon="fa-s15",
  68. template="blocks/embed_block.html",
  69. )