Ticket #10043: 10043-with-tests.diff

File 10043-with-tests.diff, 2.6 KB (added by Eric Holscher, 15 years ago)
  • django/template/defaulttags.py

    diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py
    index c08ecc4..b31de38 100644
    a b class WidthRatioNode(Node):  
    395395        try:
    396396            value = self.val_expr.resolve(context)
    397397            maxvalue = self.max_expr.resolve(context)
     398            max_width = int(self.max_width.resolve(context))
    398399        except VariableDoesNotExist:
    399400            return ''
     401        except ValueError:
     402            raise TemplateSyntaxError("widthratio final argument must be an integer")
    400403        try:
    401404            value = float(value)
    402405            maxvalue = float(maxvalue)
    403             ratio = (value / maxvalue) * int(self.max_width)
     406            ratio = (value / maxvalue) * max_width
    404407        except (ValueError, ZeroDivisionError):
    405408            return ''
    406409        return str(int(round(ratio)))
    def widthratio(parser, token):  
    11361139    if len(bits) != 4:
    11371140        raise TemplateSyntaxError("widthratio takes three arguments")
    11381141    tag, this_value_expr, max_value_expr, max_width = bits
    1139     try:
    1140         max_width = int(max_width)
    1141     except ValueError:
    1142         raise TemplateSyntaxError("widthratio final argument must be an integer")
     1142
    11431143    return WidthRatioNode(parser.compile_filter(this_value_expr),
    1144                           parser.compile_filter(max_value_expr), max_width)
     1144                          parser.compile_filter(max_value_expr),
     1145                          parser.compile_filter(max_width))
    11451146widthratio = register.tag(widthratio)
    11461147
    11471148#@register.tag
  • tests/regressiontests/templates/tests.py

    diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py
    index 042aba2..9e4bba9 100644
    a b class Templates(unittest.TestCase):  
    908908            # Raise exception if we don't have 3 args, last one an integer
    909909            'widthratio08': ('{% widthratio %}', {}, template.TemplateSyntaxError),
    910910            'widthratio09': ('{% widthratio a b %}', {'a':50,'b':100}, template.TemplateSyntaxError),
    911             'widthratio10': ('{% widthratio a b 100.0 %}', {'a':50,'b':100}, template.TemplateSyntaxError),
     911            'widthratio10': ('{% widthratio a b 100.0 %}', {'a':50,'b':100}, '50'),
     912
     913            #Ticket 10043: Widthratio should allow maxlength to be a variable
     914            'widthratio11': ('{% widthratio a b c %}', {'a':50,'b':100, 'c': 100}, '50'),
    912915
    913916            ### WITH TAG ########################################################
    914917            'with01': ('{% with dict.key as key %}{{ key }}{% endwith %}', {'dict': {'key':50}}, '50'),
Back to Top