diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2023-09-15 18:46:41 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2024-04-16 16:38:32 +0200 |
commit | 5ec78fa85dbb6b7ec7d286827b6e32f82c489b3e (patch) | |
tree | f4f98c3ef9923df42ff579fbc9b71c807f26716e /ishtar_common/data_importer.py | |
parent | b1c4e814bdb7ded9314b8d5337fa5841c737d32d (diff) | |
download | Ishtar-5ec78fa85dbb6b7ec7d286827b6e32f82c489b3e.tar.bz2 Ishtar-5ec78fa85dbb6b7ec7d286827b6e32f82c489b3e.zip |
♻️ refactoring and optimisation: manage defaults like pre_importer_values - add an explicit field for required value to apply defaults
Diffstat (limited to 'ishtar_common/data_importer.py')
-rw-r--r-- | ishtar_common/data_importer.py | 38 |
1 files changed, 14 insertions, 24 deletions
diff --git a/ishtar_common/data_importer.py b/ishtar_common/data_importer.py index d5e6f966c..8e3c41e62 100644 --- a/ishtar_common/data_importer.py +++ b/ishtar_common/data_importer.py @@ -746,7 +746,6 @@ class Importer(object): UNICITY_KEYS = [] # if set only models inside this list can be created MODEL_CREATION_LIMIT = [] - EXTRA_DEFAULTS = {} DEFAULTS = {} PRE_IMPORT_VALUES = {} # values from a form before the import ERRORS = { @@ -834,12 +833,6 @@ class Importer(object): if import_instance and import_instance.imported_images: self.archive = import_instance.imported_images self._defaults = self.DEFAULTS.copy() - # EXTRA_DEFAULTS are for multiple inheritance - if self.EXTRA_DEFAULTS: - for k in self.EXTRA_DEFAULTS: - if k not in self._defaults: - self._defaults[k] = {} - self._defaults[k].update(self.EXTRA_DEFAULTS[k]) self._pre_import_values = self.PRE_IMPORT_VALUES.copy() self.history_modifier = history_modifier self.output = output @@ -1158,6 +1151,20 @@ class Importer(object): except: # nosec pass + data = update_data(self._pre_import_values, data) + # put default values only if relevant + for mandatory_keys, defaults in self._defaults: + test_dict = data.copy() + nok = False + for k in mandatory_keys: # test have keys + if k not in test_dict: + nok = True + break + test_dict = test_dict[k] + if nok or not any(1 for k in test_dict if test_dict[k]): # test empty dict + continue + data = update_data(defaults, data) + self.validity.append(c_row) if not self.c_errors and (idx_col + 1) < self.min_col_number: self.c_errors = True @@ -1188,7 +1195,6 @@ class Importer(object): default_srs = profile.srs if profile.srs else None if "geodata" in data: - geodata = copy.deepcopy(self._defaults.get(("geodata",), {})) geodata.update(data.pop("geodata")) if default_srs and not [ 1 for k in geodata if k.startswith("spatial_reference_system") and @@ -1197,7 +1203,6 @@ class Importer(object): geodata["spatial_reference_system"] = default_srs if "main_geodata" in data: - main_geodata = copy.deepcopy(self._defaults.get(("main_geodata",), {})) main_geodata.update(data.pop("main_geodata")) if default_srs and not [ 1 for k in main_geodata if k.startswith("spatial_reference_system") @@ -1538,7 +1543,6 @@ class Importer(object): concat=concats[idx], concat_str=concat_str[idx], ) - data = update_data(self._pre_import_values, data) c_row.append(" ; ".join([v for v in c_values])) return data @@ -1582,15 +1586,6 @@ class Importer(object): # contruct many dict for each values default_dict = {} - # # get default values - p = [attribute] - if c_path: - p = list(c_path) + p - p = tuple(p) - if p in self._defaults: - for k in self._defaults[p]: - default_dict[k] = self._defaults[p][k] - # # init with simple values that will be duplicated for key in val.keys(): if type(val[key]) not in (list, tuple): @@ -1896,15 +1891,10 @@ class Importer(object): elif type(create_dict[k]) == File: create_dict[k] = copy.copy(data[k]) - # default values path = tuple(path) defaults = {} if hasattr(cls, "get_import_defaults"): defaults = cls.get_import_defaults() - if path in self._defaults: - for k in self._defaults[path]: - if (k not in data or not data[k]) and self._defaults[path][k]: - defaults[k] = self._defaults[path][k] if "history_modifier" in create_dict: defaults.update({"history_modifier": create_dict.pop("history_modifier")}) |