misc: add rich comparison methods to lazy field var (#68723) #335

Merged
fpeters merged 1 commits from wip/68723-lazy-rich-comparison into main 2023-05-26 11:30:04 +02:00
2 changed files with 26 additions and 0 deletions

View File

@ -2284,6 +2284,20 @@ def test_lazy_conditions_in(pub, variable_test_data):
assert condition.evaluate() is False
def test_lazy_conditions_rich_comparison(pub, variable_test_data):
condition = Condition({'type': 'django', 'value': 'form_var_foo_foo < "bar"'})
assert condition.evaluate() is False
condition = Condition({'type': 'django', 'value': 'form_var_foo_foo <= "bar"'})
assert condition.evaluate() is True
condition = Condition({'type': 'django', 'value': 'form_var_foo_foo >= "bar"'})
assert condition.evaluate() is True
condition = Condition({'type': 'django', 'value': 'form_var_foo_foo > "bar"'})
assert condition.evaluate() is False
def test_has_role_templatefilter(pub, variable_test_data):
condition = Condition({'type': 'django', 'value': 'form_user|has_role:"foobar"'})
assert condition.evaluate() is False

View File

@ -1107,6 +1107,18 @@ class LazyFieldVar:
def __ne__(self, other):
return force_str(self) != force_str(other)
def __lt__(self, other):
return force_str(self) < force_str(other)
def __le__(self, other):
return force_str(self) <= force_str(other)
def __gt__(self, other):
return force_str(self) > force_str(other)
def __ge__(self, other):
return force_str(self) >= force_str(other)
def __getitem__(self, key):
if isinstance(key, (int, slice)):
return self.get_value()[key]