From 60a6d5f3904bcb240610b459d3bc31108165287e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Mon, 4 Apr 2022 00:11:26 +0200 Subject: [PATCH] Do not import empty One2Many data issue11308 review388081002 --- CHANGELOG | 1 + trytond/model/modelstorage.py | 1 + trytond/tests/import_data.py | 27 ++++++++++++++++++ trytond/tests/test_importdata.py | 49 ++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index b862cbe25a..e5e05863ca 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +* Do not import empty One2Many data * Deduplicate entries in domain inversion's simplify * Prevent to create and delete singleton * Allow CORS on root path diff --git a/trytond/model/modelstorage.py b/trytond/model/modelstorage.py index 42cff8b06c..ac21b59cc0 100644 --- a/trytond/model/modelstorage.py +++ b/trytond/model/modelstorage.py @@ -1046,6 +1046,7 @@ class ModelStorage(Model): i += max2 nbrmax = max(nbrmax, i) row[field] = [] + create = [v for v in create if any(v.values())] if create: row[field].append(('create', create)) if write: diff --git a/trytond/tests/import_data.py b/trytond/tests/import_data.py index ce74ba004d..6c7a333436 100644 --- a/trytond/tests/import_data.py +++ b/trytond/tests/import_data.py @@ -117,6 +117,30 @@ class ImportDataOne2ManyTarget(ModelSQL): one2many = fields.Many2One('test.import_data.one2many', 'One2Many') +class ImportDataOne2Manies(ModelSQL): + "Import Data One2Manies" + __name__ = 'test.import_data.one2manies' + name = fields.Char("Name") + one2many1 = fields.One2Many( + 'test.import_data.one2many1.target', 'one2many', "One2Many 1") + one2many2 = fields.One2Many( + 'test.import_data.one2many2.target', 'one2many', "One2Many 2") + + +class ImportDataOne2Many1Target(ModelSQL): + "Import Data One2Many 1 Target" + __name__ = 'test.import_data.one2many1.target' + name = fields.Char("Name") + one2many = fields.Many2One('test.import_data.one2manies', "One2Many") + + +class ImportDataOne2Many2Target(ModelSQL): + "Import Data One2Many 2 Target" + __name__ = 'test.import_data.one2many2.target' + name = fields.Char("Name") + one2many = fields.Many2One('test.import_data.one2manies', "One2Many") + + class ImportDataBinary(ModelSQL): "Import Data Binary" __name__ = 'test.import_data.binary' @@ -163,6 +187,9 @@ def register(module): ImportDataMany2ManyRelation, ImportDataOne2Many, ImportDataOne2ManyTarget, + ImportDataOne2Manies, + ImportDataOne2Many1Target, + ImportDataOne2Many2Target, ImportDataReferenceSelection, ImportDataReference, ImportDataBinary, diff --git a/trytond/tests/test_importdata.py b/trytond/tests/test_importdata.py index 477b7e14f8..0056ed0e71 100644 --- a/trytond/tests/test_importdata.py +++ b/trytond/tests/test_importdata.py @@ -397,6 +397,55 @@ class ImportDataTestCase(unittest.TestCase): ['', 'Test 2'], ['Test 2', 'Test 1']]), 2) + @with_transaction() + def test_many_one2many(self): + "Test many one2many" + pool = Pool() + One2many = pool.get('test.import_data.one2manies') + + self.assertEqual(One2many.import_data( + ['name', 'one2many1/name', 'one2many2/name'], + [["Test", "Test 1", "Test 2"], + ['', '', "Test 3"]]), 1) + + record, = One2many.search([]) + + self.assertEqual(len(record.one2many1), 1) + self.assertEqual(len(record.one2many2), 2) + + @with_transaction() + def test_many_one2many_empty_last(self): + "Test many one2many with empty last" + pool = Pool() + One2many = pool.get('test.import_data.one2manies') + + self.assertEqual(One2many.import_data( + ['name', 'one2many1/name', 'one2many2/name'], + [["Test", "Test 1", "Test 2"], + ['', "Test 3", '']]), 1) + + record, = One2many.search([]) + + self.assertEqual(len(record.one2many1), 2) + self.assertEqual(len(record.one2many2), 1) + + @with_transaction() + def test_many_one2many_multiple_empty(self): + "Test many one2many with multiple empty" + pool = Pool() + One2many = pool.get('test.import_data.one2manies') + + self.assertEqual(One2many.import_data( + ['name', 'one2many1/name', 'one2many2/name'], + [["Test", "Test 1", "Test 2"], + ['', "Test 3", ''], + ['', '', "Test 4"]]), 1) + + record, = One2many.search([]) + + self.assertEqual(len(record.one2many1), 2) + self.assertEqual(len(record.one2many2), 2) + @with_transaction() def test_reference(self): 'Test reference'