blocks: convert slug dashes to underscores for use in digest template (#49760)

This commit is contained in:
Frédéric Péters 2020-12-28 13:39:27 +01:00
parent 6f99df0ff8
commit 53641d4e97
3 changed files with 12 additions and 8 deletions

View File

@ -707,12 +707,13 @@ def test_block_repeated_files(pub, blocks_feature):
assert 'test2.txt' in resp
def test_block_digest(pub, blocks_feature):
@pytest.mark.parametrize('block_name', ['foobar', 'Foo bar'])
def test_block_digest(pub, blocks_feature, block_name):
FormDef.wipe()
BlockDef.wipe()
block = BlockDef()
block.name = 'foobar'
block.name = block_name
block.fields = [
fields.StringField(
id='123', required=True, label='Test',
@ -726,7 +727,7 @@ def test_block_digest(pub, blocks_feature):
formdef = FormDef()
formdef.name = 'form title'
formdef.fields = [
fields.BlockField(id='1', label='test', type='block:foobar', max_items=3),
fields.BlockField(id='1', label='test', type='block:%s' % block.slug, max_items=3),
]
formdef.store()
formdef.data_class().wipe()
@ -745,12 +746,12 @@ def test_block_digest(pub, blocks_feature):
assert formdef.data_class().select()[0].data['1']['data'] == [
{'123': 'foo', '234': 'bar'}, {'123': 'foo2', '234': 'bar2'}]
# by default it gets the type of object
assert formdef.data_class().select()[0].data['1_display'] == 'foobar, foobar'
assert formdef.data_class().select()[0].data['1_display'] == '%s, %s' % (block.name, block.name)
# set a digest template
formdef.data_class().wipe()
block.digest_template = 'X{{foobar_var_foo}}Y'
block.digest_template = 'X{{%s_var_foo}}Y' % block.slug.replace('-', '_')
block.store()
resp = app.get(formdef.get_url())

View File

@ -147,7 +147,9 @@ class BlockDirectory(FieldsDirectory):
if disabled_slug:
widget.hint = _('The identifier can not be modified as the block is in use.')
form.add(
StringWidget, 'digest_template', title=_('Digest Template'), value=self.objectdef.digest_template, size=50
StringWidget, 'digest_template', title=_('Digest Template'),
value=self.objectdef.digest_template, size=50,
hint=_('Use %s_var_... to refer to fields.') % self.objectdef.slug.replace('-', '_')
)
if not self.objectdef.is_readonly():
form.add_submit('submit', _('Submit'))
@ -164,7 +166,7 @@ class BlockDirectory(FieldsDirectory):
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)
% self.objectdef.slug.replace('-', '_'))
else:
self.objectdef.digest_template = widget_template.parse()
self.objectdef.store()

View File

@ -92,7 +92,8 @@ class BlockDef(StorableObject):
from .qommon.substitution import CompatibilityNamesDict
from .variables import LazyBlockDataVar
context = CompatibilityNamesDict({self.slug + '_var': LazyBlockDataVar(self.fields, value)})
context = CompatibilityNamesDict(
{self.slug.replace('-', '_') + '_var': LazyBlockDataVar(self.fields, value)})
return Template(self.digest_template, autoescape=False).render(context)
def export_to_xml(self, include_id=False):