summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2024-04-29 11:07:27 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2024-04-29 11:07:27 +0200
commitcf4026184bb6465d97ff28f7d4e8e49781d813b6 (patch)
tree60d4c85a151d7173c7224a0572be99f787bc1ab6
parent8f33cc4c4b5f2e86fccbba180f660472a8be93cb (diff)
downloadIshtar-cf4026184bb6465d97ff28f7d4e8e49781d813b6.tar.bz2
Ishtar-cf4026184bb6465d97ff28f7d4e8e49781d813b6.zip
🐛 ODS/Excel imports fix date conversions (refs #5920)
-rw-r--r--ishtar_common/models_imports.py29
-rw-r--r--ishtar_common/utils.py4
2 files changed, 27 insertions, 6 deletions
diff --git a/ishtar_common/models_imports.py b/ishtar_common/models_imports.py
index a37fd2afe..97a6aaf12 100644
--- a/ishtar_common/models_imports.py
+++ b/ishtar_common/models_imports.py
@@ -68,7 +68,7 @@ from ishtar_common.model_managers import SlugModelManager
from ishtar_common.utils import (
create_slug,
- format_int_float,
+ np_format_int_float,
generate_dict_from_list,
get_all_related_m2m_objects_with_model,
get_session_var,
@@ -2182,8 +2182,10 @@ class Import(BaseImport):
if not imported_file_path.startswith(media_root):
return
try:
- data = pandas.read_excel(imported_file_path,
- sheet_name=(self.importer_type.tab_number or 1) - 1)
+ data = pandas.read_excel(
+ imported_file_path,
+ sheet_name=(self.importer_type.tab_number or 1) - 1,
+ )
except Exception:
return
data = data.dropna(how="all") # drop empty rows
@@ -2195,7 +2197,26 @@ class Import(BaseImport):
last_column = max(col_numbers)
filename = ".".join(imported_file_path.split('.')[:-1]) + f"-{random.randint(1, 10000):05d}.csv"
- data.to_csv(filename, index=False, columns=data.columns[range(last_column)], float_format=format_int_float)
+ data.to_csv(filename, index=False, columns=data.columns[range(last_column)],
+ float_format=np_format_int_float)
+ # TODO: date_format options do not do the job... Dirty hack
+ DATE = re.compile(
+ r"^(1[0-9]|20|[1-9])\d{2}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01]) 00:00:00"
+ )
+ GET_DATE = re.compile(r"(\d?\d{3})-(\d{2})-(\d{2}) 00:00:00")
+ with tempfile.NamedTemporaryFile("w", delete=False) as tf:
+ w = csv.writer(tf)
+ with open(filename, "r") as f:
+ r = csv.reader(f)
+ for line in r:
+ w.writerow([
+ "-".join(GET_DATE.match(cell).groups())
+ if DATE.match(cell) and GET_DATE.match(cell)
+ else cell for cell in line
+ ])
+ tf.close()
+ shutil.move(tf.name, filename)
+
name = filename[len(media_root):]
if name.startswith(os.sep):
name = name[1:]
diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py
index 08ef84831..92059c195 100644
--- a/ishtar_common/utils.py
+++ b/ishtar_common/utils.py
@@ -1281,9 +1281,9 @@ def _post_save_geo(sender, **kwargs):
return
-def format_int_float(values):
+def np_format_int_float(values):
"""
- Numpy array: format integer with not "."
+ Numpy array: format integer with no "."
"""
new_values = []
for value in values: