diff options
Diffstat (limited to 'ishtar_common/forms_common.py')
-rw-r--r-- | ishtar_common/forms_common.py | 47 |
1 files changed, 30 insertions, 17 deletions
diff --git a/ishtar_common/forms_common.py b/ishtar_common/forms_common.py index e4f432b4f..822efadda 100644 --- a/ishtar_common/forms_common.py +++ b/ishtar_common/forms_common.py @@ -215,6 +215,27 @@ class BaseImportForm(IshtarForm, forms.ModelForm): self.fields["imported_file"].validators = [file_size_validator] self._post_init() + def _clean_csv(self, is_csv=False): + imported_file = self.cleaned_data.get("imported_file", None) + encoding = self.cleaned_data.get("encoding", None) + print(encoding) + if imported_file and encoding: + try: + if not imported_file.name.lower().endswith(".csv"): + if is_csv: + raise AssertionError() + else: + return + imported_file.seek(0) + reader = csv.reader(StringIO(imported_file.read().decode(encoding))) + for __ in reader: + break + imported_file.seek(0) + except (AssertionError, UnicodeDecodeError) as e: + raise forms.ValidationError( + _("This is not a valid CSV file. Check file format and encoding.") + ) + def clean(self): data = self.cleaned_data if ( @@ -273,21 +294,7 @@ class NewImportForm(BaseImportForm): raise forms.ValidationError( _("\"Associated images\" field must be a valid zip file.") ) - imported_file = self.cleaned_data.get("imported_file", None) - encoding = self.cleaned_data.get("encoding", None) - if imported_file and encoding: - try: - assert imported_file.name.lower().endswith(".csv") - imported_file.seek(0) - reader = csv.reader(StringIO(imported_file.read().decode(encoding))) - for __ in reader: - break - imported_file.seek(0) - except (AssertionError, UnicodeDecodeError) as e: - raise forms.ValidationError( - _("This is not a valid CSV file. Check file format and encoding.") - ) - + self._clean_csv(is_csv=True) return data def clean_imported_images_link(self): @@ -333,6 +340,8 @@ class NewImportGISForm(BaseImportForm): "name", "importer_type", "imported_file", + "encoding", + "csv_sep", "associated_group", ) @@ -345,7 +354,7 @@ class NewImportGISForm(BaseImportForm): if value: try: ext = value.name.lower().split(".")[-1] - assert ext in ("zip", "gpkg") + assert ext in ("zip", "gpkg", "csv") if ext == "zip": zip_file = zipfile.ZipFile(value) assert not zip_file.testzip() @@ -353,7 +362,6 @@ class NewImportGISForm(BaseImportForm): for name in zip_file.namelist(): in_ext = name.lower().split(".")[-1] if in_ext in ("shp", "gpkg"): - filename = name has_correct_file = True break assert has_correct_file @@ -363,6 +371,11 @@ class NewImportGISForm(BaseImportForm): ) return value + def clean(self): + data = super().clean() + self._clean_csv() + return data + def save(self, user, commit=True): self.instance.user = user item = super(NewImportGISForm, self).save(commit) |