Coverage for portality/forms/utils.py: 87%

31 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-07-22 15:59 +0100

1def expanded2compact(expanded, join_lists=None, repeat_lists=None): 

2 if join_lists is None: 

3 join_lists = {} 

4 if repeat_lists is None: 

5 repeat_lists = [] 

6 

7 compact = {} 

8 for k, v in expanded.items(): 

9 if isinstance(v, list) and len(v) > 0: 

10 if isinstance(v[0], dict): 

11 i = 0 

12 for entry in v: 

13 sub = expanded2compact(entry) 

14 for k2, v2 in sub.items(): 

15 nk = k + "-" + str(i) + "-" + k2 

16 compact[nk] = v2 

17 i += 1 

18 else: 

19 if k in join_lists: 

20 compact[k] = join_lists[k].join(v) 

21 elif k in repeat_lists: 

22 i = 0 

23 for v2 in v: 

24 compact[k + "-" + str(i)] = v2 

25 i += 1 

26 else: 

27 compact[k] = v 

28 elif isinstance(v, dict): 

29 sub = expanded2compact(v) 

30 for k2, v2 in sub.items(): 

31 nk = k + "-" + k2 

32 compact[nk] = v2 

33 else: 

34 compact[k] = v 

35 return compact