Example #1
0
    def render(self, value):
        template = getattr(self.meta, 'template', None)

        prep_values = self.get_prep_value(value)
        format = get_image_format(value['format'])
        image = value['image']

        # Comes from wagtailimages\formats.py Format.image_to_html
        # TODO: possibly refactor wagtail codebase so Format can return silent-failing rendition
        try:
            rendition = image.get_rendition(format.filter_spec)
        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
            rendition = Rendition(image=image, width=0, height=0)
            rendition.file.name = 'not-found'

        if format.classnames:
            class_attr = 'class="%s" ' % escape(format.classnames)
        else:
            class_attr = ''

        value_dict = dict(value)
        value_dict.update({
            'rendition': rendition,
            'class_attr': mark_safe(class_attr),
            'self': value
        })
        return render_to_string(template, value_dict)
Example #2
0
def chooser_select_format(request, image_id):
    image = get_object_or_404(get_image_model(), id=image_id)

    if request.POST:
        form = ImageInsertionForm(request.POST, initial={"alt_text": image.default_alt_text})
        if form.is_valid():

            format = get_image_format(form.cleaned_data["format"])
            preview_image = image.get_rendition(format.filter_spec)

            image_json = json.dumps(
                {
                    "id": image.id,
                    "title": image.title,
                    "format": format.name,
                    "alt": form.cleaned_data["alt_text"],
                    "class": format.classnames,
                    "preview": {"url": preview_image.url, "width": preview_image.width, "height": preview_image.height},
                    "html": format.image_to_editor_html(image, form.cleaned_data["alt_text"]),
                }
            )

            return render_modal_workflow(
                request, None, "wagtailimages/chooser/image_chosen.js", {"image_json": image_json}
            )
    else:
        form = ImageInsertionForm(initial={"alt_text": image.default_alt_text})

    return render_modal_workflow(
        request,
        "wagtailimages/chooser/select_format.html",
        "wagtailimages/chooser/select_format.js",
        {"image": image, "form": form},
    )
Example #3
0
 def img_tag(self, extra_attributes=''):
     fw_format = get_image_format("fullwidth")
     extra_attrs = extra_attributes or ''
     if fw_format.filter_spec == self.filter.spec:
         return fw_format.image_to_html(self.image, self.image.title,
                                        extra_attrs)
     return super(AffixImageRendition, self).img_tag(extra_attrs)
Example #4
0
def Image(props):
    """
    Inspired by:
    - https://github.com/torchbox/wagtail/blob/master/wagtail/wagtailimages/rich_text.py
    - https://github.com/torchbox/wagtail/blob/master/wagtail/wagtailimages/shortcuts.py
    - https://github.com/torchbox/wagtail/blob/master/wagtail/wagtailimages/formats.py
    """
    image_model = get_image_model()
    alignment = props.get('alignment', 'left')
    alt_text = props.get('altText', '')

    try:
        image = image_model.objects.get(id=props['id'])
    except image_model.DoesNotExist:
        return DOM.create_element('img', {'alt': alt_text})

    image_format = get_image_format(alignment)
    rendition = get_rendition_or_not_found(image, image_format.filter_spec)

    return DOM.create_element(
        'img',
        dict(
            rendition.attrs_dict, **{
                'class': image_format.classnames,
                'src': rendition.url,
                'alt': alt_text,
            }))
Example #5
0
def chooser_select_format(request, image_id):
    image = get_object_or_404(get_image_model(), id=image_id)

    if request.POST:
        form = ImageInsertionForm(request.POST, initial={'alt_text': image.default_alt_text})
        if form.is_valid():

            format = get_image_format(form.cleaned_data['format'])
            preview_image = image.get_rendition(format.filter_spec)

            image_json = json.dumps({
                'id': image.id,
                'title': image.title,
                'format': format.name,
                'alt': form.cleaned_data['alt_text'],
                'class': format.classnames,
                'preview': {
                    'url': preview_image.url,
                    'width': preview_image.width,
                    'height': preview_image.height,
                },
                'html': format.image_to_editor_html(image, form.cleaned_data['alt_text']),
            })

            return render_modal_workflow(
                request, None, 'wagtailimages/chooser/image_chosen.js',
                {'image_json': image_json}
            )
    else:
        form = ImageInsertionForm(initial={'alt_text': image.default_alt_text})

    return render_modal_workflow(
        request, 'wagtailimages/chooser/select_format.html', 'wagtailimages/chooser/select_format.js',
        {'image': image, 'form': form}
    )
