mirror of
https://github.com/Hopiu/wagtail.git
synced 2026-05-10 00:06:13 +00:00
Moved image_tags to wagtailimages_tags
This commit is contained in:
parent
b3e7951932
commit
008b3238ab
2 changed files with 74 additions and 66 deletions
|
|
@ -1,69 +1,8 @@
|
|||
from django import template
|
||||
import warnings
|
||||
|
||||
from wagtail.wagtailimages.models import Filter
|
||||
|
||||
register = template.Library()
|
||||
|
||||
# Local cache of filters, avoid hitting the DB
|
||||
filters = {}
|
||||
|
||||
@register.tag(name="image")
|
||||
def image(parser, token):
|
||||
args = token.split_contents()
|
||||
|
||||
if len(args) == 3:
|
||||
# token is of the form {% image self.photo max-320x200 %}
|
||||
tag_name, image_var, filter_spec = args
|
||||
return ImageNode(image_var, filter_spec)
|
||||
|
||||
elif len(args) == 5:
|
||||
# token is of the form {% image self.photo max-320x200 as img %}
|
||||
tag_name, image_var, filter_spec, as_token, out_var = args
|
||||
|
||||
if as_token != 'as':
|
||||
raise template.TemplateSyntaxError("'image' tag should be of the form {%% image self.photo max-320x200 %%} or {%% image self.photo max-320x200 as img %%}")
|
||||
|
||||
return ImageNode(image_var, filter_spec, out_var)
|
||||
|
||||
else:
|
||||
raise template.TemplateSyntaxError("'image' tag should be of the form {%% image self.photo max-320x200 %%} or {%% image self.photo max-320x200 as img %%}")
|
||||
warnings.warn(
|
||||
"The image_tags tags has been renamed. "
|
||||
"Use wagtailimages_tags instead.", DeprecationWarning)
|
||||
|
||||
|
||||
class ImageNode(template.Node):
|
||||
def __init__(self, image_var_name, filter_spec, output_var_name=None):
|
||||
self.image_var = template.Variable(image_var_name)
|
||||
self.output_var_name = output_var_name
|
||||
|
||||
if filter_spec not in filters:
|
||||
filters[filter_spec], _ = Filter.objects.get_or_create(spec=filter_spec)
|
||||
self.filter = filters[filter_spec]
|
||||
|
||||
def render(self, context):
|
||||
try:
|
||||
image = self.image_var.resolve(context)
|
||||
except template.VariableDoesNotExist:
|
||||
return ''
|
||||
|
||||
if not image:
|
||||
return ''
|
||||
|
||||
try:
|
||||
rendition = image.get_rendition(self.filter)
|
||||
except IOError:
|
||||
# It's fairly routine for people to pull down remote databases to their
|
||||
# local dev versions without retrieving the corresponding image files.
|
||||
# In such a case, we would get an IOError at the point where we try to
|
||||
# create the resized version of a non-existent image. Since this is a
|
||||
# bit catastrophic for a missing image, we'll substitute a dummy
|
||||
# Rendition object so that we just output a broken link instead.
|
||||
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 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
|
||||
return rendition.img_tag()
|
||||
from wagtail.wagtailimages.templatetags.wagtailimages_tags import register
|
||||
|
|
|
|||
69
wagtail/wagtailimages/templatetags/wagtailimages_tags.py
Normal file
69
wagtail/wagtailimages/templatetags/wagtailimages_tags.py
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
from django import template
|
||||
|
||||
from wagtail.wagtailimages.models import Filter
|
||||
|
||||
register = template.Library()
|
||||
|
||||
# Local cache of filters, avoid hitting the DB
|
||||
filters = {}
|
||||
|
||||
@register.tag(name="image")
|
||||
def image(parser, token):
|
||||
args = token.split_contents()
|
||||
|
||||
if len(args) == 3:
|
||||
# token is of the form {% image self.photo max-320x200 %}
|
||||
tag_name, image_var, filter_spec = args
|
||||
return ImageNode(image_var, filter_spec)
|
||||
|
||||
elif len(args) == 5:
|
||||
# token is of the form {% image self.photo max-320x200 as img %}
|
||||
tag_name, image_var, filter_spec, as_token, out_var = args
|
||||
|
||||
if as_token != 'as':
|
||||
raise template.TemplateSyntaxError("'image' tag should be of the form {%% image self.photo max-320x200 %%} or {%% image self.photo max-320x200 as img %%}")
|
||||
|
||||
return ImageNode(image_var, filter_spec, out_var)
|
||||
|
||||
else:
|
||||
raise template.TemplateSyntaxError("'image' tag should be of the form {%% image self.photo max-320x200 %%} or {%% image self.photo max-320x200 as img %%}")
|
||||
|
||||
|
||||
class ImageNode(template.Node):
|
||||
def __init__(self, image_var_name, filter_spec, output_var_name=None):
|
||||
self.image_var = template.Variable(image_var_name)
|
||||
self.output_var_name = output_var_name
|
||||
|
||||
if filter_spec not in filters:
|
||||
filters[filter_spec], _ = Filter.objects.get_or_create(spec=filter_spec)
|
||||
self.filter = filters[filter_spec]
|
||||
|
||||
def render(self, context):
|
||||
try:
|
||||
image = self.image_var.resolve(context)
|
||||
except template.VariableDoesNotExist:
|
||||
return ''
|
||||
|
||||
if not image:
|
||||
return ''
|
||||
|
||||
try:
|
||||
rendition = image.get_rendition(self.filter)
|
||||
except IOError:
|
||||
# It's fairly routine for people to pull down remote databases to their
|
||||
# local dev versions without retrieving the corresponding image files.
|
||||
# In such a case, we would get an IOError at the point where we try to
|
||||
# create the resized version of a non-existent image. Since this is a
|
||||
# bit catastrophic for a missing image, we'll substitute a dummy
|
||||
# Rendition object so that we just output a broken link instead.
|
||||
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 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
|
||||
return rendition.img_tag()
|
||||
Loading…
Reference in a new issue