Ticket #1209: ifequal_AND_OR_Support.patch

File ifequal_AND_OR_Support.patch, 5.1 KB (added by jay@…, 18 years ago)

patch to add AND OR support to the ifequal tag

  • django/core/template/defaulttags.py

     
    132132            return ''
    133133
    134134class IfEqualNode(Node):
    135     def __init__(self, var1, var2, nodelist_true, nodelist_false, negate):
    136         self.var1, self.var2 = var1, var2
     135    def __init__(self, variables, nodelist_true, nodelist_false, negate):
     136        self.variables = variables
    137137        self.nodelist_true, self.nodelist_false = nodelist_true, nodelist_false
    138138        self.negate = negate
    139139
     
    141141        return "<IfEqualNode>"
    142142
    143143    def render(self, context):
    144         val1 = resolve_variable(self.var1, context)
    145         val2 = resolve_variable(self.var2, context)
    146         if (self.negate and val1 != val2) or (not self.negate and val1 == val2):
    147             return self.nodelist_true.render(context)
     144        val1 = resolve_variable(self.variables[1], context)
     145        val2 = resolve_variable(self.variables[2], context)
     146        if len(self.variables) > 3:
     147            operator = self.variables[3]
     148            val3 = resolve_variable(self.variables[4], context)
     149            val4 = resolve_variable(self.variables[5], context)
     150            if operator == 'OR':
     151                if ((self.negate and val1 != val2) or (not self.negate and val1 == val2) or ((self.negate and val3 != val4) or (not self.negate and val3 == val4))):
     152                    return self.nodelist_true.render(context)
     153            else:
     154                if ((self.negate and val1 != val2) or (not self.negate and val1 == val2) and ((self.negate and val3 != val4) or (not self.negate and val3 == val4))):
     155                    return self.nodelist_true.render(context)
     156        else:
     157            if (self.negate and val1 != val2) or (not self.negate and val1 == val2):
     158                return self.nodelist_true.render(context)
    148159        return self.nodelist_false.render(context)
    149160
    150161class IfNode(Node):
     
    478489        {% else %}
    479490            ...
    480491        {% endifnotequal %}
     492
     493        {% ifequal user.id comment.user_id OR user.id owner.user_id %}
     494            ...
     495        {% endifequal %}
     496
     497        {% ifequal user.id comment.user_id AND user.id owner.user_id %}
     498            ...
     499        {% endifequal %}
    481500    """
    482501    bits = token.contents.split()
    483     if len(bits) != 3:
     502    operator = ''
     503    if len(bits) > 3:
     504        operator = bits[3]
     505    if operator == '' and len(bits) != 3:
    484506        raise TemplateSyntaxError, "%r takes two arguments" % bits[0]
     507    elif operator != '' and len(bits) < 6:
     508        raise TemplateSyntaxError, "%r takes four arguments when using OR or AND" % bits[0]
     509       
    485510    end_tag = 'end' + bits[0]
    486511    nodelist_true = parser.parse(('else', end_tag))
    487512    token = parser.next_token()
     
    490515        parser.delete_first_token()
    491516    else:
    492517        nodelist_false = NodeList()
    493     return IfEqualNode(bits[1], bits[2], nodelist_true, nodelist_false, negate)
     518    return IfEqualNode(bits, nodelist_true, nodelist_false, negate)
    494519
    495520#@register.tag
    496521def ifequal(parser, token):
  • tests/othertests/templates.py

     
    177177    'ifequal18': ('{% ifequal a 1.2 %}yes{% else %}no{% endifequal %}', {"a": "1.2"}, "no"),
    178178    'ifequal19': ('{% ifequal a "1.2" %}yes{% else %}no{% endifequal %}', {"a": 1.2}, "no"),
    179179    'ifequal20': ('{% ifequal a "1.2" %}yes{% else %}no{% endifequal %}', {"a": "1.2"}, "yes"),
     180   
     181    #OR
     182    'ifequal21': ('{% ifequal a b OR c d %}yes{% endifequal %}', {"a": 1, "b": 1, "c": 2, "d": 2}, "yes"),
     183    'ifequal22': ('{% ifequal a b OR c d %}yes{% endifequal %}', {"a": 1, "b": 2, "c": 2, "d": 2}, "yes"),
     184    'ifequal23': ('{% ifequal a b OR c d %}yes{% endifequal %}', {"a": 1, "b": 1, "c": 1, "d": 2}, "yes"),
     185    'ifequal24': ('{% ifequal a b OR c d %}yes{% endifequal %}', {"a": 1, "b": 2, "c": 1, "d": 2}, ""),
     186    'ifequal25': ('{% ifequal a b OR c d %}yes{% else %}no{% endifequal %}', {"a": 1, "b": 2, "c": 1, "d": 2}, "no"),
     187    'ifequal26': ('{% ifequal a b OR c d %}yes{% else %}no{% endifequal %}', {"a": 1, "b": 2, "c": 1, "d": 2}, "no"),
     188   
     189    #AND
     190    'ifequal27': ('{% ifequal a b AND c d %}yes{% endifequal %}', {"a": 1, "b": 1, "c": 2, "d": 2}, "yes"),
     191    'ifequal28': ('{% ifequal a b AND c d %}yes{% endifequal %}', {"a": 1, "b": 2, "c": 2, "d": 2}, ""),
     192    'ifequal28': ('{% ifequal a b AND c d %}yes{% endifequal %}', {"a": 1, "b": 1, "c": 1, "d": 2}, ""),
     193    'ifequal29': ('{% ifequal a b AND c d %}yes{% else %}no{% endifequal %}', {"a": 1, "b": 1, "c": 2, "d": 2}, "yes"),
     194    'ifequal30': ('{% ifequal a b AND c d %}yes{% else %}no{% endifequal %}', {"a": 1, "b": 2, "c": 2, "d": 2}, "no"),
     195    'ifequal30': ('{% ifequal a b AND c d %}yes{% else %}no{% endifequal %}', {"a": 1, "b": 1, "c": 1, "d": 2}, "no"),
    180196
    181197    ### IFNOTEQUAL TAG ########################################################
    182198    'ifnotequal01': ("{% ifnotequal a b %}yes{% endifnotequal %}", {"a": 1, "b": 2}, "yes"),
Back to Top