Skip to content

Instantly share code, notes, and snippets.

@shacker

shacker/stream_blocks.py

Last active Nov 26, 2016
Embed
What would you like to do?
BlockQuoteBlock for use with Wagtail Streamfields
from django import forms
from django.utils.encoding import force_text
from django.utils.html import format_html
from wagtail.wagtailcore.blocks import FieldBlock
class BlockQuoteBlock(FieldBlock):
def __init__(self, required=True, help_text=None, max_length=None, min_length=None, **kwargs):
self.field = forms.CharField(
required=required,
help_text=help_text,
max_length=max_length,
min_length=min_length
)
super(BlockQuoteBlock, self).__init__(**kwargs)
def get_searchable_content(self, value):
return [force_text(value)]
def render_basic(self, value, context=None):
if value:
return format_html('<blockquote>{0}</blockquote>', value)
else:
return ''
class Meta:
icon = "openquote"
# Then in your models.py, e.g::
from cms.stream_blocks import BlockQuoteBlock
...
body = StreamField([
('heading', blocks.CharBlock(classname="full title")),
('paragraph', blocks.RichTextBlock()),
('image', ImageChooserBlock()),
('blockquote', BlockQuoteBlock()),
], blank=True, null=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment