diff options
| -rw-r--r-- | ishtar_common/tests.py | 63 | ||||
| -rw-r--r-- | ishtar_common/utils.py | 53 | 
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) | 
