def test_serialize(self): block = EmbedBlock(required=False) block_val = EmbedValue('http://www.example.com/foo') serialized_val = block.get_prep_value(block_val) self.assertEqual(serialized_val, 'http://www.example.com/foo') serialized_empty_val = block.get_prep_value(None) self.assertEqual(serialized_empty_val, '')
def test_render_form(self): """ The form field for an EmbedBlock should be a text input containing the URL """ block = EmbedBlock() form_html = block.render_form(EmbedValue('http://www.example.com/foo'), prefix='myembed') self.assertIn('<input ', form_html) self.assertIn('value="http://www.example.com/foo"', form_html)
def test_clean_non_required(self, get_embed): get_embed.return_value = Embed(html='<h1>Hello world!</h1>') block = EmbedBlock(required=False) cleaned_value = block.clean( EmbedValue('https://www.youtube.com/watch?v=_U79Wc965vw')) self.assertIsInstance(cleaned_value, EmbedValue) self.assertEqual(cleaned_value.url, 'https://www.youtube.com/watch?v=_U79Wc965vw') cleaned_value = block.clean(None) self.assertIsNone(cleaned_value)
def test_clean_required(self, get_embed): get_embed.return_value = Embed(html='<h1>Hello world!</h1>') block = EmbedBlock() cleaned_value = block.clean( EmbedValue('https://www.youtube.com/watch?v=_U79Wc965vw')) self.assertIsInstance(cleaned_value, EmbedValue) self.assertEqual(cleaned_value.url, 'https://www.youtube.com/watch?v=_U79Wc965vw') with self.assertRaisesMessage(ValidationError, ''): block.clean(None)
def test_value_from_form(self): """ EmbedBlock should be able to turn a URL submitted as part of a form back into an EmbedValue """ block = EmbedBlock(required=False) block_val = block.value_from_datadict({'myembed': 'http://www.example.com/foo'}, {}, prefix='myembed') self.assertIsInstance(block_val, EmbedValue) self.assertEqual(block_val.url, 'http://www.example.com/foo') # empty value should result in None empty_val = block.value_from_datadict({'myembed': ''}, {}, prefix='myembed') self.assertEqual(empty_val, None)
def test_deserialize(self): """ Deserialising the JSONish value of an EmbedBlock (a URL) should give us an EmbedValue for that URL """ block = EmbedBlock(required=False) block_val = block.to_python('http://www.example.com/foo') self.assertIsInstance(block_val, EmbedValue) self.assertEqual(block_val.url, 'http://www.example.com/foo') # empty values should yield None empty_block_val = block.to_python('') self.assertEqual(empty_block_val, None)
def test_render(self, get_embed): get_embed.return_value = Embed(html='<h1>Hello world!</h1>') block = EmbedBlock() block_val = block.to_python('http://www.example.com/foo') temp = template.Template('embed: {{ embed }}') context = template.Context({'embed': block_val}) result = temp.render(context) # Check that the embed was in the returned HTML self.assertIn('<h1>Hello world!</h1>', result) # Check that get_embed was called correctly get_embed.assert_any_call('http://www.example.com/foo')
class BlogPage(Page): date = models.DateField("Post Date") intro = models.CharField(max_length=250) body = StreamField([ ('heading', blocks.CharBlock(classname="full title", icon="title")), ('paragraph', blocks.RichTextBlock(icon="pilcrow")), ('embed', EmbedBlock(icon="media")), ]) content_panels = Page.content_panels + [ FieldPanel('date'), FieldPanel('intro'), StreamFieldPanel('body'), ]
class PostPage(Page): excerpt = MarkdownField() featured = models.BooleanField(default=False) body = StreamField([ ('image', ImageChooserBlock()), ('html', blocks.RawHTMLBlock()), ('embed', EmbedBlock()), ('paragraph', MarkdownBlock(icon="code")), ]) date = models.DateTimeField(verbose_name="Post date", default=datetime.datetime.today) header_image = models.ForeignKey( 'wagtailimages.Image', null=True, blank=True, on_delete=models.SET_NULL, related_name='+', ) header_image_link = models.CharField(max_length=255, blank=True) categories = ParentalManyToManyField('blog.BlogCategory', blank=True) tags = ClusterTaggableManager(through='blog.BlogPageTag', blank=True) content_panels = Page.content_panels + [ StreamFieldPanel("body"), ImageChooserPanel('header_image'), FieldPanel('header_image_link'), FieldPanel('excerpt'), FieldPanel('categories', widget=forms.CheckboxSelectMultiple), #FieldPanel('tags'), FieldPanel('featured'), ] settings_panels = Page.settings_panels + [ FieldPanel('date'), ] @property def blog_page(self): return self.get_parent().specific def get_context(self, request, *args, **kwargs): context = super(PostPage, self).get_context(request, *args, **kwargs) context['blog_page'] = self.blog_page context['post'] = self return context def get_absolute_url(self): return "/news/%s/" % self.slug
class HomePage(Page): body = StreamField([ ('heading', blocks.CharBlock(classname="full title")), ('paragraph', blocks.RichTextBlock()), ('image', ImageChooserBlock()), ('page', blocks.PageChooserBlock()), ('document', DocumentChooserBlock()), ('media', EmbedBlock()), ('html', blocks.RawHTMLBlock(label='Raw HTML')), ]) content_panels = Page.content_panels + [ StreamFieldPanel('body'), ]
class StoryBlock(blocks.StreamBlock): heading = blocks.CharBlock( classname='full title', icon='title', template='patterns/molecules/streamfield/blocks/heading_block.html') paragraph = blocks.RichTextBlock( features=['h2', 'h3', 'bold', 'italic', 'link', 'ol', 'ul']) image = ImageBlock() quote = QuoteBlock() embed = EmbedBlock() document = DocumentBlock() class Meta: template = "patterns/molecules/streamfield/stream_block.html"
class Poll(models.Model): intro = StreamField([ ('heading', blocks.CharBlock(classname='full title')), ('html', blocks.RawHTMLBlock()), ('image', ImageChooserBlock()), ('paragraph', blocks.RichTextBlock()), ('video', EmbedBlock()), ]) thank_you_text = StreamField([ ('heading', blocks.CharBlock(classname='full title')), ('html', blocks.RawHTMLBlock()), ('image', ImageChooserBlock()), ('paragraph', blocks.RichTextBlock()), ('video', EmbedBlock()), ]) panels = [ # FieldPanel('title', classname='full title'), StreamFieldPanel('intro'), # InlinePanel('form_fields', label='Questions'), StreamFieldPanel('thank_you_text'), ]
class StoryStreamBlock(StreamBlock): subheadline = CharBlock(icon="title", classname="title") embedded_image = EmbeddedImageBlock() h2 = CharBlock(icon="title", classname="title") h3 = CharBlock(icon="title", classname="title") h4 = CharBlock(icon="title", classname="title") intro = RichTextBlock(icon="pilcrow") paragraph = RichTextBlock(icon="pilcrow") classy_paragraph = ClassyParagraph(icon="pilcrow") image = ImageBlock(label="Image", icon="image") aligned_html = AlignedHTMLBlock(icon="code", label='Raw HTML') embed = EmbedBlock(help_text="URL for media to embed") document = DocumentChooserBlock(icon="doc-full-inverse") image_gallery = ImageGalleryBlock(ImageGalleryItemBlock)
class _S_LightBlock(blocks.StructBlock): documentchooserblock = docblocks.DocumentChooserBlock() imagechooserblock = ImageChooserBlock() snippetchooserblock = SnippetChooserBlock(target_model="utils.Button") embedblock = EmbedBlock() staticblock = blocks.StaticBlock() graphql_fields = [ GraphQLDocument("documentchooserblock"), GraphQLImage("imagechooserblock"), GraphQLSnippet("snippetchooserblock", snippet_model="utils.Button"), GraphQLEmbed("embedblock"), GraphQLString("staticblock"), ]
class VideoCardBlock(blocks.StructBlock): title = blocks.CharBlock(required=True, help_text='Add Title') cards = blocks.ListBlock( blocks.StructBlock([ ("title", blocks.CharBlock(required=True, help_text='Add Title')), ("caption", blocks.CharBlock(required=True, help_text='Caption')), ("video", EmbedBlock(label=("Video"))), ])) class Meta: template = "resources/video_card.html" icon = "media" label = "Video Cards"
class ModernPost(BasePage, BlogPost): body = StreamField([ ('heading', blocks.CharBlock(classname='full title')), ('paragraph', blocks.RichTextBlock( features=[ 'ol', 'ul', 'blockquote', 'bold', 'italic', 'superscript', 'subscript', 'strikethrough', 'code', 'link', 'image', 'document-link', ], )), ('image', ImageBlock()), ('embed', EmbedBlock()), ('media', MediaBlock(icon='media')), ('raw_HTML', blocks.RawHTMLBlock(required=False)), ]) content_panels = Page.content_panels + [ StreamFieldPanel('body'), FieldPanel('tags'), FieldPanel('has_comments_enabled'), ] search_fields = Page.search_fields + [ index.SearchField('title'), index.SearchField('body'), ] subpage_types = [] def full_clean(self, *args, **kwargs): """ Override method to provide default slug for micro posts. """ if self.is_micro(): base_slug = dt.datetime.now(dt.timezone.utc).strftime('%Y%m%dT%H%MZ') self.slug = self._get_autogenerated_slug(base_slug) super(ModernPost, self).full_clean(*args, **kwargs) def is_micro(self): return True if 'micro' in self.tags.names() else False def is_snapshot(self): return True if 'snapshot' in self.tags.names() else False
class StoryBlock(blocks.StreamBlock): heading = blocks.CharBlock(classname="full title", icon='title') paragraph = blocks.RichTextBlock() image = ImageBlock() quote = QuoteBlock() embed = EmbedBlock() call_to_action = SnippetChooserBlock( 'utils.CallToActionSnippet', template="blocks/call_to_action_block.html" ) document = DocumentBlock() class Meta: template = "blocks/stream_block.html"
class TestimonialsPage(Page): subpage_types = [] max_count = 1 intro = models.CharField(max_length=150, blank=True) testimonials_video = StreamField([ ('embedded_video', EmbedBlock(icon="media")), ], null=True, blank=True) content_panels = Page.content_panels + [ FieldPanel("intro"), StreamFieldPanel('testimonials_video'), ]
class BaseStreamBlock(StreamBlock): """ Define the custom blocks that `StreamField` will utilize """ heading_block = HeadingBlock() paragraph_block = RichTextBlock(icon="fa-paragraph", template="blocks/paragraph_block.html") image_block = ImageBlock() block_quote = BlockQuote() embed_block = EmbedBlock( help_text= 'Insert an embed URL e.g https://www.youtube.com/embed/SGJFWirQ3ks', icon="fa-s15", template="blocks/embed_block.html")
class CommonVideoBlock(blocks.StructBlock): """ Video block """ video = EmbedBlock( required=True, help_text= _('Paste your video URL ie: https://www.youtube.com/watch?v=05GKqTZGRXU' )) caption = SimpleRichTextBlock(required=False) class Meta: icon = 'media' template = 'commonblocks/video.html'
class EmbedVideoBlock(BaseBlock): """ Embedded media using stock wagtail functionality. """ url = EmbedBlock( required=True, label=_('URL'), help_text=_('Link to a YouTube/Vimeo video, tweet, facebook post, etc.') ) class Meta: template = 'coderedcms/blocks/embed_video_block.html' icon = 'media' label = _('Embed Media')
class Resource(Orderable): page = ParentalKey(ActivityPage, on_delete=models.CASCADE, related_name="resources") content = StreamField([ ("heading", blocks.CharBlock()), ("paragraph", blocks.RichTextBlock()), ("embed", EmbedBlock()), ("image", ImageChooserBlock()), ("document", DocumentChooserBlock()), ]) panels = [ StreamFieldPanel('content'), ]
def __init__(self, *args, **kwargs): if 'default' not in kwargs: kwargs['default'] = None super().__init__([ ('paragraph', RichTextBlock(features=RICH_TEXT_FEATURES)), ('image', ImageChooserBlock()), ('embed', EmbedBlock()), ('embed_html', RawHTMLBlock( help_text='''Warning: be careful what you paste here, since this field could introduce XSS (or similar) bugs. This field is meant solely for third-party embeds.''' )), ('code_snippet', CodeSnippetBlock()), ], **kwargs)
class TwoColumnBlock(StructBlock): background = ChoiceBlock(choices=COLOUR_CHOICES, default="orange") left_column = StreamBlock([ ('heading', CharBlock(classname="full title")), ('paragraph', RichTextBlock()), ('image', ImageChooserBlock()), ('embedded_video', EmbedBlock()), ], icon='arrow-left', label='Left column content') right_column = StreamBlock([ ('heading', CharBlock(classname="full title")), ('paragraph', RichTextBlock()), ('image', ImageChooserBlock()), ('embedded_video', EmbedBlock()), ], icon='arrow-right', label='Right column content') class Meta: template = 'blocks/two_column_block.html' icon = 'placeholder' label = 'Two Columns'
class StoryBlock(blocks.StreamBlock): heading = blocks.CharBlock( classname="full title", icon="title", template="patterns/molecules/streamfield/blocks/heading_block.html", ) paragraph = blocks.RichTextBlock( features=["h2", "h3", "bold", "italic", "link", "ol", "ul"]) image = ImageBlock() quote = QuoteBlock() embed = EmbedBlock() document = DocumentBlock() class Meta: template = "patterns/molecules/streamfield/stream_block.html"
class BaseStreamBlock(StreamBlock): heading_block = HeadingBlock() intro = RichTextBlock(icon="pilcrow") paragraph_block = RichTextBlock(icon="form", template="blocks/block_paragraph.html") aligned_image = BaseImageBlock(label=_('Aligned image'), icon="image") aligned_html = RawHTMLBlock(icon="code", label='Raw HTML') document = DocumentChooserBlock(icon="doc-full-inverse") block_quote = BlockQuote() block_parallax = BaseParallaxBlock() embed_block = EmbedBlock( help_text= 'Insert an embed URL e.g https://www.youtube.com/embed/SGJFWirQ3ks', icon="placeholder", template="blocks/block_embed.html")
class CaptionEmbedBlock(StructBlock): """ Custom `StructBlock` that allows the user to embed and add a caption. """ caption = TextBlock() caption_es = TextBlock() embed = EmbedBlock( help_text= 'Insert an embed URL e.g https://www.youtube.com/embed/SGJFWirQ3ks', icon="fa-s15", template="blocks/embed_block.html") class Meta: icon = "fa-s15" template = 'blocks/caption_embed_block.html'
class BaseStreamBlock(StreamBlock): heading_block = HeadingBlock() paragraph_block = RichTextBlock( icon="fa-paragraph", template="theme/blocks/paragraph_block.html") image_block = ImageBlock() quote_block = QuoteBlock() embed_block = EmbedBlock( help_text= 'Insert an embed URL e.g https://www.youtube.com/embed/SGJFWirQ3ks', icon="fa-s15", template="theme/blocks/embed_block.html") class Meta: icon = "fa-quote-left" template = "theme/blocks/streamfield_block.html"
class BaseStreamBlock(StreamBlock): """ Определите пользовательские блоки, которые StreamField будет использовать """ heading_block = HeadingBlock(label="Заголовок") paragraph_block = RichTextBlock(label="Простой текст", icon="fa-paragraph", template="blocks/paragraph_block.html" ) image_block = ImageBlock(label="Картинка") block_quote = BlockQuote(label="Цитата") embed_block = EmbedBlock( help_text='Insert an embed URL e.g https://www.youtube.com/embed/SGJFWirQ3ks', icon="fa-s15", template="blocks/embed_block.html")
class BaseStreamBlock(StreamBlock): """ Define the custom blocks that `StreamField` will utilize """ heading_block = HeadingBlock() paragraph_block = RichTextBlock(template="cms/blocks/paragraph_block.html") image_block = ImageBlock() block_quote = BlockQuote() cta_block = BlockCTA() column_block = ColumnBlock() featured_block = FeaturedBlock() embed_block = EmbedBlock( help_text= 'Insert an embed URL e.g https://www.youtube.com/embed/SGJFWirQ3ks', template="cms/blocks/embed_block.html")
class ThreeColumnBlock(blocks.StructBlock): title = blocks.CharBlock(required=False, max_length=255) color = blocks.CharBlock(required=True, max_length=255) one_column = blocks.StreamBlock([ ('heading', blocks.CharBlock(classname="full title")), ('paragraph', blocks.RichTextBlock()), ('image', ImageChooserBlock()), ('embedded_video', EmbedBlock()), ], icon='arrow-left', label='One column content') two_column = blocks.StreamBlock([ ('heading', blocks.CharBlock(classname="full title")), ('paragraph', blocks.RichTextBlock()), ('image', ImageChooserBlock()), ('embedded_video', EmbedBlock()), ], icon='arrow-right', label='Two column content') three_column = blocks.StreamBlock([ ('heading', blocks.CharBlock(classname="full title")), ('paragraph', blocks.RichTextBlock()), ('image', ImageChooserBlock()), ('embedded_video', EmbedBlock()), ], icon='arrow-right', label='Three column content') class Meta: template = 'home/three_column_block.html' icon = 'placeholder' label = 'Three Columns'
def test_clean(self): required_block = EmbedBlock() nonrequired_block = EmbedBlock(required=False) # a valid EmbedValue should return the same value on clean cleaned_value = required_block.clean( EmbedValue('http://www.example.com/foo')) self.assertIsInstance(cleaned_value, EmbedValue) self.assertEqual(cleaned_value.url, 'http://www.example.com/foo') cleaned_value = nonrequired_block.clean( EmbedValue('http://www.example.com/foo')) self.assertIsInstance(cleaned_value, EmbedValue) self.assertEqual(cleaned_value.url, 'http://www.example.com/foo') # None should only be accepted for nonrequired blocks cleaned_value = nonrequired_block.clean(None) self.assertEqual(cleaned_value, None) with self.assertRaises(ValidationError): required_block.clean(None)
class VideoBlock(blocks.StructBlock, BlockTupleMixin): video = EmbedBlock( label="Video Embed URL", help_text="Paste the video URL from YouTube or Vimeo. e.g. https://www.youtube.com/watch?v=l3Pz_xQZVDg " "or https://vimeo.com/207076450." ) title = blocks.CharBlock(required=False) fixed_dimensions = DimensionsOptionsBlock() class Meta: label = 'Video w/ Title' template = 'jetstream/blocks/video_block.tpl' form_classname = 'video-block struct-block' icon = 'media'
class CustomEmbedBlock(StructBlock): """ Custom 'StructBlock' that allows you to embed videos """ embed = EmbedBlock() custom_class = TextBlock( required=False, blank=True, help_text="control this element by giving unique class names " "separated by space and styling the class in css") class Meta: icon = "fa-link" template = "blocks/embed_block.html" help_text = ("Insert a youtube URL e.g " "https://www.youtube.com/watch?v=SGJFWirQ3ks")
class ContentBlock(StreamBlock): text = RichTextBlock(icon='fa-paragraph', template='blocks/paragraph_block.html') embed_block = EmbedBlock() alert_block = AlertBlock(required=False) button_block = ButtonBlock(required=False) carousel_block = CarouselBlock() testimonial = TestimonialBlock() quotable_share = QuotableShare() code_block = CodeBlock() full_width_image = FullWidthImageBlock() side_by_side_images = SideBySideImages() class Meta: icon = 'fa fa-scroll' template = 'blocks/content_block.html'
class VideosIndexPage(Page): heading = models.CharField(max_length=65) subheading = models.CharField(max_length=65) introduction = models.TextField( help_text='Text to describe the page', blank=True) body = StreamField([ ('embedded_video', EmbedBlock(icon="media")), ], blank=True) content_panels = Page.content_panels + [ FieldPanel('heading', classname="full"), FieldPanel('subheading', classname="full"), FieldPanel('introduction', classname="full"), StreamFieldPanel('body'), ]
class Body(blocks.StreamBlock): introduction = blocks.RichTextBlock(icon="openquote") heading = blocks.CharBlock(classname='full title', icon="title", template="blocks/heading.html") paragraph = blocks.RichTextBlock() inline_image = CustomImageBlock(icon='image') video = EmbedBlock(icon='media') table = TableBlock(template="blocks/table.html") button = ButtonBlock() iframe = IframeBlock(icon="link") datawrapper = DatawrapperBlock(icon="code") dataviz = DatavizBlock(icon="code") timeline = TimelineBlock(icon="arrows-up-down") google_map = GoogleMapBlock(icon="site") resource_kit = ResourceKit(icon="folder")
def test_clean_invalid_url(self, get_embed): get_embed.side_effect = EmbedNotFoundException non_required_block = EmbedBlock(required=False) with self.assertRaises(ValidationError): non_required_block.clean( EmbedValue('http://no-oembed-here.com/something')) required_block = EmbedBlock() with self.assertRaises(ValidationError): required_block.clean( EmbedValue('http://no-oembed-here.com/something'))
def test_clean(self): required_block = EmbedBlock() nonrequired_block = EmbedBlock(required=False) # a valid EmbedValue should return the same value on clean cleaned_value = required_block.clean(EmbedValue('http://www.example.com/foo')) self.assertIsInstance(cleaned_value, EmbedValue) self.assertEqual(cleaned_value.url, 'http://www.example.com/foo') cleaned_value = nonrequired_block.clean(EmbedValue('http://www.example.com/foo')) self.assertIsInstance(cleaned_value, EmbedValue) self.assertEqual(cleaned_value.url, 'http://www.example.com/foo') # None should only be accepted for nonrequired blocks cleaned_value = nonrequired_block.clean(None) self.assertEqual(cleaned_value, None) with self.assertRaises(ValidationError): required_block.clean(None)
def test_default(self): block1 = EmbedBlock() self.assertEqual(block1.get_default(), None) block2 = EmbedBlock(default='') self.assertEqual(block2.get_default(), None) block3 = EmbedBlock(default=None) self.assertEqual(block3.get_default(), None) block4 = EmbedBlock(default='http://www.example.com/foo') self.assertIsInstance(block4.get_default(), EmbedValue) self.assertEqual(block4.get_default().url, 'http://www.example.com/foo') block5 = EmbedBlock(default=EmbedValue('http://www.example.com/foo')) self.assertIsInstance(block5.get_default(), EmbedValue) self.assertEqual(block5.get_default().url, 'http://www.example.com/foo')