datasource: don't reset slug on import (#46310)

This commit is contained in:
Lauréline Guérin 2020-09-07 15:30:39 +02:00
parent 049b485be0
commit a1c0fd02a0
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
2 changed files with 20 additions and 6 deletions

View File

@ -384,6 +384,7 @@ def test_data_sources_import(pub):
NamedDataSource.wipe()
data_source = NamedDataSource(name='foobar')
data_source.slug = 'baaaz'
data_source.data_source = {'type': 'formula', 'value': '[]'}
data_source.store()
data_source_xml = ET.tostring(data_source.export_to_xml(include_id=True))
@ -397,15 +398,24 @@ def test_data_sources_import(pub):
resp.forms[0]['file'] = Upload('datasource.wcs', data_source_xml)
resp = resp.forms[0].submit()
assert NamedDataSource.count() == 1
assert NamedDataSource.get(1).slug == 'baaaz'
# import the same datasource a second time, make sure slug is not reused
# check slug
resp = app.get('/backoffice/settings/data-sources/')
resp = resp.click(href='import')
resp.forms[0]['file'] = Upload('datasource.wcs', data_source_xml)
resp = resp.forms[0].submit()
assert NamedDataSource.count() == 2
assert NamedDataSource.get(1).slug == 'foobar'
assert NamedDataSource.get(2).slug == 'foobar-1'
assert NamedDataSource.get(1).slug == 'baaaz'
assert NamedDataSource.get(2).slug == 'foobar'
resp = app.get('/backoffice/settings/data-sources/')
resp = resp.click(href='import')
resp.forms[0]['file'] = Upload('datasource.wcs', data_source_xml)
resp = resp.forms[0].submit()
assert NamedDataSource.count() == 3
assert NamedDataSource.get(1).slug == 'baaaz'
assert NamedDataSource.get(2).slug == 'foobar'
assert NamedDataSource.get(3).slug == 'foobar-1'
# import an invalid file
resp = app.get('/backoffice/settings/data-sources/')

View File

@ -333,7 +333,6 @@ class NamedDataSourcesDirectory(Directory):
return r.getvalue()
def import_submit(self, form):
self.imported_datasource = None
fp = form.get_widget('file').parse().fp
error = False
@ -348,7 +347,12 @@ class NamedDataSourcesDirectory(Directory):
form.set_error('file', _('Invalid File'))
raise ValueError()
self.imported_datasource = datasource
datasource.slug = None # a new one will be set in .store()
try:
# check slug unicity
NamedDataSource.get_on_index(datasource.slug, 'slug', ignore_migration=True)
except KeyError:
pass
else:
datasource.slug = None # a new one will be set in .store()
datasource.store()
return redirect('%s/' % datasource.id)