0001-templates-add-support-for-in-and-not-in-using-a-lazy.patch
tests/test_formdata.py | ||
---|---|---|
1521 | 1521 |
assert condition.evaluate() is True |
1522 | 1522 | |
1523 | 1523 | |
1524 |
def test_lazy_conditions_in(pub, variable_test_data): |
|
1525 |
condition = Condition({'type': 'django', 'value': 'form_var_foo_foo in "foo, bar, baz"'}) |
|
1526 |
assert condition.evaluate() is True |
|
1527 | ||
1528 |
condition = Condition({'type': 'django', 'value': 'form_var_foo_foo not in "foo, bar, baz"'}) |
|
1529 |
assert condition.evaluate() is False |
|
1530 | ||
1531 |
condition = Condition({'type': 'django', 'value': '"b" in form_var_foo_foo'}) |
|
1532 |
assert condition.evaluate() is True |
|
1533 | ||
1534 |
condition = Condition({'type': 'django', 'value': '"b" not in form_var_foo_foo'}) |
|
1535 |
assert condition.evaluate() is False |
|
1536 | ||
1537 | ||
1524 | 1538 |
def test_has_role_templatefilter(pub, variable_test_data): |
1525 | 1539 |
condition = Condition({'type': 'django', 'value': 'form_user|has_role:"foobar"'}) |
1526 | 1540 |
assert condition.evaluate() is False |
wcs/conditions.py | ||
---|---|---|
16 | 16 | |
17 | 17 |
from quixote import get_publisher |
18 | 18 |
from django.template import Context, Template, TemplateSyntaxError |
19 |
import django.template.smartif |
|
19 | 20 |
from django.utils.encoding import force_text |
20 | 21 | |
21 | 22 |
from .qommon import _, get_logger, force_str |
... | ... | |
94 | 95 |
Template('{%% if %s %%}OK{%% endif %%}' % self.value) |
95 | 96 |
except (TemplateSyntaxError, OverflowError) as e: |
96 | 97 |
raise ValidationError(_('syntax error: %s') % force_str(force_text(e))) |
98 | ||
99 | ||
100 |
# add support for "in" and "not in" operators with left operand being a lazy |
|
101 |
# value. |
|
102 |
def lazy_eval(context, x): |
|
103 |
x = x.eval(context) |
|
104 |
if hasattr(x, 'get_value'): |
|
105 |
x = x.get_value() |
|
106 |
return x |
|
107 | ||
108 | ||
109 |
django.template.smartif.OPERATORS['in'] = django.template.smartif.infix( |
|
110 |
django.template.smartif.OPERATORS['in'].lbp, |
|
111 |
lambda context, x, y: lazy_eval(context, x) in y.eval(context)) |
|
112 | ||
113 |
django.template.smartif.OPERATORS['not in'] = django.template.smartif.infix( |
|
114 |
django.template.smartif.OPERATORS['not in'].lbp, |
|
115 |
lambda context, x, y: lazy_eval(context, x) not in y.eval(context)) |
|
97 |
- |