Import multiselection field as CSV list
Closes #13995 (grafted from b750eb7cfb864e10a081e20ac5b3449c591cd484) --HG-- branch : 7.0
This commit is contained in:
@@ -1016,6 +1016,16 @@ class ModelStorage(Model):
|
||||
elif value:
|
||||
return value
|
||||
|
||||
def convert_multiselection(value):
|
||||
if isinstance(value, str) and value:
|
||||
return next(csv.reader(
|
||||
value.splitlines(),
|
||||
delimiter=',',
|
||||
quoting=csv.QUOTE_NONE,
|
||||
escapechar='\\'))
|
||||
else:
|
||||
return value
|
||||
|
||||
try:
|
||||
return locals()['convert_%s' % ftype](value)
|
||||
except KeyError:
|
||||
|
||||
@@ -69,6 +69,16 @@ class ImportDataSelection(ModelSQL):
|
||||
], 'Selection')
|
||||
|
||||
|
||||
class ImportDataMultiSelection(ModelSQL):
|
||||
"Import Data MultiSelection"
|
||||
__name__ = 'test.import_data.multiselection'
|
||||
multiselection = fields.MultiSelection([
|
||||
('select1', "Select 1"),
|
||||
('select2', "Select 2"),
|
||||
('select3', "Select 3"),
|
||||
], "MultiSelection")
|
||||
|
||||
|
||||
class ImportDataMany2OneTarget(ModelSQL):
|
||||
"Import Data Many2One Target"
|
||||
__name__ = 'test.import_data.many2one.target'
|
||||
@@ -180,6 +190,7 @@ def register(module):
|
||||
ImportDataDateTime,
|
||||
ImportDataTimeDelta,
|
||||
ImportDataSelection,
|
||||
ImportDataMultiSelection,
|
||||
ImportDataMany2OneTarget,
|
||||
ImportDataMany2One,
|
||||
ImportDataMany2ManyTarget,
|
||||
|
||||
@@ -265,6 +265,28 @@ class ImportDataTestCase(unittest.TestCase):
|
||||
Selection.import_data(['selection'], [['select1'], ['select2']]),
|
||||
2)
|
||||
|
||||
@with_transaction()
|
||||
def test_multiselection(self):
|
||||
'Test multiselection'
|
||||
pool = Pool()
|
||||
MultiSelection = pool.get('test.import_data.multiselection')
|
||||
|
||||
for value in ['select1', 'select1,select2', '']:
|
||||
with self.subTest(value=value):
|
||||
self.assertEqual(
|
||||
MultiSelection.import_data(
|
||||
['multiselection'], [[value]]), 1)
|
||||
|
||||
@with_transaction()
|
||||
def test_multiselection_many_rows(self):
|
||||
'Test multiselection many rows'
|
||||
pool = Pool()
|
||||
MultiSelection = pool.get('test.import_data.multiselection')
|
||||
|
||||
self.assertEqual(
|
||||
MultiSelection.import_data(
|
||||
['multiselection'], [['select1'], ['select1,select2']]), 2)
|
||||
|
||||
@with_transaction()
|
||||
def test_many2one(self):
|
||||
'Test many2one'
|
||||
|
||||
Reference in New Issue
Block a user