From cf4026184bb6465d97ff28f7d4e8e49781d813b6 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Mon, 29 Apr 2024 11:07:27 +0200 Subject: 🐛 ODS/Excel imports fix date conversions (refs #5920) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ishtar_common/models_imports.py | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) (limited to 'ishtar_common/models_imports.py') 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:] -- cgit v1.2.3