content_blocks.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 ugettext_lazy as _
  6. from wagtail.core import blocks
  7. from wagtail.core.models import Page
  8. from wagtail.documents.blocks import DocumentChooserBlock
  9. from wagtail.images.blocks import ImageChooserBlock
  10. from wagtail.snippets.blocks import SnippetChooserBlock
  11. from .base_blocks import BaseBlock, BaseLayoutBlock, ButtonMixin, CollectionChooserBlock
  12. from .html_blocks import ButtonBlock
  13. class CardBlock(BaseBlock):
  14. """
  15. A component of information with image, text, and buttons.
  16. """
  17. image = ImageChooserBlock(
  18. required=False,
  19. max_length=255,
  20. label=_('Image'),
  21. )
  22. title = blocks.CharBlock(
  23. required=False,
  24. max_length=255,
  25. label=_('Title'),
  26. )
  27. subtitle = blocks.CharBlock(
  28. required=False,
  29. max_length=255,
  30. label=_('Subtitle'),
  31. )
  32. description = blocks.RichTextBlock(
  33. features=['bold', 'italic', 'ol', 'ul', 'hr', 'link', 'document-link'],
  34. label=_('Body'),
  35. )
  36. links = blocks.StreamBlock(
  37. [('Links', ButtonBlock())],
  38. blank=True,
  39. required=False,
  40. label=_('Links'),
  41. )
  42. class Meta:
  43. template = 'coderedcms/blocks/card_foot.html'
  44. icon = 'fa-list-alt'
  45. label = _('Card')
  46. class CarouselBlock(BaseBlock):
  47. """
  48. Enables choosing a Carousel snippet.
  49. """
  50. carousel = SnippetChooserBlock('coderedcms.Carousel')
  51. class Meta:
  52. icon = 'image'
  53. label = _('Carousel')
  54. template = 'coderedcms/blocks/carousel_block.html'
  55. class ImageGalleryBlock(BaseBlock):
  56. """
  57. Show a collection of images with interactive previews that expand to
  58. full size images in a modal.
  59. """
  60. collection = CollectionChooserBlock(
  61. label=_('Image Collection'),
  62. )
  63. class Meta:
  64. template = 'coderedcms/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='fa-file-text-o', max_length=255, label=_('Simple Text'))),
  83. ('button', ButtonBlock()),
  84. ],
  85. required=False,
  86. label=_('Modal footer'),
  87. )
  88. class Meta:
  89. template = 'coderedcms/blocks/modal_block.html'
  90. icon = 'fa-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 = 'coderedcms/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 = 'coderedcms/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 = 'coderedcms/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 PageListBlock(BaseBlock):
  171. """
  172. Renders a preview of selected pages.
  173. """
  174. show_preview = blocks.BooleanBlock(
  175. required=False,
  176. default=False,
  177. label=_('Show body preview'),
  178. )
  179. num_posts = blocks.IntegerBlock(
  180. default=3,
  181. label=_('Number of pages to show'),
  182. )
  183. indexed_by = blocks.PageChooserBlock(
  184. required=False,
  185. label=_('Limit to'),
  186. help_text=_('Only show pages that are children of the selected page. Uses the subpage sorting as specified in the page’s LAYOUT tab.'),
  187. )
  188. class Meta:
  189. template = 'coderedcms/blocks/pagelist_block.html'
  190. icon = 'list-ul'
  191. label = _('Latest Pages')
  192. def get_context(self, value, parent_context=None):
  193. context = super().get_context(value, parent_context=parent_context)
  194. if value['indexed_by']:
  195. indexer = value['indexed_by'].specific
  196. # try to use the CoderedPage `get_index_children()`,
  197. # but fall back to get_children if this is a non-CoderedPage
  198. try:
  199. pages = indexer.get_index_children()
  200. except AttributeError:
  201. pages = indexer.get_children().live()
  202. else:
  203. pages = Page.objects.live().order_by('-first_published_at')
  204. context['pages'] = pages[:value['num_posts']]
  205. return context
  206. class PriceListItemBlock(BaseBlock):
  207. """
  208. Represents one item in a PriceListBlock, such as an entree in a restaurant menu.
  209. """
  210. image = ImageChooserBlock(
  211. required=False,
  212. label=_('Image'),
  213. )
  214. name = blocks.CharBlock(
  215. requred=True,
  216. max_length=255,
  217. label=_('Name'),
  218. )
  219. description = blocks.TextBlock(
  220. required=False,
  221. rows=4,
  222. label=_('Description'),
  223. )
  224. price = blocks.CharBlock(
  225. required=True,
  226. label=_('Price'),
  227. help_text=_('Any text here. Include currency sign if desired.'),
  228. )
  229. class Meta:
  230. template = 'coderedcms/blocks/pricelistitem_block.html'
  231. icon = 'fa-usd'
  232. label = _('Price List Item')
  233. class PriceListBlock(BaseBlock):
  234. """
  235. A price list, such as a menu for a restaurant.
  236. """
  237. heading = blocks.CharBlock(
  238. required=False,
  239. max_length=255,
  240. label=_('Heading'),
  241. )
  242. items = blocks.StreamBlock(
  243. [
  244. ('item', PriceListItemBlock()),
  245. ],
  246. label=_('Items'),
  247. )
  248. class Meta:
  249. template = 'coderedcms/blocks/pricelist_block.html'
  250. icon = 'fa-usd'
  251. label = _('Price List')
  252. class ContentWallBlock(BaseBlock):
  253. """
  254. Enables choosing a ContentWall snippet.
  255. """
  256. content_wall = SnippetChooserBlock('coderedcms.ContentWall')
  257. show_content_wall_on_children = blocks.BooleanBlock(
  258. required=False,
  259. default=False,
  260. verbose_name=_('Show content walls on children pages?'),
  261. help_text=_('If this is checked, the content walls will be displayed on all children pages of this page.')
  262. )
  263. class Meta:
  264. icon = 'fa-stop'
  265. label = _('Content Wall')
  266. template = 'coderedcms/blocks/content_wall_block.html'
  267. class ReusableContentBlock(BaseBlock):
  268. """
  269. Enables choosing a ResusableContent snippet.
  270. """
  271. content = SnippetChooserBlock('coderedcms.ReusableContent')
  272. class Meta:
  273. icon = 'fa-recycle'
  274. label = _('Reusable Content')
  275. template = 'coderedcms/blocks/reusable_content_block.html'