api: keep carddef/formdef internal references on update (#73912)

This commit is contained in:
Frédéric Péters 2023-01-27 09:06:51 +01:00 committed by Gitea
parent 7885be0160
commit 4a7f5b9604
2 changed files with 55 additions and 17 deletions

View File

@ -454,29 +454,14 @@ def test_export_import_redirect_url(pub):
get_app(pub).get('/api/export-import/roles/test/redirect/', status=404)
def create_bundle(*args):
def create_bundle(elements, *args):
tar_io = io.BytesIO()
with tarfile.open(mode='w', fileobj=tar_io) as tar:
manifest_json = {
'application': 'Test',
'slug': 'test',
'description': '',
'elements': [
{'type': 'forms-categories', 'slug': 'test', 'name': 'test'},
{'type': 'forms', 'slug': 'test', 'name': 'test'},
{'type': 'cards-categories', 'slug': 'test', 'name': 'test'},
{'type': 'cards', 'slug': 'test', 'name': 'test'},
{'type': 'blocks-categories', 'slug': 'test', 'name': 'test'},
{'type': 'blocks', 'slug': 'test', 'name': 'test'},
{'type': 'roles', 'slug': 'test', 'name': 'test'},
{'type': 'workflows-categories', 'slug': 'test', 'name': 'test'},
{'type': 'workflows', 'slug': 'test', 'name': 'test'},
{'type': 'mail-templates-categories', 'slug': 'test', 'name': 'test'},
{'type': 'mail-templates', 'slug': 'test', 'name': 'test'},
{'type': 'data-sources-categories', 'slug': 'test', 'name': 'test'},
{'type': 'data-sources', 'slug': 'test', 'name': 'test'},
{'type': 'wscalls', 'slug': 'test', 'name': 'test'},
],
'elements': elements,
}
manifest_fd = io.BytesIO(json.dumps(manifest_json, indent=2).encode())
tarinfo = tarfile.TarInfo('manifest.json')
@ -561,6 +546,21 @@ def test_export_import_bundle_import(pub):
wscall.store()
bundle = create_bundle(
[
{'type': 'forms', 'slug': 'test', 'name': 'test'},
{'type': 'cards-categories', 'slug': 'test', 'name': 'test'},
{'type': 'cards', 'slug': 'test', 'name': 'test'},
{'type': 'blocks-categories', 'slug': 'test', 'name': 'test'},
{'type': 'blocks', 'slug': 'test', 'name': 'test'},
{'type': 'roles', 'slug': 'test', 'name': 'test'},
{'type': 'workflows-categories', 'slug': 'test', 'name': 'test'},
{'type': 'workflows', 'slug': 'test', 'name': 'test'},
{'type': 'mail-templates-categories', 'slug': 'test', 'name': 'test'},
{'type': 'mail-templates', 'slug': 'test', 'name': 'test'},
{'type': 'data-sources-categories', 'slug': 'test', 'name': 'test'},
{'type': 'data-sources', 'slug': 'test', 'name': 'test'},
{'type': 'wscalls', 'slug': 'test', 'name': 'test'},
],
('forms-categories/test', category),
('forms/test', formdef),
('cards-categories/test', card_category),
@ -662,3 +662,35 @@ def test_export_import_bundle_import(pub):
formdef = FormDef.select()[0]
assert formdef.disabled is True
assert formdef.workflow_roles == {'_receiver': extra_role.id}
def test_export_import_formdef_do_not_overwrite_table_name(pub):
formdef = FormDef()
formdef.name = 'Test2'
formdef.fields = []
formdef.disabled = False
formdef.store()
assert formdef.table_name == 'formdata_%s_test2' % formdef.id
formdata = formdef.data_class()()
formdata.just_created()
formdata.store()
# change formdef url name, internal table name won't be changed
formdef.url_name = 'test'
formdef.store()
assert formdef.table_name == 'formdata_%s_test2' % formdef.id
assert formdef.data_class().count() == 1
bundle = create_bundle([{'type': 'forms', 'slug': 'test', 'name': 'test'}], ('forms/test', formdef))
resp = get_app(pub).put(sign_uri('/api/export-import/bundle-import/'), bundle)
afterjob_url = resp.json['url']
resp = get_app(pub).put(sign_uri(afterjob_url))
assert resp.json['data']['status'] == 'completed'
# check table name is not overwritten
formdef = FormDef.select()[0]
assert formdef.table_name == 'formdata_%s_test2' % formdef.id
assert formdef.data_class().count() == 1

View File

@ -293,6 +293,12 @@ class BundleImportJob(AfterJob):
continue
# replace
new_object.id = existing_object.id
if element['type'] in ('forms', 'cards'):
# keep internal references
new_object.internal_identifier = existing_object.internal_identifier
new_object.table_name = existing_object.table_name
if element['type'] in ('forms', 'cards') and not existing_object.name.startswith('[pre-import]'):
# keep some settings when updating
for attr in (