[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) <abz@odoo.com>
Signed-off-by: Saurabh Choraria (sauc) <sauc@odoo.com>
This commit is contained in:
Saurabh Choraria
2023-10-13 14:14:05 +00:00
parent 8bf49da5a7
commit 85b5ddfc0d
2 changed files with 14 additions and 0 deletions
+7
View File
@@ -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."
+7
View File
@@ -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 '<unknown>', 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 '<unknown>', 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'],