Ticket #1209: ifequal_AND_OR_Support.patch
File ifequal_AND_OR_Support.patch, 5.1 KB (added by , 19 years ago) |
---|
-
django/core/template/defaulttags.py
132 132 return '' 133 133 134 134 class IfEqualNode(Node): 135 def __init__(self, var 1, var2, nodelist_true, nodelist_false, negate):136 self.var 1, self.var2 = var1, var2135 def __init__(self, variables, nodelist_true, nodelist_false, negate): 136 self.variables = variables 137 137 self.nodelist_true, self.nodelist_false = nodelist_true, nodelist_false 138 138 self.negate = negate 139 139 … … 141 141 return "<IfEqualNode>" 142 142 143 143 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) 148 159 return self.nodelist_false.render(context) 149 160 150 161 class IfNode(Node): … … 478 489 {% else %} 479 490 ... 480 491 {% 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 %} 481 500 """ 482 501 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: 484 506 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 485 510 end_tag = 'end' + bits[0] 486 511 nodelist_true = parser.parse(('else', end_tag)) 487 512 token = parser.next_token() … … 490 515 parser.delete_first_token() 491 516 else: 492 517 nodelist_false = NodeList() 493 return IfEqualNode(bits [1], bits[2], nodelist_true, nodelist_false, negate)518 return IfEqualNode(bits, nodelist_true, nodelist_false, negate) 494 519 495 520 #@register.tag 496 521 def ifequal(parser, token): -
tests/othertests/templates.py
177 177 'ifequal18': ('{% ifequal a 1.2 %}yes{% else %}no{% endifequal %}', {"a": "1.2"}, "no"), 178 178 'ifequal19': ('{% ifequal a "1.2" %}yes{% else %}no{% endifequal %}', {"a": 1.2}, "no"), 179 179 '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"), 180 196 181 197 ### IFNOTEQUAL TAG ######################################################## 182 198 'ifnotequal01': ("{% ifnotequal a b %}yes{% endifnotequal %}", {"a": 1, "b": 2}, "yes"),