Added ability to use 'not' in template conditions. fixed #33

master
Иванов Денис 3 years ago
parent 804b7ee3d7
commit 80da6602f0

@ -984,6 +984,7 @@ class CalculateExtension(Extension):
self.tags = {'calculate', 'save', 'set_var'}
self.CONDITION_TOKENS_TYPES = {'eq', 'ne', 'lt', 'gt', 'lteq', 'gteq'}
self.CONDITION_NAME_TOKENS = {'not'}
self.LITERAL_TOKENS_TYPES = {'string', 'integer', 'float'}
if hasattr(self._datavars, 'variables_to_save'):
@ -1069,7 +1070,9 @@ class CalculateExtension(Extension):
and self.stream.current.value in self._parameters_set
and self.stream.look().type != 'dot'
and self.stream.look().type not in
self.CONDITION_TOKENS_TYPES):
self.CONDITION_TOKENS_TYPES
and self.stream.current.value not in
self.CONDITION_NAME_TOKENS):
# разбираем параметр.
# pairs_list.append(self.get_parameter_node())
name_node, value_node = self._get_parameter()
@ -1091,7 +1094,8 @@ class CalculateExtension(Extension):
elif (self._is_variable_name(self.stream.current)
or self.stream.current.type == 'lparen'
or self.stream.current.type == 'integer'):
or self.stream.current.type == 'integer'
or self.stream.current.value in self.CONDITION_NAME_TOKENS):
# разбираем условие. Если условие False -- кидаем исключение.
# condition_result = self.get_condition_result()
# if not condition_result:

@ -66,7 +66,7 @@ class TestTemplateEngine():
output_parameters.env == {datavars_module['other_vars']})
def test_if_an_input_template_contains_condition_and_it_is_True__the_template_engine_object_will_be_initialized_without_any_exceptions(self):
input_template = '''{% calculate vars.var_1 < vars.var_2 or (not (var_3 == 'required status') and vars.var_4), env = 'vars' %}'''
input_template = '''{% calculate not (var_3 == 'required status') and vars.var_4 or vars.var_1 < vars.var_2, env = 'vars' %}'''
datavars_module = Variables({'vars':
Variables({'var_1': 12,
'var_2': 1.2,

Loading…
Cancel
Save