def setUp(self): # test format self.format = Format('test name', 'test label', 'test classnames', 'test filter spec') # test image self.image = MagicMock() self.image.id = 0
def setUp(self): # test format self.format = Format("test name", "test label", "test classnames", "original") # test image self.image = Image.objects.create( title="Test image", file=get_test_image_file(), )
def setUp(self): # test format self.format = Format('test name', 'test label', 'test classnames', 'original') # test image self.image = Image.objects.create( title="Test image", file=get_test_image_file(), )
class TestFormat(TestCase): def setUp(self): # test format self.format = Format('test name', 'test label', 'test classnames', 'test filter spec') # test image self.image = MagicMock() self.image.id = 0 def test_editor_attributes(self): result = self.format.editor_attributes(self.image, 'test alt text') self.assertEqual( result, 'data-embedtype="image" data-id="0" data-format="test name" data-alt="test alt text" ' ) def test_image_to_editor_html(self): result = self.format.image_to_editor_html(self.image, 'test alt text') self.assertRegex( result, '<img data-embedtype="image" data-id="0" data-format="test name" ' 'data-alt="test alt text" class="test classnames" src="[^"]+" width="1" height="1" alt="test alt text">', ) def test_image_to_editor_html_with_quoting(self): result = self.format.image_to_editor_html( self.image, 'Arthur "two sheds" Jackson') self.assertRegex( result, '<img data-embedtype="image" data-id="0" data-format="test name" ' 'data-alt="Arthur "two sheds" Jackson" class="test classnames" src="[^"]+" width="1" height="1" alt="Arthur "two sheds" Jackson">', ) def test_image_to_html_no_classnames(self): self.format.classnames = None result = self.format.image_to_html(self.image, 'test alt text') self.assertRegex( result, '<img src="[^"]+" width="1" height="1" alt="test alt text">') self.format.classnames = 'test classnames' def test_image_to_html_with_quoting(self): result = self.format.image_to_html(self.image, 'Arthur "two sheds" Jackson') self.assertRegex( result, '<img class="test classnames" src="[^"]+" width="1" height="1" alt="Arthur "two sheds" Jackson">' ) def test_get_image_format(self): register_image_format(self.format) result = get_image_format('test name') self.assertEqual(result, self.format)
def image_embedtype_handler(attrs, images=[]): """ Reimplementation of wagtail.images.rich_text.image_embedtype_handler (v2) Turns an <embed>'s attributes (attrs) into a valid <img> tag. Uses an in-memory list of Images (images) to try and find the image locally before querying the database. """ Image = get_image_model() # noqa: N806 image_id = attrs.get('id', '') alt_text = attrs.get('alt', '') width = attrs.get('width', '') # Handle the case where the embed has a blank or missing 'id' attribute # eg. <embed embedtype="news-image" id=""/><embed> if not str.isdigit(image_id): logger.error('Image embed does not have a valid id: {}'.format(attrs)) return '<img>' # Try to efficiently fetch image from local memory image = None for img in images: if str(img.pk) == image_id: image = img # Default to fetching the image from the database if it is not found in images if not image: try: image = Image.objects.prefetch_related('renditions').get( id=image_id) except Image.DoesNotExist: return '<img>' # Figure out how we will format the image rendition if str.isdigit(width): # Use a custom width rendition if the embed has a width specified filter_spec = 'width-{}'.format(width) image_format = Format( filter_spec=filter_spec, classnames='richtext-image', # Unused required fields name='_', label='_', ) else: # Use the default 'fullwidth' rendition if no width is specified format_name = attrs.get('format', 'fullwidth') image_format = formats.get_image_format(format_name) return image_format.image_to_html(image, alt_text)
def setUp(self): # test format self.format = Format( 'test name', 'test label', 'test classnames', 'test filter spec' ) # test image self.image = MagicMock() self.image.id = 0
def setUp(self): # test format self.format = Format( 'test name', 'test label', 'test classnames', 'original' ) # test image self.image = Image.objects.create( title="Test image", file=get_test_image_file(), )
def test_image_to_html(self): cif = CaptionedImageFormat( "captioned_center", "Centered captioned", "bodytext-image", "width-800" ) test_file = get_test_image_file() image = Image.objects.create( title="Test image", file=test_file, ) alt_text = "An example image" default_html = Format.image_to_html(cif, image, alt_text) html = cif.image_to_html(image, alt_text) assert ( html == '<figure class="landscape">%s<figcaption>%s</figcaption></figure>' % ( default_html, alt_text, ) )
from wagtail.images.formats import Format, register_image_format register_image_format( Format('thumbnail', 'Thumbnail', 'richtext-image thumbnail', 'max-20x20'))
from wagtail.images.formats import ( Format, register_image_format, unregister_image_format, ) unregister_image_format("fullwidth") unregister_image_format("left") unregister_image_format("right") register_image_format( Format("left", "Left-aligned", "richtext-image format-left", "width-300")) register_image_format( Format("right", "Right-aligned", "richtext-image format-right", "width-300")) register_image_format( Format("center", "Center-aligned", "richtext-image format-center", "width-300"))
from wagtail.images.formats import Format, register_image_format, unregister_image_format unregister_image_format('fullwidth') register_image_format( Format('fullwidth', 'Full width', 'richtext-image full-width img-fluid d-block mx-auto mb-3', 'width-800')) unregister_image_format('left') register_image_format( Format( 'left', 'Left-aligned', 'richtext-image left img-fluid d-block mx-auto float-md-left mr-md-3 mb-3', 'width-400')) unregister_image_format('right') register_image_format( Format( 'right', 'Right-aligned', 'richtext-image right img-fluid d-block mx-auto float-md-right ml-md-3 mb-3', 'width-400')) register_image_format( Format('super_wide', 'Super wide', 'richtext-image super img-fluid mb-3', 'max-1200x1200'))
# image_formats.py from wagtail.images.formats import Format, register_image_format register_image_format( Format('thumbnail right float', 'Thumbnail Right Float', 'richtext-image-thumbnail-right-float', 'max-500x500')) register_image_format( Format('thumbnail left float', 'Thumbnail Left Float', 'richtext-image-thumbnail-left-float', 'max-500x500')) register_image_format( Format('thumbnail right 500', 'Thumbnail Right 500', 'richtext-image-thumbnail-right', 'max-500x500')) register_image_format( Format('thumbnail left 500', 'Thumbnail Left 500', 'richtext-image-thumbnail-left', 'max-500x500')) register_image_format( Format('full width', 'Full Width', 'richtextimagefullimage', 'width-1920')) register_image_format( Format('half width right float', 'Half Width Right Float', 'richtext-image-halfwidth-right-float', 'width-800')) register_image_format( Format('half width left float', 'Half Width LeftFloat', 'richtext-image-halfwidth-left-float', 'width-800')) register_image_format( Format('half width right', 'Half Width Right', 'richtext-image-halfwidth-right', 'width-800')) register_image_format( Format('half width left', 'Half Width Left', 'richtext-image-halfwidth-left', 'width-800'))
from wagtail.images.formats import Format, register_image_format, unregister_image_format unregister_image_format('fullwidth') register_image_format( Format('fullwidth', 'Full width', 'richtext-image img-fluid', 'width-1100')) unregister_image_format('left') register_image_format( Format('left', 'Left-aligned', 'richtext-image img-fluid float-left mr-3', 'width-450')) unregister_image_format('right') register_image_format( Format('right', 'Right-aligned', 'richtext-image img-fluid float-right ml-3', 'width-450'))
# image_formats.py from wagtail.images.formats import Format, register_image_format register_image_format( Format('center', 'Center', 'richtext-image center', 'width-800'))
# -*- coding: utf-8 -*- from django.utils.translation import ugettext_lazy as _ from wagtail.images.formats import Format, register_image_format register_image_format(Format('cosinnus_format_tiny', _('Tiny (50 x *)'), 'richtext-image richtext-image-tiny', 'width-50')) register_image_format(Format('cosinnus_format_very_small', _('Very Small (100 x *)'), 'richtext-image richtext-image-very-small', 'width-100')) register_image_format(Format('cosinnus_format_smaller', _('Smaller (150 x *)'), 'richtext-image richtext-image-smaller', 'width-150')) register_image_format(Format('cosinnus_format_small', _('Small (200 x *)'), 'richtext-image richtext-image-small', 'width-200')) register_image_format(Format('cosinnus_format_mediumer', _('Medium-small (250 x *)'), 'richtext-image richtext-image-mediumer', 'width-250')) register_image_format(Format('cosinnus_format_medium', _('Medium (300 x *)'), 'richtext-image richtext-image-medium', 'width-300')) register_image_format(Format('cosinnus_format_large', _('Large (400 x *)'), 'richtext-image richtext-image-large', 'width-400')) register_image_format(Format('cosinnus_format_larger', _('Larger (500 x *)'), 'richtext-image richtext-image-larger', 'width-500')) register_image_format(Format('cosinnus_format_very_large', _('Very large (650 x *)'), 'richtext-image richtext-image-very-large', 'width-650')) register_image_format(Format('cosinnus_format_extra_large', _('Extra large (800 x *)'), 'richtext-image richtext-image-extra-large', 'width-800')) register_image_format(Format('cosinnus_format_xxl', _('XXL (1200 x *)'), 'richtext-image richtext-image-xxl', 'width-1200'))
# image_formats.py from wagtail.images.formats import Format, unregister_image_format, register_image_format unregister_image_format('fullwidth') register_image_format( Format('fullwidth', 'Fullwidth', 'richtext-image fullwidth', 'max-1200x1200'))
from wagtail.images.formats import Format, register_image_format, unregister_image_format register_image_format(Format('thumbnail', 'Thumbnail', 'richtext-image thumbnail img-responsive', 'width-120'))
from wagtail.images.formats import Format, register_image_format register_image_format(Format('right-200', 'right-200', 'richtext-image right', 'width-200')) register_image_format(Format('left-200', 'left-200', 'richtext-image left', 'width-200'))
from wagtail.images.formats import Format, register_image_format # Set it to 2280px because 1140px is the max width of Bootstrap's container. # Then we multiply that number by 2 so the image can render well on retina. register_image_format( Format('fullwidth_high_quality', 'Full width high quality', 'richtext-image full-width high-quality', 'width-2280'))
from wagtail.images.formats import Format, register_image_format register_image_format( Format('thumbnail', 'Thumbnail', 'richtext-image thumbnail', 'width-300'))
class TestFormat(TestCase, WagtailTestUtils): def setUp(self): # test format self.format = Format( 'test name', 'test label', 'test classnames', 'original' ) # test image self.image = Image.objects.create( title="Test image", file=get_test_image_file(), ) def test_editor_attributes(self): result = self.format.editor_attributes( self.image, 'test alt text' ) self.assertEqual(result, {'data-alt': 'test alt text', 'data-embedtype': 'image', 'data-format': 'test name', 'data-id': self.image.pk}) def test_image_to_editor_html(self): result = self.format.image_to_editor_html( self.image, 'test alt text' ) self.assertTagInHTML( '<img data-embedtype="image" data-id="%d" data-format="test name" ' 'data-alt="test alt text" class="test classnames" ' 'width="640" height="480" alt="test alt text" >' % self.image.pk, result, allow_extra_attrs=True) def test_image_to_editor_html_with_quoting(self): result = self.format.image_to_editor_html( self.image, 'Arthur "two sheds" Jackson' ) expected_html = ( '<img data-embedtype="image" data-id="%d" data-format="test name" ' 'data-alt="Arthur "two sheds" Jackson" class="test classnames" ' 'width="640" height="480" alt="Arthur "two sheds" Jackson" >' % self.image.pk ) self.assertTagInHTML(expected_html, result, allow_extra_attrs=True) def test_image_to_html_no_classnames(self): self.format.classnames = None result = self.format.image_to_html(self.image, 'test alt text') self.assertTagInHTML( '<img width="640" height="480" alt="test alt text">', result, allow_extra_attrs=True) self.format.classnames = 'test classnames' def test_image_to_html_with_quoting(self): result = self.format.image_to_html(self.image, 'Arthur "two sheds" Jackson') self.assertTagInHTML( '<img class="test classnames" width="640" height="480" ' 'alt="Arthur "two sheds" Jackson">', result, allow_extra_attrs=True) def test_get_image_format(self): register_image_format(self.format) result = get_image_format('test name') self.assertEqual(result, self.format)
from wagtail.images.formats import Format, register_image_format # Atempting to implement Image Size Options: # https://erev0s.com/blog/wagtail-list-tips-and-tricks/#add-a-custom-widthheight-image-in-the-rich-text-editor register_image_format( Format('footer-col-head', 'footerColumnHeader25', 'richtext-image footer-col-head', 'max-25x25')) register_image_format( Format('max-50', 'Max50', 'richtext-image max-50', 'max-50x50')) register_image_format( Format('max-100', 'Max100', 'richtext-image max-100', 'max-100x100')) register_image_format( Format('max-150', 'Max150', 'richtext-image max-150', 'max-150x150')) register_image_format( Format('max-250', 'Max250', 'richtext-image max-250', 'max-250x250')) register_image_format( Format('max-350', 'Max350', 'richtext-image max-350', 'max-350x350')) register_image_format( Format('max-500', 'Max500', 'richtext-image max-500', 'max-500x500')) register_image_format( Format('max-750', 'Max750', 'richtext-image max-750', 'max-750x750'))
from wagtail.images.formats import Format, register_image_format from django.utils.translation import gettext_lazy as _ register_image_format( Format('center', _('Centered'), 'richtext-image center', 'original'))
from wagtail.images.formats import Format, register_image_format, unregister_image_format unregister_image_format('fullwidth') register_image_format( Format('fullwidth', 'Full width', 'richtext-image mx-auto d-block img-responsive', 'width-800')) unregister_image_format('left') register_image_format( Format('left', 'Left-aligned', 'richtext-image float-left img-responsive', 'width-500')) unregister_image_format('right') register_image_format( Format('right', 'Right-aligned', 'richtext-image float-right img-responsive', 'width-500'))
from wagtail.images.formats import Format, register_image_format register_image_format( Format("thumbnail", "Thumbnail", "richtext-image thumbnail", "max-120x120"))
except SourceImageIOError: # Image file is (probably) missing from /media/original_images - generate a dummy # rendition so that we just output a broken image, rather than crashing out completely # during rendering Rendition = image.renditions.model # pick up any custom Image / Rendition classes that may be in use half_rendition = Rendition(image=image, width=0, height=0) half_rendition.file.name = 'not-found' if self.classnames: class_attr = 'class="%s" ' % escape(self.classnames) else: class_attr = '' sizes = "(max-width: 480px) 512w, 100vw" srcset = "%s 512w, %s" % (escape( half_rendition.url), escape(rendition.url)) return ('<img %s%s ' 'width="%d" height="%d" ' 'alt="%s" srcset="%s" sizes="%s">') % ( extra_attributes, class_attr, rendition.width, rendition.height, alt_text, srcset, sizes) register_image_format( Format('halfwidth', 'Half Width (512px)', 'richtext-image half-width', 'max-512x410')) unregister_image_format("fullwidth") register_image_format( FullWidthImgFormat('fullwidth', 'Full width', 'richtext-image full-width', 'max-1400x1120'))
from wagtail.images.formats import Format, register_image_format, unregister_image_format unregister_image_format('fullwidth') unregister_image_format('left') unregister_image_format('right') register_image_format( Format('fullwidth', 'Full width', 'richtext-image img-responsive full-width', 'width-400')) register_image_format( Format('left', 'Left-aligned', 'richtext-image img-responsive left', 'width-200')) register_image_format( Format('right', 'Right-aligned', 'richtext-image img-responsive right', 'width-200'))
from wagtail.images.formats import Format, register_image_format, unregister_image_format # unregister default image formats in richtext component unregister_image_format('fullwidth') unregister_image_format('left') unregister_image_format('right') register_image_format( Format('full_width', 'Full width', 'richtext-image full-width', 'scale-100')) register_image_format( Format('left', 'Left', 'richtext-image float-left', 'max-500x300')) register_image_format( Format('right', 'Right', 'richtext-image float-right', 'max-500x300')) register_image_format( Format('center', 'Center', 'richtext-image mx-auto d-block', 'max-500x300'))
class TestFormat(TestCase): def setUp(self): # test format self.format = Format( 'test name', 'test label', 'test classnames', 'test filter spec' ) # test image self.image = MagicMock() self.image.id = 0 def test_editor_attributes(self): result = self.format.editor_attributes( self.image, 'test alt text' ) self.assertEqual(result, 'data-embedtype="image" data-id="0" data-format="test name" data-alt="test alt text" ') def test_image_to_editor_html(self): result = self.format.image_to_editor_html( self.image, 'test alt text' ) self.assertRegex( result, '<img data-embedtype="image" data-id="0" data-format="test name" ' 'data-alt="test alt text" class="test classnames" src="[^"]+" width="1" height="1" alt="test alt text">', ) def test_image_to_editor_html_with_quoting(self): result = self.format.image_to_editor_html( self.image, 'Arthur "two sheds" Jackson' ) self.assertRegex( result, '<img data-embedtype="image" data-id="0" data-format="test name" ' 'data-alt="Arthur "two sheds" Jackson" class="test classnames" src="[^"]+" width="1" height="1" alt="Arthur "two sheds" Jackson">', ) def test_image_to_html_no_classnames(self): self.format.classnames = None result = self.format.image_to_html(self.image, 'test alt text') self.assertRegex( result, '<img src="[^"]+" width="1" height="1" alt="test alt text">' ) self.format.classnames = 'test classnames' def test_image_to_html_with_quoting(self): result = self.format.image_to_html(self.image, 'Arthur "two sheds" Jackson') self.assertRegex( result, '<img class="test classnames" src="[^"]+" width="1" height="1" alt="Arthur "two sheds" Jackson">' ) def test_get_image_format(self): register_image_format(self.format) result = get_image_format('test name') self.assertEqual(result, self.format)
from wagtail.images.formats import Format, register_image_format register_image_format(Format('responive', 'Responsive', 'responsive img-responsive', 'max-1000x800'))
class TestFormat(TestCase, WagtailTestUtils): def setUp(self): # test format self.format = Format("test name", "test label", "test classnames", "original") # test image self.image = Image.objects.create( title="Test image", file=get_test_image_file(), ) def test_editor_attributes(self): result = self.format.editor_attributes(self.image, "test alt text") self.assertEqual( result, { "data-alt": "test alt text", "data-embedtype": "image", "data-format": "test name", "data-id": self.image.pk, }, ) def test_image_to_editor_html(self): result = self.format.image_to_editor_html(self.image, "test alt text") self.assertTagInHTML( '<img data-embedtype="image" data-id="%d" data-format="test name" ' 'data-alt="test alt text" class="test classnames" ' 'width="640" height="480" alt="test alt text" >' % self.image.pk, result, allow_extra_attrs=True, ) def test_image_to_editor_html_with_quoting(self): result = self.format.image_to_editor_html( self.image, 'Arthur "two sheds" Jackson') expected_html = ( '<img data-embedtype="image" data-id="%d" data-format="test name" ' 'data-alt="Arthur "two sheds" Jackson" class="test classnames" ' 'width="640" height="480" alt="Arthur "two sheds" Jackson" >' % self.image.pk) self.assertTagInHTML(expected_html, result, allow_extra_attrs=True) def test_image_to_html_no_classnames(self): self.format.classnames = None result = self.format.image_to_html(self.image, "test alt text") self.assertTagInHTML( '<img width="640" height="480" alt="test alt text">', result, allow_extra_attrs=True, ) self.format.classnames = "test classnames" def test_image_to_html_with_quoting(self): result = self.format.image_to_html(self.image, 'Arthur "two sheds" Jackson') self.assertTagInHTML( '<img class="test classnames" width="640" height="480" ' 'alt="Arthur "two sheds" Jackson">', result, allow_extra_attrs=True, ) def test_get_image_format(self): register_image_format(self.format) result = get_image_format("test name") self.assertEqual(result, self.format)
# image_formats.py from wagtail.images.formats import Format, register_image_format register_image_format( Format('thumbnail', 'Thumbnail', 'richtext-image thumbnail', 'fill-120x120')) register_image_format( Format('default', 'Default', 'richtext-image default', 'fill-300x200')) register_image_format( Format('middium', 'Middium', 'richtext-image middium', 'fill-450x300')) register_image_format( Format('large', 'Large', 'richtext-image large', 'fill-600x400'))
from wagtail.core.fields import StreamField from wagtail.images.formats import Format, register_image_format from wagtail.search import index '''To add a new size format use the following format register_image_format(Format('name', 'label', 'class_names', 'filter_spec')) These are the Format arguments: name -> unique key used to identify the format label -> label used in the chooser form when inserting the image in the RichTextField class_names -> string to assign to the class attribute of the generated <img> tag. filter_spec -> string specification to create the image rendition. ''' register_image_format( Format('600x600', '600x600', 'richtext-image 600x600', 'max-600x600')) register_image_format( Format('logo_icon', 'Logo_Icon', 'richtext-image logo_icon', 'max-30x30')) register_image_format( Format('original', 'Original', 'richtext-image original', 'original')) class ContentTag(TaggedItemBase): content_object = ParentalKey('core.Content', related_name='tagged_items') class Content(Page): body = StreamField([ ('heading', blocks.CharBlock(classname='full title')), ('rich_text', blocks.RichTextBlock()), ('raw', blocks.RawHTMLBlock()),
from wagtail.images.formats import Format, register_image_format, unregister_image_format unregister_image_format('fullwidth') register_image_format( Format('fullwidth', 'Full width', 'richtext-image full-width img-responsive', 'width-800')) unregister_image_format('left') register_image_format( Format('left', 'Left-aligned', 'richtext-image left img-responsive', 'width-500')) unregister_image_format('right') register_image_format( Format('right', 'Right-aligned', 'richtext-image right img-responsive', 'width-500'))