def test_fallback_to_not_found(self): bad_image = Image.objects.get(id=1) good_image = Image.objects.create(title="Test image", file=get_test_image_file()) rendition = get_rendition_or_not_found(good_image, "width-400") self.assertEqual(rendition.width, 400) rendition = get_rendition_or_not_found(bad_image, "width-400") self.assertEqual(rendition.file.name, "not-found")
def test_fallback_to_not_found(self): bad_image = Image.objects.get(id=1) good_image = Image.objects.create( title="Test image", file=get_test_image_file(), ) rendition = get_rendition_or_not_found(good_image, 'width-400') self.assertEqual(rendition.width, 400) rendition = get_rendition_or_not_found(bad_image, 'width-400') self.assertEqual(rendition.file.name, 'not-found')
def image(request, pk, specs): ''' Request an image given some specs and redirects to it ''' image = get_object_or_404(get_image_model(), pk=pk) rendition = get_rendition_or_not_found(image, specs) return redirect(rendition.url)
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, }))
def get_image_field_display(self, field_name, field): """ Render an image """ from wagtail.wagtailimages.shortcuts import get_rendition_or_not_found image = getattr(self.instance, field_name) if image: return get_rendition_or_not_found(image, 'max-400x400').img_tag return self.model_admin.get_empty_value_display(field_name)
def admin_thumb(self, obj): try: image = getattr(obj, self.thumb_image_field_name, None) except AttributeError: raise ImproperlyConfigured( u"The `thumb_image_field_name` attribute on your `%s` class " "must name a field on your model." % self.__class__.__name__ ) img_attrs = { 'src': self.thumb_default, 'width': self.thumb_image_width, 'class': self.thumb_classname, } if not image: if self.thumb_default: return mark_safe('<img{}>'.format(flatatt(img_attrs))) return '' # try to get a rendition of the image to use from wagtail.wagtailimages.shortcuts import get_rendition_or_not_found spec = self.thumb_image_filter_spec rendition = get_rendition_or_not_found(image, spec) img_attrs.update({'src': rendition.url}) return mark_safe('<img{}>'.format(flatatt(img_attrs)))
def srcset(image, filter_specs): """ generate a list of image renditions suitable for html5 srcset usage: srcset="{{ image|srcset:'width-320|jpg width-640|jpg' }}" """ sources = {} for filter_spec in filter_specs.split(' '): rendition = get_rendition_or_not_found(image, filter_spec) if not rendition.width in sources: sources[rendition.width] = "%s %iw" % (rendition.url, rendition.width) return ', '.join(sources.values())
def render(self, context): try: image = self.image_expr.resolve(context) except template.VariableDoesNotExist: return '' if not image: return '' rendition = get_rendition_or_not_found(image, self.filter) if self.output_var_name: # return the rendition object in the given variable context[self.output_var_name] = rendition return '' else: # render the rendition's image tag now resolved_attrs = {} for key in self.attrs: resolved_attrs[key] = self.attrs[key].resolve(context) return rendition.img_tag(resolved_attrs)
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, }))
def admin_thumb(self, obj): try: image = getattr(obj, self.thumb_image_field_name, None) except AttributeError: raise ImproperlyConfigured( u"The `thumb_image_field_name` attribute on your `%s` class " "must name a field on your model." % self.__class__.__name__) img_attrs = { 'src': self.thumb_default, 'width': self.thumb_image_width, 'class': self.thumb_classname, } if not image: if self.thumb_default: return mark_safe('<img{}>'.format(flatatt(img_attrs))) return '' # try to get a rendition of the image to use from wagtail.wagtailimages.shortcuts import get_rendition_or_not_found spec = self.thumb_image_filter_spec rendition = get_rendition_or_not_found(image, spec) img_attrs.update({'src': rendition.url}) return mark_safe('<img{}>'.format(flatatt(img_attrs)))