content_blocks.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. """
  2. Content blocks are for building complex, nested HTML structures that usually
  3. contain sub-blocks, and may require javascript to function properly.
  4. """
  5. from django.utils.translation import gettext_lazy as _
  6. from wagtail import blocks
  7. from wagtail.documents.blocks import DocumentChooserBlock
  8. from wagtail.images.blocks import ImageChooserBlock
  9. from wagtail.snippets.blocks import SnippetChooserBlock
  10. from .base_blocks import BaseBlock, BaseLayoutBlock, ButtonMixin, CollectionChooserBlock
  11. from .html_blocks import ButtonBlock
  12. class CardBlock(BaseBlock):
  13. """
  14. A component of information with image, text, and buttons.
  15. """
  16. image = ImageChooserBlock(
  17. required=False,
  18. max_length=255,
  19. label=_('Image'),
  20. )
  21. title = blocks.CharBlock(
  22. required=False,
  23. max_length=255,
  24. label=_('Title'),
  25. )
  26. subtitle = blocks.CharBlock(
  27. required=False,
  28. max_length=255,
  29. label=_('Subtitle'),
  30. )
  31. description = blocks.RichTextBlock(
  32. features=['bold', 'italic', 'ol', 'ul', 'hr', 'link', 'document-link'],
  33. label=_('Body'),
  34. )
  35. links = blocks.StreamBlock(
  36. [('Links', ButtonBlock())],
  37. blank=True,
  38. required=False,
  39. label=_('Links'),
  40. )
  41. class Meta:
  42. template = 'wagtailcrx/blocks/card_foot.html'
  43. icon = 'cr-list-alt'
  44. label = _('Card')
  45. class CarouselBlock(BaseBlock):
  46. """
  47. Enables choosing a Carousel snippet.
  48. """
  49. carousel = SnippetChooserBlock('wagtailcrx.Carousel')
  50. class Meta:
  51. icon = 'image'
  52. label = _('Carousel')
  53. template = 'wagtailcrx/blocks/carousel_block.html'
  54. class ImageGalleryBlock(BaseBlock):
  55. """
  56. Show a collection of images with interactive previews that expand to
  57. full size images in a modal.
  58. """
  59. collection = CollectionChooserBlock(
  60. required=True,
  61. label=_('Image Collection'),
  62. )
  63. class Meta:
  64. template = 'wagtailcrx/blocks/image_gallery_block.html'
  65. icon = 'image'
  66. label = _('Image Gallery')
  67. class ModalBlock(ButtonMixin, BaseLayoutBlock):
  68. """
  69. Renders a button that then opens a popup/modal with content.
  70. """
  71. header = blocks.CharBlock(
  72. required=False,
  73. max_length=255,
  74. label=_('Modal heading'),
  75. )
  76. content = blocks.StreamBlock(
  77. [],
  78. label=_('Modal content'),
  79. )
  80. footer = blocks.StreamBlock(
  81. [
  82. ('text', blocks.CharBlock(icon='cr-font', max_length=255, label=_('Simple Text'))), # noqa
  83. ('button', ButtonBlock()),
  84. ],
  85. required=False,
  86. label=_('Modal footer'),
  87. )
  88. class Meta:
  89. template = 'wagtailcrx/blocks/modal_block.html'
  90. icon = 'cr-window-maximize'
  91. label = _('Modal')
  92. class NavBaseLinkBlock(BaseBlock):
  93. display_text = blocks.CharBlock(
  94. required=False,
  95. max_length=255,
  96. label=_('Display text'),
  97. )
  98. image = ImageChooserBlock(
  99. required=False,
  100. label=_('Image'),
  101. )
  102. class NavExternalLinkBlock(NavBaseLinkBlock):
  103. """
  104. External link.
  105. """
  106. link = blocks.CharBlock(
  107. required=False,
  108. label=_('URL'),
  109. )
  110. class Meta:
  111. template = 'wagtailcrx/blocks/external_link_block.html'
  112. label = _('External Link')
  113. class NavPageLinkBlock(NavBaseLinkBlock):
  114. """
  115. Page link.
  116. """
  117. page = blocks.PageChooserBlock(
  118. label=_('Page'),
  119. )
  120. class Meta:
  121. template = 'wagtailcrx/blocks/page_link_block.html'
  122. label = _('Page Link')
  123. class NavDocumentLinkBlock(NavBaseLinkBlock):
  124. """
  125. Document link.
  126. """
  127. document = DocumentChooserBlock(
  128. label=_('Document'),
  129. )
  130. class Meta:
  131. template = 'wagtailcrx/blocks/document_link_block.html'
  132. label = _('Document Link')
  133. class NavSubLinkBlock(BaseBlock):
  134. """
  135. Streamblock for rendering nested sub-links.
  136. """
  137. sub_links = blocks.StreamBlock(
  138. [
  139. ('page_link', NavPageLinkBlock()),
  140. ('external_link', NavExternalLinkBlock()),
  141. ('document_link', NavDocumentLinkBlock()),
  142. ],
  143. required=False,
  144. label=_('Sub-links'),
  145. )
  146. class NavExternalLinkWithSubLinkBlock(NavSubLinkBlock, NavExternalLinkBlock):
  147. """
  148. Extermal link with option for sub-links.
  149. """
  150. class Meta:
  151. label = _('External link with sub-links')
  152. class NavPageLinkWithSubLinkBlock(NavSubLinkBlock, NavPageLinkBlock):
  153. """
  154. Page link with option for sub-links or showing child pages.
  155. """
  156. show_child_links = blocks.BooleanBlock(
  157. required=False,
  158. default=False,
  159. label=_('Show child pages'),
  160. help_text=_('Automatically show a link to the Page’s child pages as a dropdown menu.'),
  161. )
  162. class Meta:
  163. label = _('Page link with sub-links')
  164. class NavDocumentLinkWithSubLinkBlock(NavSubLinkBlock, NavDocumentLinkBlock):
  165. """
  166. Document link with option for sub-links.
  167. """
  168. class Meta:
  169. label = _('Document link with sub-links')
  170. class PriceListItemBlock(BaseBlock):
  171. """
  172. Represents one item in a PriceListBlock, such as an entree in a restaurant menu.
  173. """
  174. image = ImageChooserBlock(
  175. required=False,
  176. label=_('Image'),
  177. )
  178. name = blocks.CharBlock(
  179. required=True,
  180. max_length=255,
  181. label=_('Name'),
  182. )
  183. description = blocks.TextBlock(
  184. required=False,
  185. rows=4,
  186. label=_('Description'),
  187. )
  188. price = blocks.CharBlock(
  189. required=True,
  190. label=_('Price'),
  191. help_text=_('Any text here. Include currency sign if desired.'),
  192. )
  193. class Meta:
  194. template = 'wagtailcrx/blocks/pricelistitem_block.html'
  195. icon = 'cr-usd'
  196. label = _('Price List Item')
  197. class PriceListBlock(BaseBlock):
  198. """
  199. A price list, such as a menu for a restaurant.
  200. """
  201. heading = blocks.CharBlock(
  202. required=False,
  203. max_length=255,
  204. label=_('Heading'),
  205. )
  206. items = blocks.StreamBlock(
  207. [
  208. ('item', PriceListItemBlock()),
  209. ],
  210. label=_('Items'),
  211. )
  212. class Meta:
  213. template = 'wagtailcrx/blocks/pricelist_block.html'
  214. icon = 'cr-usd'
  215. label = _('Price List')
  216. class ContentWallBlock(BaseBlock):
  217. """
  218. Enables choosing a ContentWall snippet.
  219. """
  220. content_wall = SnippetChooserBlock('wagtailcrx.ContentWall')
  221. show_content_wall_on_children = blocks.BooleanBlock(
  222. required=False,
  223. default=False,
  224. verbose_name=_('Show content walls on children pages?'),
  225. help_text=_(
  226. 'If this is checked, the content walls will be displayed on all children pages of this page.') # noqa
  227. )
  228. class Meta:
  229. icon = 'cr-stop'
  230. label = _('Content Wall')
  231. template = 'wagtailcrx/blocks/content_wall_block.html'
  232. class ReusableContentBlock(BaseBlock):
  233. """
  234. Enables choosing a ResusableContent snippet.
  235. """
  236. content = SnippetChooserBlock('wagtailcrx.ReusableContent')
  237. class Meta:
  238. icon = 'cr-recycle'
  239. label = _('Reusable Content')
  240. template = 'wagtailcrx/blocks/reusable_content_block.html'