diff options
Diffstat (limited to 'ishtar_common/forms_common.py')
-rw-r--r-- | ishtar_common/forms_common.py | 31 |
1 files changed, 22 insertions, 9 deletions
diff --git a/ishtar_common/forms_common.py b/ishtar_common/forms_common.py index 5ea90a8d5..dd3a83971 100644 --- a/ishtar_common/forms_common.py +++ b/ishtar_common/forms_common.py @@ -274,16 +274,23 @@ class BaseImportForm(IshtarForm, forms.ModelForm): BAD_CHARS = ["é", "³", "ô", "Ã\xa0", "é"] - def _clean_csv(self, is_csv=False): + def _clean_imported_file(self, types=None): imported_file = self.cleaned_data.get("imported_file", None) + if not imported_file: + return + imported_file_name = imported_file.name.lower() + if types: + if not [1 for tpe in types if imported_file_name.endswith(tpe)]: + if len(types) == 1: + msg = str(_("Bad format. Extension of the file must be {}.")).format(types[0][1:]) + else: + msg = str(_("Bad format. Extension of the file must be: {} or {}.")).format( + ", ".join([tpe[1:] for tpe in types[:-1]]), types[-1][1:] + ) + raise forms.ValidationError(msg) encoding = self.cleaned_data.get("encoding", None) - if imported_file and encoding: + if encoding and imported_file_name.endswith(".csv"): try: - if not imported_file.name.lower().endswith(".csv"): - if is_csv: - raise ValueError() - else: - return imported_file.seek(0) reader = csv.reader(StringIO(imported_file.read().decode(encoding))) idx = 0 @@ -375,7 +382,10 @@ class NewImportForm(BaseImportForm): raise forms.ValidationError( _('"Associated images" field must be a valid zip file.') ) - self._clean_csv(is_csv=True) + types = [".csv"] + if settings.USE_LIBREOFFICE: + types += [".ods", ".xls", ".xlsx"] + self._clean_imported_file(types=types) archive_required = self._need_archive(data) if archive_required and ( not data.get("imported_images", None) @@ -466,7 +476,10 @@ class NewImportGISForm(BaseImportForm): def clean(self): data = super().clean() - self._clean_csv() + types = ["zip", "gpkg", "csv"] + if settings.USE_LIBREOFFICE: + types += [".ods", ".xls", ".xlsx"] + self._clean_imported_file(types=types) return data def save(self, user, commit=True): |