summaryrefslogtreecommitdiff
path: root/ishtar_common
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2018-10-31 13:57:07 +0100
committerÉtienne Loks <etienne.loks@iggdrasil.net>2018-10-31 13:57:07 +0100
commit6e7988a67871bf806fd7044e030aeeeffbc7567d (patch)
tree95cd521ca0d79a5b04625462af93fbe387bb8436 /ishtar_common
parent6ef3532a6e3cfba73d225f3e6de7a92d228d6f95 (diff)
downloadIshtar-6e7988a67871bf806fd7044e030aeeeffbc7567d.tar.bz2
Ishtar-6e7988a67871bf806fd7044e030aeeeffbc7567d.zip
Move dict data
Diffstat (limited to 'ishtar_common')
-rw-r--r--ishtar_common/tests.py63
-rw-r--r--ishtar_common/utils.py53
2 files changed, 115 insertions, 1 deletions
diff --git a/ishtar_common/tests.py b/ishtar_common/tests.py
index 90a9d10ad..65a5a7593 100644
--- a/ishtar_common/tests.py
+++ b/ishtar_common/tests.py
@@ -45,7 +45,7 @@ from django.test.runner import DiscoverRunner
from ishtar_common import models
from ishtar_common import views
from ishtar_common.apps import admin_site
-from ishtar_common.utils import post_save_point
+from ishtar_common.utils import post_save_point, update_data, move_dict_data
COMMON_FIXTURES = [
@@ -93,6 +93,67 @@ class TestCase(BaseTestCase):
pass
+class UtilsTest(TestCase):
+ def test_update_data(self):
+ data_1 = {
+ 'old':
+ {'youpi':
+ {'plouf': u'tralalalère'}}
+ }
+ data_2 = {
+ 'tsoin_tsoin': 'hop',
+ 'old':
+ {'hoppy': 'hop'}
+ }
+ expected = {
+ 'tsoin_tsoin': 'hop',
+ 'old':
+ {'youpi':
+ {'plouf': u'tralalalère'},
+ 'hoppy': 'hop'}
+ }
+ res = update_data(data_1, data_2)
+ self.assertEqual(res, expected)
+
+ def test_move_dict_data(self):
+ data = {
+ 'old': {u'daté': "value"}
+ }
+ expected = {
+ 'old': {'date': "value"}
+ }
+ res = move_dict_data(
+ data, u'data__old__daté', u"data__old__date")
+ self.assertEqual(res, expected)
+ data = {
+ '': {'hop': "value"}
+ }
+ expected = {
+ 'hop': "value",
+ '': {}
+ }
+ res = move_dict_data(data, u'data____hop', u"data__hop")
+ self.assertEqual(res, expected)
+ data = {
+ 'old': {
+ 'traitement': {
+ u'constat_état': 'aaa'
+ }
+ }
+ }
+ expected = {
+ 'old': {
+ 'traitement': {
+ 'constat_etat': 'aaa'
+ }
+ }
+ }
+ res = move_dict_data(data,
+ u'data__old__traitement__constat_état',
+ u'data__old__traitement__constat_etat')
+ self.assertEqual(res, expected)
+
+
class CommandsTestCase(TestCase):
fixtures = [settings.ROOT_PATH +
'../ishtar_common/fixtures/test_towns.json']
diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py
index fb09c5876..4f968af31 100644
--- a/ishtar_common/utils.py
+++ b/ishtar_common/utils.py
@@ -163,6 +163,59 @@ def update_data(data_1, data_2):
return res
+def move_dict_data(data, key1, key2):
+ """
+ Move key1 value to key2 value in a data dict
+ :param data: data dict (with subdicts)
+ :param key1: key to move (with __ notation for hierarchy - begining with
+ "data__")
+ :param key2: target key (with __ notation for hierarchy - begining with
+ "data__")
+ :return: result data
+ """
+ keys1 = key1.split('__')
+ keys2 = key2.split('__')
+ value = data
+ for idx, key in enumerate(keys1):
+ if not idx:
+ if key != 'data':
+ return data
+ continue
+ if key not in value:
+ return data
+ if idx == (len(keys1) - 1): # last
+ value = value.pop(key) # remove from data
+ else:
+ value = value[key]
+
+ new_value = data
+ for idx, key in enumerate(keys2):
+ if not idx:
+ if key != 'data':
+ return data
+ continue
+ if idx == (len(keys2) - 1): # last
+ new_value[key] = value
+ else:
+ if key not in new_value:
+ new_value[key] = {}
+ new_value = new_value[key]
+ return data
+
+
+def clean_empty_data(data):
+ """
+ Clean empty branches of a data dict
+ """
+ for key in data.keys():
+ if data[key] in [{}, None, u""]:
+ data.pop(key)
+ continue
+ if isinstance(data[key], dict):
+ data[key] = clean_empty_data(data[key])
+ return data
+
+
class MultiValueDict(BaseMultiValueDict):
def get(self, *args, **kwargs):
v = super(MultiValueDict, self).getlist(*args, **kwargs)