blocks: check variable name in digest template (#48207)

This commit is contained in:
Lauréline Guérin 2020-11-24 10:38:46 +01:00
parent 6238ac9b76
commit e10eca5c0d
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
2 changed files with 33 additions and 3 deletions

View File

@ -47,6 +47,7 @@ def teardown_module(module):
def test_block_new(pub, blocks_feature):
create_superuser(pub)
create_role()
BlockDef.wipe()
app = login(get_app(pub))
resp = app.get('/backoffice/forms/')
resp = resp.click('Fields blocks')
@ -106,6 +107,29 @@ def test_block_options(pub, blocks_feature):
resp = resp.follow()
def test_block_options_digest_template(pub, blocks_feature):
create_superuser(pub)
BlockDef.wipe()
block = BlockDef()
block.name = 'foobar'
block.fields = []
block.store()
app = login(get_app(pub))
resp = app.get('/backoffice/forms/blocks/%s/settings' % block.id)
resp.form['digest_template'] = 'X{{form_var_foo}}Y'
resp = resp.form.submit('submit')
assert 'Wrong variable "form_var_…" detected. Please replace it by "foobar_var_…".' in resp.text
block = BlockDef.get(block.id)
assert block.digest_template is None
resp = app.get('/backoffice/forms/blocks/%s/settings' % block.id)
resp.form['digest_template'] = 'X{{foobar_var_foo}}Y'
resp = resp.form.submit('submit')
block = BlockDef.get(block.id)
assert block.digest_template == 'X{{foobar_var_foo}}Y'
def test_block_export_import(pub, blocks_feature):
create_superuser(pub)
create_role()

View File

@ -160,9 +160,15 @@ class BlockDirectory(FieldsDirectory):
self.objectdef.name = form.get_widget('name').parse()
if form.get_widget('slug'):
self.objectdef.slug = form.get_widget('slug').parse()
self.objectdef.digest_template = form.get_widget('digest_template').parse()
self.objectdef.store()
return redirect('.')
widget_template = form.get_widget('digest_template')
if widget_template.parse() and 'form_var_' in widget_template.parse():
widget_template.set_error(
_('Wrong variable "form_var_…" detected. Please replace it by "%s_var_…".')
% self.objectdef.slug)
else:
self.objectdef.digest_template = widget_template.parse()
self.objectdef.store()
return redirect('.')
html_top(self.section, title=_('Settings'))
r = TemplateIO(html=True)