Skip to content

Commit d32b0fc

Browse files
committed
Update unflattening extras patch
1 parent fb455f1 commit d32b0fc

File tree

1 file changed

+44
-34
lines changed

1 file changed

+44
-34
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,47 @@
1-
diff --git a/ckan/logic/converters.py b/ckan/logic/converters.py
2-
index c87cad49a..a6d99d2a0 100644
3-
--- a/ckan/logic/converters.py
4-
+++ b/ckan/logic/converters.py
5-
@@ -25,14 +25,24 @@ def convert_to_extras(key, data, errors, context):
1+
From 7c3dbf1ccd050a09e6d84872e166411f060b2dc5 Mon Sep 17 00:00:00 2001
2+
From: Ian Ward <[email protected]>
3+
Date: Mon, 21 Sep 2020 13:42:04 -0400
4+
Subject: [PATCH 1/2] [#5602] unflatten: skip empty items regression + test
65

7-
def convert_from_extras(key, data, errors, context):
6+
---
7+
ckan/lib/navl/dictization_functions.py | 14 +++++++++++---
8+
ckan/tests/legacy/lib/test_navl.py | 16 ++++++++++++++++
9+
2 files changed, 27 insertions(+), 3 deletions(-)
810

9-
- def remove_from_extras(data, key):
10-
- to_remove = []
11-
- for data_key, data_value in data.iteritems():
12-
- if (data_key[0] == 'extras'
13-
- and data_key[1] == key):
14-
- to_remove.append(data_key)
15-
- for item in to_remove:
16-
- del data[item]
17-
+ def remove_from_extras(data, idx):
18-
+ for key in sorted(data):
19-
+ if key[0] != 'extras' or key[1] < idx:
20-
+ continue
21-
+ if key[1] == idx:
22-
+ del data[key]
23-
+
24-
+ # Following block required for unflattening extras with
25-
+ # "gaps" created sometimes by `convert_from_extra`
26-
+ # validator :
27-
+ #
28-
+ # {
29-
+ # ('extras', 0, 'key'): 'x',
30-
+ # ('extras', 2, 'key): 'y'
31-
+ # }
32-
+ if key[1] > idx:
33-
+ new_key = (key[0], key[1] - 1) + key[2:]
34-
+ data[new_key] = data.pop(key)
11+
diff --git a/ckan/lib/navl/dictization_functions.py b/ckan/lib/navl/dictization_functions.py
12+
index a4cfc00bc5..314600d377 100644
13+
--- a/ckan/lib/navl/dictization_functions.py
14+
+++ b/ckan/lib/navl/dictization_functions.py
15+
@@ -405,7 +405,7 @@ def unflatten(data):
16+
'''
17+
18+
unflattened = {}
19+
- convert_to_list = []
20+
+ clean_lists = {}
3521

36-
for data_key, data_value in data.iteritems():
37-
if (data_key[0] == 'extras'
22+
for flattend_key in sorted(data.keys(), key=flattened_order_key):
23+
current_pos = unflattened
24+
@@ -414,8 +414,13 @@ def unflatten(data):
25+
try:
26+
current_pos = current_pos[key]
27+
except IndexError:
28+
- new_pos = {}
29+
- current_pos.append(new_pos)
30+
+ while True:
31+
+ new_pos = {}
32+
+ current_pos.append(new_pos)
33+
+ if key < len(current_pos):
34+
+ break
35+
+ # skipped list indexes need to be removed before returning
36+
+ clean_lists[id(current_pos)] = current_pos
37+
current_pos = new_pos
38+
except KeyError:
39+
new_pos = []
40+
@@ -423,6 +428,9 @@ def unflatten(data):
41+
current_pos = new_pos
42+
current_pos[flattend_key[-1]] = data[flattend_key]
43+
44+
+ for cl in clean_lists.values():
45+
+ cl[:] = [i for i in cl if i]
46+
+
47+
return unflattened

0 commit comments

Comments
 (0)