diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2023-10-19 13:32:28 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2024-04-16 16:38:32 +0200 |
commit | 89b1e4a21e80d53be0e28df8723eec36358034f9 (patch) | |
tree | 979c247c80b26c93da7e029657fae3501cbf5f9b /ishtar_common/forms_common.py | |
parent | d71382379f5c806a5b5c8cafd06d7e60564b8d4a (diff) | |
download | Ishtar-89b1e4a21e80d53be0e28df8723eec36358034f9.tar.bz2 Ishtar-89b1e4a21e80d53be0e28df8723eec36358034f9.zip |
✨ imports: manage ods, xls and xlsx
Convert them to CSV and store the resulting file in a specific field.
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): |