From f893eef973d8af8e7bc4ef4fed76996ff2842dca Mon Sep 17 00:00:00 2001 From: Saurabh Choraria Date: Mon, 7 Aug 2023 13:51:37 +0000 Subject: [PATCH] [FIX] base_import: raise ImportValidationError when invalid seperator selected When a user tries to import the CSV file with a different separator at that time, the values in mapper and rows_to_import are not correctly mapped. So the traceback will be generated. Steps to reproduce: 1. Click on import in the bank statement. 2. Select any CSV file for the bank statement line or can download and import this file https://drive.google.com/file/d/1lnScw4RN6T01pOkyNON8vvb3FQOPiy1O/view?usp=drive_link 3. Select any separator other than a comma. 4. Click on the test or Import button. 5. Error will occur. Error: IndexError: list index out of range. To solve this issue, a row's length is checked with the number of fields. sentry-4021250095 closes odoo/odoo#134596 X-original-commit: 0eb30132c14420d42f88a5f54a81ba1dc51a867c Signed-off-by: Achraf Ben Azzouz (abz) Signed-off-by: Saurabh Choraria (sauc) --- addons/base_import/i18n/base_import.pot | 10 ++++++++++ addons/base_import/models/base_import.py | 4 ++++ 2 files changed, 14 insertions(+) diff --git a/addons/base_import/i18n/base_import.pot b/addons/base_import/i18n/base_import.pot index 38c450f9f48..8bd249014bc 100644 --- a/addons/base_import/i18n/base_import.pot +++ b/addons/base_import/i18n/base_import.pot @@ -255,6 +255,16 @@ msgstr "" msgid "Error at row %s: \"%s\"" msgstr "" +#. module: base_import +#. odoo-python +#: code:addons/base_import/models/base_import.py:0 +#, python-format +msgid "" +"Error while importing records: all rows should be of the same size, but the " +"title row has %d entries while the first row has %d. You may need to change " +"the separator character." +msgstr "" + #. module: base_import #. odoo-javascript #: code:addons/base_import/static/src/import_data_progress/import_data_progress.xml:0 diff --git a/addons/base_import/models/base_import.py b/addons/base_import/models/base_import.py index 660b11da9a3..5f0bd1af8a1 100644 --- a/addons/base_import/models/base_import.py +++ b/addons/base_import/models/base_import.py @@ -1040,6 +1040,10 @@ class Import(models.TransientModel): import_fields = [f for f in fields if f] _file_length, rows_to_import = self._read_file(options) + if len(rows_to_import[0]) != len(fields): + raise ImportValidationError( + _("Error while importing records: all rows should be of the same size, but the title row has %d entries while the first row has %d. You may need to change the separator character.", len(fields), len(rows_to_import[0])) + ) if options.get('has_headers'): rows_to_import = rows_to_import[1:]