def chooser_select_format(request, image_id): image = get_object_or_404(get_image_model(), id=image_id) if request.method == 'POST': form = ImageInsertionForm(request.POST, initial={'alt_text': image.default_alt_text}, prefix='image-chooser-insertion') if form.is_valid(): format = get_image_format(form.cleaned_data['format']) preview_image = image.get_rendition(format.filter_spec) image_data = { 'id': image.id, 'title': image.title, 'format': format.name, 'alt': form.cleaned_data['alt_text'], 'class': format.classnames, 'edit_link': reverse('wagtailimages:edit', args=(image.id, )), '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, None, None, json_data={ 'step': 'image_chosen', 'result': image_data }) else: initial = {'alt_text': image.default_alt_text} initial.update(request.GET.dict()) # If you edit an existing image, and there is no alt text, ensure that # "image is decorative" is ticked when you open the form initial['image_is_decorative'] = initial['alt_text'] == '' form = ImageInsertionForm(initial=initial, prefix='image-chooser-insertion') return render_modal_workflow(request, 'wagtailimages/chooser/select_format.html', None, { 'image': image, 'form': form }, json_data={'step': 'select_format'})
def img_tag(self, extra_attributes=None): if extra_attributes is None: extra_attributes = {} fw_format = get_image_format("fullwidth") extra_attrs = extra_attributes 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(cls, attrs): """ Given a dict of attributes from the <embed> tag, return the real HTML representation for use on the front-end. """ try: image = cls.get_instance(attrs) except ObjectDoesNotExist: return '<img alt="">' image_format = get_image_format(attrs['format']) return image_format.image_to_html(image, attrs.get('alt', ''))
def expand_db_attributes(cls, attrs): """ Given a dict of attributes from the <embed> tag, return the real HTML representation for use on the front-end. """ try: image = cls.get_instance(attrs) except ObjectDoesNotExist: return '<img alt="">' image_format = get_image_format(attrs["format"]) return image_format.image_to_html(image, attrs.get("alt", ""))
def image_embedtype_handler(attrs): """ Given a dict of attributes from the <embed> tag, return the real HTML representation for use on the front-end. """ Image = get_image_model() try: image = Image.objects.get(id=attrs['id']) except Image.DoesNotExist: return "<img>" image_format = get_image_format(attrs['format']) return image_format.image_to_html(image, attrs.get('alt', ''))
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 expand_db_attributes(attrs): """ Given a dict of attributes from the <embed> tag, return the real HTML representation for use within the editor. """ Image = get_image_model() try: image = Image.objects.get(id=attrs['id']) except Image.DoesNotExist: return "<img>" image_format = get_image_format(attrs['format']) return image_format.image_to_editor_html(image, attrs.get('alt', ''))
def expand_db_attributes(attrs): """ Given a dict of attributes from the <embed> tag, return the real HTML representation for use within the editor. """ Image = get_image_model() try: image = Image.objects.get(id=attrs["id"]) except Image.DoesNotExist: return '<img alt="">' image_format = get_image_format(attrs["format"]) return image_format.image_to_editor_html(image, attrs.get("alt", ""))
def get_chosen_response_data(self, image): format = get_image_format(self.form.cleaned_data["format"]) alt_text = self.form.cleaned_data["alt_text"] response_data = super().get_chosen_response_data( image, preview_image_filter=format.filter_spec ) response_data.update( { "format": format.name, "alt": alt_text, "class": format.classnames, "html": format.image_to_editor_html(image, alt_text), } ) return response_data
def chooser_select_format(request, image_id): image = get_object_or_404(get_image_model(), id=image_id) if request.method == '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, 'edit_link': reverse('wagtailimages:edit', args=(image.id, )), '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: initial = {'alt_text': image.default_alt_text} initial.update(request.GET.dict()) form = ImageInsertionForm(initial=initial) return render_modal_workflow(request, 'wagtailimages/chooser/select_format.html', 'wagtailimages/chooser/select_format.js', { 'image': image, 'form': form })
def create_entity(self, name, attrs, state, contentstate): Image = get_image_model() try: image = Image.objects.get(id=attrs['id']) image_format = get_image_format(attrs['format']) rendition = get_rendition_or_not_found(image, image_format.filter_spec) src = rendition.url except Image.DoesNotExist: src = '' return Entity('IMAGE', 'IMMUTABLE', { 'id': attrs['id'], 'src': src, 'alt': attrs.get('alt'), 'format': attrs['format'] })
def image_link_handler(attrs): """Handle a link of the form <embed embedtype="image" id="123">""" # Default implementation from wagtail.images.rich_text.image_embedtype_handler Image = get_image_model() try: image = Image.objects.get(id=attrs['id']) except Image.DoesNotExist: return "<img>" image_format = get_image_format(attrs['format']) # form extra src attribute base_url = Site.objects.first().root_url rendition = get_rendition_or_not_found(image, image_format.filter_spec) extra_attributes = {'src': '{}{}'.format(base_url, rendition.url)} # From default implementation, except extra src attribute return image_format.image_to_html(image, attrs.get('alt', ''), extra_attributes)
def create_entity(self, name, attrs, state, contentstate): Image = get_image_model() try: image = Image.objects.get(id=attrs["id"]) image_format = get_image_format(attrs["format"]) rendition = get_rendition_or_not_found(image, image_format.filter_spec) src = rendition.url except Image.DoesNotExist: src = "" return Entity( "IMAGE", "IMMUTABLE", { "id": attrs["id"], "src": src, "alt": attrs.get("alt"), "format": attrs["format"], }, )
def chooser_select_format(request, image_id): image = get_object_or_404(get_image_model(), id=image_id) if request.method == 'POST': form = ImageInsertionForm( request.POST, initial={'alt_text': image.default_alt_text}, prefix='image-chooser-insertion' ) if form.is_valid(): format = get_image_format(form.cleaned_data['format']) preview_image = image.get_rendition(format.filter_spec) image_data = { 'id': image.id, 'title': image.title, 'format': format.name, 'alt': form.cleaned_data['alt_text'], 'class': format.classnames, 'edit_link': reverse('wagtailimages:edit', args=(image.id,)), '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, None, None, json_data={'step': 'image_chosen', 'result': image_data} ) else: initial = {'alt_text': image.default_alt_text} initial.update(request.GET.dict()) form = ImageInsertionForm(initial=initial, prefix='image-chooser-insertion') return render_modal_workflow( request, 'wagtailimages/chooser/select_format.html', None, {'image': image, 'form': form}, json_data={'step': 'select_format'} )
def test_get_image_format(self): register_image_format(self.format) result = get_image_format("test name") self.assertEqual(result, self.format)
def test_get_image_format(self): register_image_format(self.format) result = get_image_format('test name') self.assertEqual(result, self.format)
def image_select_format(request, image_id): image = get_object_or_404(get_image_model(), id=image_id) if request.method == "POST": form = ImageInsertionForm( request.POST, initial={"alt_text": image.default_alt_text}, prefix="image-chooser-insertion", ) if form.is_valid(): format = get_image_format(form.cleaned_data["format"]) preview_image = image.get_rendition(format.filter_spec) image_data = { "id": image.id, "title": image.title, "format": format.name, "alt": form.cleaned_data["alt_text"], "class": format.classnames, "edit_link": reverse("wagtailimages:edit", args=(image.id, )), "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, None, None, json_data={ "step": "image_chosen", "result": image_data }, ) else: initial = {"alt_text": image.default_alt_text} initial.update(request.GET.dict()) form = ImageInsertionForm(initial=initial, prefix="image-chooser-insertion") return render_modal_workflow( request, "non_admin_draftail/image/select_format.html", None, { "image": image, "form": form }, json_data={"step": "select_format"}, )
def image_halfwidth(sender, instance, **kwargs): # Create thumbnails for the image thumbnail_format = get_image_format("halfwidth") instance.get_rendition(thumbnail_format.filter_spec)