Refactor image tag parser so that it only has two cases to handle

pull/342/head
Matt Westcott 2014-06-19 21:55:45 +01:00
rodzic 64b933ceee
commit 5e055972cd
2 zmienionych plików z 10 dodań i 19 usunięć

Wyświetl plik

@ -68,8 +68,8 @@ class AbstractImage(models.Model, TagSearchable):
except ObjectDoesNotExist:
file_field = self.file
# If we have a backend attribute then pass it to process
# image - else pass 'default'
# If we have a backend attribute then pass it to process
# image - else pass 'default'
backend_name = getattr(self, 'backend', 'default')
generated_image_file = filter.process_image(file_field.file, backend_name=backend_name)

Wyświetl plik

@ -15,31 +15,22 @@ def image(parser, token):
filter_spec = bits[1]
bits = bits[2:]
if len(bits) == 0:
# token is of the form {% image self.photo max-320x200 %}
return ImageNode(image_var, filter_spec)
elif len(bits) == 2:
if len(bits) == 2 and bits[0] == 'as':
# token is of the form {% image self.photo max-320x200 as img %}
if bits[0] == 'as':
return ImageNode(image_var, filter_spec, output_var_name=bits[1])
if len(bits) > 0:
# customized <img> attrs
return ImageNode(image_var, filter_spec, output_var_name=bits[1])
else:
# token is of the form {% image self.photo max-320x200 %} - all additional tokens
# should be kwargs, which become attributes
attrs = {}
for bit in bits:
try:
name,value = bit.split('=')
except:
raise template.TemplateSyntaxError("'image' tag should be of the form {%% image self.photo max-320x200 [ custom-attr=\"value\" [ ... ] ] %%} or {%% image self.photo max-320x200 as img %%}")
name, value = bit.split('=')
except ValueError:
raise template.TemplateSyntaxError("'image' tag should be of the form {% image self.photo max-320x200 [ custom-attr=\"value\" ... ] %} or {% image self.photo max-320x200 as img %}")
attrs[name] = parser.compile_filter(value) # setup to resolve context variables as value
return ImageNode(image_var, filter_spec, attrs=attrs)
# something is wrong if we made it this far
raise template.TemplateSyntaxError("'image' tag should be of the form {%% image self.photo max-320x200 [ custom-attr=\"value\" [ ... ] ] %%} or {%% image self.photo max-320x200 as img %%}")
class ImageNode(template.Node):
def __init__(self, image_var_name, filter_spec, output_var_name=None, attrs={}):