From 85b5ddfc0d95f69a3a5b22cd498c04e8c3af2bc6 Mon Sep 17 00:00:00 2001 From: Saurabh Choraria Date: Thu, 14 Sep 2023 15:57:27 +0530 Subject: [PATCH] [FIX] base_import: accept single character in text delimiter When a user tries to import the CSV file with an empty text delimiter or more than one character in text delimiter at that time the traceback will be generated. Steps to reproduce: - Install Accounting module. - Click on import in the bank statement. - 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 - Enter empty text delimiter or more than one character in text delimiter. - Click on the test or Import button. - Error will occur. Error: ValueError: Unsupported file format "text/csv", import only supports CSV, ODS, XLS and XLSX The issue is occurring because text delimiter (options['quoting']) is used as quotechar while reading csv file and quotechar is always a single character string. Check here - https://github.com/odoo/odoo/blob/0fde590bee71618f78e5f954349530bd007c62cf/addons/base_import/models/base_import.py#L494-L497 To solve this issue the length of text delimiter has been checked and if it is not equal to one then a warning is given to the user. sentry-4390461991 closes odoo/odoo#138638 X-original-commit: 1f4aa620779ebe6a0cb7f7ca1910080a258d6fac Signed-off-by: Achraf Ben Azzouz (abz) Signed-off-by: Saurabh Choraria (sauc) --- addons/base_import/i18n/base_import.pot | 7 +++++++ addons/base_import/models/base_import.py | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/addons/base_import/i18n/base_import.pot b/addons/base_import/i18n/base_import.pot index 8bd249014bc..9569385f43d 100644 --- a/addons/base_import/i18n/base_import.pot +++ b/addons/base_import/i18n/base_import.pot @@ -260,6 +260,13 @@ msgstr "" #: code:addons/base_import/models/base_import.py:0 #, python-format msgid "" +"Error while importing records: Text Delimiter should be a single character." +msgstr "" + +#. module: base_import +#: 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." diff --git a/addons/base_import/models/base_import.py b/addons/base_import/models/base_import.py index 48d75aba83a..d4017e97ee3 100644 --- a/addons/base_import/models/base_import.py +++ b/addons/base_import/models/base_import.py @@ -348,6 +348,8 @@ class Import(models.TransientModel): return getattr(self, '_read_' + file_extension)(options) except ValueError as e: raise e + except ImportValidationError as e: + raise e except Exception: _logger.warning("Failed to read file '%s' (transient id %d) using guessed mimetype %s", self.file_name or '', self.id, mimetype) @@ -358,6 +360,8 @@ class Import(models.TransientModel): return getattr(self, '_read_' + file_extension)(options) except ValueError as e: raise e + except ImportValidationError as e: + raise e except Exception: _logger.warning("Failed to read file '%s' (transient id %d) using user-provided mimetype %s", self.file_name or '', self.id, self.file_type) @@ -485,6 +489,9 @@ class Import(models.TransientModel): separator = options['separator'] = candidate break + if not len(options['quoting']) == 1: + raise ImportValidationError(_("Error while importing records: Text Delimiter should be a single character.")) + csv_iterator = pycompat.csv_reader( io.BytesIO(csv_data), quotechar=options['quoting'],