Fixed #10043 -- widthratio tag now accepts a variable for the max_width argument.
The max_width argument now passes through FilterExpression which by side-affect allows float values, but will be truncated. Thanks obeattie and Eric Holscher for patches. git-svn-id: http://code.djangoproject.com/svn/django/trunk@10352 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
624caace17
commit
64e82fb648
|
@ -402,12 +402,15 @@ class WidthRatioNode(Node):
|
||||||
try:
|
try:
|
||||||
value = self.val_expr.resolve(context)
|
value = self.val_expr.resolve(context)
|
||||||
maxvalue = self.max_expr.resolve(context)
|
maxvalue = self.max_expr.resolve(context)
|
||||||
|
max_width = int(self.max_width.resolve(context))
|
||||||
except VariableDoesNotExist:
|
except VariableDoesNotExist:
|
||||||
return ''
|
return ''
|
||||||
|
except ValueError:
|
||||||
|
raise TemplateSyntaxError("widthratio final argument must be an number")
|
||||||
try:
|
try:
|
||||||
value = float(value)
|
value = float(value)
|
||||||
maxvalue = float(maxvalue)
|
maxvalue = float(maxvalue)
|
||||||
ratio = (value / maxvalue) * int(self.max_width)
|
ratio = (value / maxvalue) * max_width
|
||||||
except (ValueError, ZeroDivisionError):
|
except (ValueError, ZeroDivisionError):
|
||||||
return ''
|
return ''
|
||||||
return str(int(round(ratio)))
|
return str(int(round(ratio)))
|
||||||
|
@ -1143,12 +1146,10 @@ def widthratio(parser, token):
|
||||||
if len(bits) != 4:
|
if len(bits) != 4:
|
||||||
raise TemplateSyntaxError("widthratio takes three arguments")
|
raise TemplateSyntaxError("widthratio takes three arguments")
|
||||||
tag, this_value_expr, max_value_expr, max_width = bits
|
tag, this_value_expr, max_value_expr, max_width = bits
|
||||||
try:
|
|
||||||
max_width = int(max_width)
|
|
||||||
except ValueError:
|
|
||||||
raise TemplateSyntaxError("widthratio final argument must be an integer")
|
|
||||||
return WidthRatioNode(parser.compile_filter(this_value_expr),
|
return WidthRatioNode(parser.compile_filter(this_value_expr),
|
||||||
parser.compile_filter(max_value_expr), max_width)
|
parser.compile_filter(max_value_expr),
|
||||||
|
parser.compile_filter(max_width))
|
||||||
widthratio = register.tag(widthratio)
|
widthratio = register.tag(widthratio)
|
||||||
|
|
||||||
#@register.tag
|
#@register.tag
|
||||||
|
|
|
@ -923,7 +923,10 @@ class Templates(unittest.TestCase):
|
||||||
# Raise exception if we don't have 3 args, last one an integer
|
# Raise exception if we don't have 3 args, last one an integer
|
||||||
'widthratio08': ('{% widthratio %}', {}, template.TemplateSyntaxError),
|
'widthratio08': ('{% widthratio %}', {}, template.TemplateSyntaxError),
|
||||||
'widthratio09': ('{% widthratio a b %}', {'a':50,'b':100}, template.TemplateSyntaxError),
|
'widthratio09': ('{% widthratio a b %}', {'a':50,'b':100}, template.TemplateSyntaxError),
|
||||||
'widthratio10': ('{% widthratio a b 100.0 %}', {'a':50,'b':100}, template.TemplateSyntaxError),
|
'widthratio10': ('{% widthratio a b 100.0 %}', {'a':50,'b':100}, '50'),
|
||||||
|
|
||||||
|
# #10043: widthratio should allow max_width to be a variable
|
||||||
|
'widthratio11': ('{% widthratio a b c %}', {'a':50,'b':100, 'c': 100}, '50'),
|
||||||
|
|
||||||
### WITH TAG ########################################################
|
### WITH TAG ########################################################
|
||||||
'with01': ('{% with dict.key as key %}{{ key }}{% endwith %}', {'dict': {'key':50}}, '50'),
|
'with01': ('{% with dict.key as key %}{{ key }}{% endwith %}', {'dict': {'key':50}}, '50'),
|
||||||
|
|
Loading…
Reference in New Issue