From 9945b83c5184a1c794f0e8b7ae854ff1c6a812b0 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Fri, 5 Aug 2022 13:24:20 +0200 Subject: Imports: CSV check fix encoding check --- ishtar_common/forms_common.py | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) (limited to 'ishtar_common/forms_common.py') diff --git a/ishtar_common/forms_common.py b/ishtar_common/forms_common.py index 2ec6822b8..e4f432b4f 100644 --- a/ishtar_common/forms_common.py +++ b/ishtar_common/forms_common.py @@ -273,24 +273,22 @@ class NewImportForm(BaseImportForm): raise forms.ValidationError( _("\"Associated images\" field must be a valid zip file.") ) - - return data - - def clean_imported_file(self): - value = self.cleaned_data.get("imported_file", None) - if value: + imported_file = self.cleaned_data.get("imported_file", None) + encoding = self.cleaned_data.get("encoding", None) + if imported_file and encoding: try: - assert value.name.lower().endswith(".csv") - value.seek(0) - reader = csv.reader(StringIO(value.read().decode('utf-8'))) + assert imported_file.name.lower().endswith(".csv") + imported_file.seek(0) + reader = csv.reader(StringIO(imported_file.read().decode(encoding))) for __ in reader: break - value.seek(0) - except (AssertionError, UnicodeDecodeError): + imported_file.seek(0) + except (AssertionError, UnicodeDecodeError) as e: raise forms.ValidationError( - _("This is not a valid CSV file.") + _("This is not a valid CSV file. Check file format and encoding.") ) - return value + + return data def clean_imported_images_link(self): value = self.cleaned_data.get("imported_images_link", None) -- cgit v1.2.3