Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite ckan.logic.converters.convert_to_extras #2151

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 10 additions & 9 deletions ckan/logic/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@


def convert_to_extras(key, data, errors, context):

# Get the current extras index
current_indexes = [k[1] for k in data.keys()
if len(k) > 1 and k[0] == 'extras']

new_index = max(current_indexes) + 1 if current_indexes else 0

data[('extras', new_index, 'key')] = key[-1]
data[('extras', new_index, 'value')] = data[key]
if ('extras', ) in data:
del data[('extras', )]
# There is no tally for the number of fields converted to extras.
extras = [k for k in data.keys() if k[0] == 'extras' and len(k) > 1]
new_pos = 0
if extras:
extras.sort()
new_pos = extras[-1][-2] + 1 # e.g. ('extras', 5, 'value')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You refactored line 16 into new lines 15-18 and TBH I find the original easier to understand.

data[('extras', new_pos, 'key')] = key[-1]
data[('extras', new_pos, 'value')] = data[key]


def convert_from_extras(key, data, errors, context):
Expand Down
40 changes: 40 additions & 0 deletions ckan/new_tests/logic/test_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,46 @@ def test_convert_to_extras_output_unflattened(self):

eq_(errors, {})

def test_convert_to_extras_with_added_extras_output_unflattened(self):
"""
See https://github.com/ckan/ckan/issues/2150 for origin of this
test.

tl;dr - the existence of the ('extras', ) key/value pair brought about
by customizing a data set with named metadata fields stored as extras
(as outlined in the CKAN docs) interfered with the unflattening
process.
"""

data = {
('foo', ): 'bar',
('baz', ): 'quux',
('extras',): [{'key': 'foo', 'value': u'bar'},
{'key': 'baz', 'value': u'quux'}, ],
('extras', 0, 'key'): u'qwe',
('extras', 0, 'value'): u'qwe',
('extras', 1, 'key'): u'wer',
('extras', 1, 'value'): u'wer',
}
errors = {}
context = {}

converters.convert_to_extras(('foo',), data, errors, context)
converters.convert_to_extras(('baz',), data, errors, context)

eq_(data[('extras', 0, 'key')], 'qwe')
eq_(data[('extras', 0, 'value')], 'qwe')
eq_(data[('extras', 1, 'key')], 'wer')
eq_(data[('extras', 1, 'value')], 'wer')
eq_(data[('extras', 2, 'key')], 'foo')
eq_(data[('extras', 2, 'value')], 'bar')
eq_(data[('extras', 3, 'key')], 'baz')
eq_(data[('extras', 3, 'value')], 'quux')

assert not ('extras',) in data

eq_(errors, {})

def test_convert_to_extras_output_unflattened_with_correct_index(self):

key = ('test_field',)
Expand Down