backoffice: compute data_source URL if it's a template (#13902)

This commit is contained in:
Thomas NOËL 2020-03-09 16:34:12 +01:00 committed by Thomas NOËL
parent e3c5b3be9b
commit a56ae27bcb
3 changed files with 15 additions and 1 deletions

View File

@ -4994,6 +4994,13 @@ def test_data_sources_view(pub):
assert 'Preview' in resp.text
assert 'foo' in resp.text
# variadic url
data_source.data_source = {'type': 'json', 'value': '{{ site_url }}/foo/bar'}
data_source.store()
with HttpRequestsMocking() as http_requests:
resp = app.get('/backoffice/settings/data-sources/%s/' % data_source.id)
assert '<a href="http://example.net/foo/bar"' in resp.text
data_source.data_source = {'type': 'formula', 'value': '[str(x) for x in range(100)]'}
data_source.store()
resp = app.get('/backoffice/settings/data-sources/%s/' % data_source.id)

View File

@ -424,6 +424,13 @@ class NamedDataSource(XmlStorableObject):
def humanized_cache_duration(self):
return seconds2humanduration(int(self.cache_duration))
def get_variadic_url(self):
url = self.data_source.get('value').strip()
if url and Template.is_template_string(url):
vars = get_publisher().substitutions.get_context_variables(mode='lazy')
url = get_variadic_url(url, vars)
return url
def is_used_in_formdef(self, formdef):
from .fields import WidgetField
for field in formdef.fields or []:

View File

@ -18,7 +18,7 @@
<ul>
<li>{% trans "Type of source:" %} {{ datasource.type_label }}</li>
{% if datasource.data_source.type == 'json' or datasource.data_source.type == 'jsonp' %}
<li>{% trans "URL:" %} <a href="{{ datasource.data_source.value }}">{{ datasource.data_source.value }}</a></li>
<li>{% trans "URL:" %} <a href="{{ datasource.get_variadic_url }}">{{ datasource.data_source.value }}</a></li>
{% elif datasource.data_source.type == 'formula' %}
<li>{% trans "Python Expression:" %} {{ datasource.data_source.value }}</li>
{% endif %}