diff options
Diffstat (limited to 'ishtar_common')
-rw-r--r-- | ishtar_common/models_imports.py | 29 | ||||
-rw-r--r-- | ishtar_common/utils.py | 4 |
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: |