diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2023-10-19 13:32:28 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2024-02-05 10:51:52 +0100 |
commit | 0e87aae64761766e8f74328a8e68e079062aa0af (patch) | |
tree | 8a77c4f2afa4fcab3d24604633e8fe5a55a7d56d /ishtar_common/forms_common.py | |
parent | e6dab88f74f0f13ab047b4f0059c7f1e469118f3 (diff) | |
download | Ishtar-0e87aae64761766e8f74328a8e68e079062aa0af.tar.bz2 Ishtar-0e87aae64761766e8f74328a8e68e079062aa0af.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): |