Example #6
0
def chooser_select_format(request, image_id):
    image = get_object_or_404(get_image_model(), id=image_id)

    if request.POST:
        form = ImageInsertionForm(request.POST, initial={'alt_text': image.default_alt_text})
        if form.is_valid():

            format = get_image_format(form.cleaned_data['format'])
            preview_image = image.get_rendition(format.filter_spec)

            image_json = json.dumps({
                'id': image.id,
                'title': image.title,
                'format': format.name,
                'alt': form.cleaned_data['alt_text'],
                'class': format.classnames,
                'preview': {
                    'url': preview_image.url,
                    'width': preview_image.width,
                    'height': preview_image.height,
                },
                'html': format.image_to_editor_html(image, form.cleaned_data['alt_text']),
            })

            return render_modal_workflow(
                request, None, 'wagtailimages/chooser/image_chosen.js',
                {'image_json': image_json}
            )
    else:
        form = ImageInsertionForm(initial={'alt_text': image.default_alt_text})

    return render_modal_workflow(
        request, 'wagtailimages/chooser/select_format.html', 'wagtailimages/chooser/select_format.js',
        {'image': image, 'form': form}
    )
Example #7
0
 def img_tag(self, extra_attributes=''):
     fw_format = get_image_format("fullwidth")
     extra_attrs = extra_attributes or ''
     if fw_format.filter_spec == self.filter.spec:
         return fw_format.image_to_html(
             self.image, self.image.title, extra_attrs
         )
     return super(AffixImageRendition, self).img_tag(extra_attrs)
    def expand_db_attributes(attrs, for_editor):
        """
        Given a dict of attributes from the <embed> tag, return the real HTML
        representation.
        """
        Image = get_image_model()
        try:
            image = Image.objects.get(id=attrs['id'])
        except Image.DoesNotExist:
            return "<img>"

        image_format = get_image_format(attrs['format'])

        if for_editor:
            return image_format.image_to_editor_html(image, attrs['alt'])
        else:
            return image_format.image_to_html(image, attrs['alt'])
Example #9
0
    def expand_db_attributes(attrs, for_editor):
        """
        Given a dict of attributes from the <embed> tag, return the real HTML
        representation.
        """
        Image = get_image_model()
        try:
            image = Image.objects.get(id=attrs['id'])
        except Image.DoesNotExist:
            return "<img>"

        image_format = get_image_format(attrs['format'])

        if for_editor:
            return image_format.image_to_editor_html(image, attrs['alt'])
        else:
            return image_format.image_to_html(image, attrs['alt'])
Example #10
0
def get_image_json(image):
    """
    helper function: given an image, return the json to pass back to the
    image chooser panel or the editor to set the poster image
    """
    format = get_image_format('left')
    preview_image = image.get_rendition(format.filter_spec)
    return json.dumps({
        'id': image.id,
        'title': image.title,
        'format': format.name,
        'alt': image.title,
        'class': format.classnames,
        'preview': {
            'url': preview_image.url,
            'width': preview_image.width,
            'height': preview_image.height,
        },
        'html': format.image_to_editor_html(image, image.title),
    })
Example #11
0
    def render(self, props):
        image_model = get_image_model()
        alignment = props.get('alignment', 'left')
        alt_text = props.get('altText', '')

        try:
            image = image_model.objects.get(id=props['id'])
        except image_model.DoesNotExist:
            return DOM.create_element('img', {'alt': alt_text})

        image_format = get_image_format(alignment)
        rendition = get_rendition_or_not_found(image, image_format.filter_spec)

        return DOM.create_element(
            'img',
            dict(
                rendition.attrs_dict, **{
                    'class': image_format.classnames,
                    'src': rendition.url,
                    'alt': alt_text,
                }))
Example #12
0
 def test_get_image_format(self):
     register_image_format(self.format)
     result = get_image_format('test name')
     self.assertEqual(result, self.format)
Example #13
0
 def test_get_image_format(self):
     register_image_format(self.format)
     result = get_image_format('test name')
     self.assertEqual(result, self.format)
Example #14
0
def image_halfwidth(sender, instance, **kwargs):
    # Create thumbnails for the image
    thumbnail_format = get_image_format("halfwidth")
    instance.get_rendition(thumbnail_format.filter_spec)