environment: allow templated sites variable values (#74500)
gitea-wip/hobo/pipeline/pr-main This commit looks good Details

This commit is contained in:
Paul Marillonnet 2023-02-14 11:54:41 +01:00
parent fe64b31ba2
commit 3e6d5eaaf5
2 changed files with 26 additions and 2 deletions

View File

@ -73,8 +73,12 @@ class Variable(models.Model):
return self.name
def _parse_value_as_json(self):
if self.value and (
self.value[0] in '{[' or self.value in ('true', 'false', 'null') or FLOAT_RE.match(self.value)
if (
self.value
and not self.value.startswith(('{{', '{%'))
and (
self.value[0] in '{[' or self.value in ('true', 'false', 'null') or FLOAT_RE.match(self.value)
)
):
try:
return json.loads(self.value)

View File

@ -236,6 +236,26 @@ def test_new_variable_view(app, admin_user):
assert Variable.objects.all()[0].value == 'foofoo'
def test_new_variable_templated_value(app, admin_user):
app = login(app)
response = app.get('/sites/new-variable')
response.form['name'] = 'foo'
response.form['label'] = 'bar'
response.form['value'] = '{{ foobar }}'
response = response.form.submit()
assert response.location == '/sites/variables'
assert Variable.objects.all()[0].name == 'foo'
assert Variable.objects.all()[0].label == 'bar'
assert Variable.objects.all()[0].value == '{{ foobar }}'
response = app.get('/sites/new-variable')
response.form['name'] = 'foo'
response.form['label'] = 'bar'
response.form['value'] = '{% firstof foofoo "Plop" %}'
response = response.form.submit()
assert response.location == '/sites/variables'
assert Variable.objects.all()[0].value == '{% firstof foofoo "Plop" %}'
def test_new_variable_service_view(app, admin_user):
app = login(app)
Combo.objects.create(