misc: add custom behaviour for booleans in [variables] (#56105)

This commit is contained in:
Frédéric Péters 2021-08-10 22:11:27 +02:00
parent 036577686a
commit 9e6bd493a6
2 changed files with 56 additions and 2 deletions

View File

@ -1245,3 +1245,29 @@ def test_qrcode(pub):
img = Template('{{ url|qrcode }}').render({'url': 1, 'allow_complex': True})
assert img == ''
def test_site_options_booleans(pub):
if not pub.site_options.has_section('variables'):
pub.site_options.add_section('variables')
pub.site_options.set('variables', 'something_true', 'true')
pub.site_options.set('variables', 'something_false', 'false')
with open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w') as fd:
pub.site_options.write(fd)
pub.load_site_options()
context = pub.substitutions.get_context_variables()
assert Template('{{ something_true }}').render(context) == 'true'
assert Template('{% if something_true %}hello{% endif %}').render(context) == 'hello'
assert Template('{% if something_true == "true" %}hello{% endif %}').render(context) == 'hello'
assert Template('{% if something_true == True %}hello{% endif %}').render(context) == 'hello'
assert Template('{% if something_true == "false" %}hello{% endif %}').render(context) == ''
assert Template('{% if something_true == False %}hello{% endif %}').render(context) == ''
assert Template('{{ something_false }}').render(context) == 'false'
assert Template('{% if something_false %}hello{% endif %}').render(context) == ''
assert Template('{% if something_false == False %}hello{% endif %}').render(context) == 'hello'
assert Template('{% if something_false == "false" %}hello{% endif %}').render(context) == 'hello'
assert Template('{% if something_false == True %}hello{% endif %}').render(context) == ''
assert Template('{% if something_false == "true" %}hello{% endif %}').render(context) == ''

View File

@ -67,6 +67,30 @@ class Tenant:
self.hostname = os.path.basename(directory)
class SiteOptionsBoolean:
# support class for values from site-options [variables] section that
# can be used as if strings as well as booleans
true_strings = ('yes', 'true', 'on')
false_strings = ('no', 'false', 'off')
def __init__(self, value):
if isinstance(value, str):
self.as_str = value
self.value = bool(value.lower() in self.true_strings)
else:
self.value = bool(value)
self.as_str = str(value)
def __bool__(self):
return self.value
def __eq__(self, other):
return bool(self) is bool(SiteOptionsBoolean(other))
def __str__(self):
return self.as_str
class QommonPublisher(Publisher):
# noqa pylint: disable=too-many-public-methods
APP_NAME = None
@ -913,9 +937,13 @@ class QommonPublisher(Publisher):
if self.site_options is None:
self.load_site_options()
try:
d.update(dict(self.site_options.items('variables', raw=True)))
site_options_vars = dict(self.site_options.items('variables', raw=True))
except configparser.NoSectionError:
pass
site_options_vars = {}
for k, v in site_options_vars.items():
if v.lower() in SiteOptionsBoolean.true_strings + SiteOptionsBoolean.false_strings:
site_options_vars[k] = SiteOptionsBoolean(v)
d.update(site_options_vars)
d['manager_homepage_url'] = d.get('portal_agent_url')
d['manager_homepage_title'] = d.get('portal_agent_title')
return d