From 575050dec8eec6feb8ffb629807240c907651417 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Wed, 3 Aug 2022 00:00:09 +0200 Subject: [PATCH] backoffice: add action to duplicate blocks (#67594) --- tests/admin_pages/test_block.py | 42 +++++++++++++++++++++++++++++++-- wcs/admin/blocks.py | 34 ++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/tests/admin_pages/test_block.py b/tests/admin_pages/test_block.py index 2dbcf5064..73cf95ddc 100644 --- a/tests/admin_pages/test_block.py +++ b/tests/admin_pages/test_block.py @@ -436,7 +436,7 @@ def test_block_edit_field_warnings(pub): resp = app.get('/backoffice/forms/blocks/%s/' % blockdef.id) assert 'more than 30 fields' not in resp.text assert '

New Field

' in resp.text - assert '>Duplicate<' in resp.text + assert resp.pyquery('#fields-list a[title="Duplicate"]').length blockdef.fields.extend( [fields.StringField(id='%d' % i, label='field %d' % i, type='string') for i in range(21, 51)] @@ -445,7 +445,7 @@ def test_block_edit_field_warnings(pub): resp = app.get('/backoffice/forms/blocks/%s/' % blockdef.id) assert 'This block of fields contains 60 fields.' in resp.text assert '

New Field

' not in resp.text - assert '>Duplicate<' not in resp.text + assert not resp.pyquery('#fields-list a[title="Duplicate"]').length def test_block_inspect(pub): @@ -463,3 +463,41 @@ def test_block_inspect(pub): resp = app.get('/backoffice/forms/blocks/%s/' % block.id) resp = resp.click('Inspector') assert resp.pyquery('#inspect-fields .inspect-field').length == 2 + + +def test_block_duplicate(pub): + create_superuser(pub) + + BlockDef.wipe() + block = BlockDef() + block.name = 'Foobar' + block.fields = [ + fields.StringField(id='123', required=True, label='Test', type='string'), + fields.StringField(id='124', required=True, label='Test2', type='string'), + ] + block.store() + + app = login(get_app(pub)) + resp = app.get('/backoffice/forms/blocks/%s/' % block.id) + + resp = resp.click(href=re.compile('^duplicate$')) + assert resp.form['name'].value == 'Foobar (copy)' + resp = resp.form.submit('cancel').follow() + assert BlockDef.count() == 1 + + resp = resp.click(href=re.compile('^duplicate$')) + assert resp.form['name'].value == 'Foobar (copy)' + resp = resp.form.submit('submit').follow() + assert BlockDef.count() == 2 + + resp = app.get('/backoffice/forms/blocks/%s/' % block.id) + resp = resp.click(href=re.compile('^duplicate$')) + assert resp.form['name'].value == 'Foobar (copy 2)' + resp.form['name'].value = 'other copy' + resp = resp.form.submit('submit').follow() + assert BlockDef.count() == 3 + assert {x.name for x in BlockDef.select()} == {'Foobar', 'Foobar (copy)', 'other copy'} + assert {x.slug for x in BlockDef.select()} == {'foobar', 'foobar_copy', 'other_copy'} + + block_copy = BlockDef.get_by_slug('other_copy') + assert len(block_copy.fields) == 2 diff --git a/wcs/admin/blocks.py b/wcs/admin/blocks.py index 6ca116287..9462418b9 100644 --- a/wcs/admin/blocks.py +++ b/wcs/admin/blocks.py @@ -45,6 +45,7 @@ class BlockDirectory(FieldsDirectory): 'export', 'settings', 'inspect', + 'duplicate', ('history', 'snapshots_dir'), ] field_def_page_class = BlockFieldDefPage @@ -101,6 +102,7 @@ class BlockDirectory(FieldsDirectory): r = TemplateIO(html=True) r += htmltext('