summaryrefslogtreecommitdiff
path: root/ishtar_common/utils.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2023-09-13 14:19:19 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2024-04-16 16:38:32 +0200
commitb1c4e814bdb7ded9314b8d5337fa5841c737d32d (patch)
tree5369c8a52be64aa839194051dfd06713ec94867b /ishtar_common/utils.py
parent030202bb4ec26e10fe54dc761352c0d83bc133d1 (diff)
downloadIshtar-b1c4e814bdb7ded9314b8d5337fa5841c737d32d.tar.bz2
Ishtar-b1c4e814bdb7ded9314b8d5337fa5841c737d32d.zip
✨ Pre-import form: manage import
Diffstat (limited to 'ishtar_common/utils.py')
-rw-r--r--ishtar_common/utils.py47
1 files changed, 30 insertions, 17 deletions
diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py
index 621e96b11..4e50c506c 100644
--- a/ishtar_common/utils.py
+++ b/ishtar_common/utils.py
@@ -274,32 +274,45 @@ def check_model_access_control(request, model, available_perms=None):
return allowed, own
-def update_data(data_1, data_2, merge=False):
+def update_data(data, new_data, merge=False):
"""
Update a data directory taking account of key detail
"""
res = {}
- if not isinstance(data_1, dict) or not isinstance(data_2, dict):
- if data_2 and not data_1:
- return data_2
+ if not isinstance(data, dict) or not isinstance(new_data, dict):
+ if new_data and not data:
+ return new_data
if not merge:
- if data_2:
- return data_2
- return data_1
- if data_2 and data_2 != data_1:
- return data_1 + " ; " + data_2
- return data_1
- for k in data_1:
- if k not in data_2:
- res[k] = data_1[k]
+ if new_data:
+ return new_data
+ return data
+ if new_data and data_2 != data:
+ return data + " ; " + new_data
+ return data
+ for k in data:
+ if k not in new_data:
+ res[k] = data[k]
else:
- res[k] = update_data(data_1[k], data_2[k], merge=merge)
- for k in data_2:
- if k not in data_1:
- res[k] = data_2[k]
+ res[k] = update_data(data[k], new_data[k], merge=merge)
+ for k in new_data:
+ if k not in data:
+ res[k] = new_data[k]
return res
+def generate_dict_from_list(lst: list, value) -> dict:
+ """
+ ("key1", "key2", "key3"), value -> {"key1": {"key2": {"key3": value}}}
+ """
+ dct = {}
+ for key in reversed(lst):
+ if not dct:
+ dct = {key: value}
+ else:
+ dct = {key: dct}
+ return dct
+
+
def move_dict_data(data, key1, key2):
"""
Move key1 value to key2 value in a data dict