Skip to content

Commit

Permalink
Fixed ValueError: 'value' is not in list in `content.sort_on_vocab_…
Browse files Browse the repository at this point in the history
…order` when a value of given `p_values` does not exist in the given `p_vocab`.
  • Loading branch information
gbastien committed Aug 28, 2023
1 parent c0225b9 commit 6168211
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Changelog
[gbastien]
- Improved `transmogrifier.str_to_date` with min and max
[sgeulette]
- Fixed `ValueError: 'value' is not in list` in `content.sort_on_vocab_order`
when a value of given `p_values` does not exist in the given `p_vocab`.
[gbastien]

0.74 (2023-08-24)
-----------------
Expand Down
14 changes: 12 additions & 2 deletions src/imio/helpers/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -740,10 +740,20 @@ def get_user_fullname(userid, none_if_no_user=False, none_if_unfound=False):


def sort_on_vocab_order(values, vocab=None, obj=None, vocab_name=None):
""" """
"""Sort a given list of values depending on the order of these values
of a given vocabulary.
:param values: the list of values to sort
:param vocab: an optional vocabulary instance
:param obj: when vocab not given, will be used to get vocabulary from vocab_name
:param vocab_name: a vocabulayr name that will be used to get a vocabulary instance
:return: values sorted with order of vocablary terms.
"""
if vocab is None:
vocab = get_vocab(obj, vocab_name)

ordered_values = [term.value for term in vocab._terms]
values_indexes = [ordered_values.index(value) for value in values]
# do not fail a a value of values is not in ordered_values
values_indexes = [ordered_values.index(value) if value in ordered_values else 999
for value in values]
return sort_by_indexes(list(values), values_indexes)
9 changes: 9 additions & 0 deletions src/imio/helpers/tests/test_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,3 +425,12 @@ def test_sort_on_vocab_order(self):
sort_on_vocab_order(('testingtype', 'Link', 'Document', 'File'),
vocab=vocab),
['File', 'Link', 'Document', 'testingtype'])
# not found values are moved to the end
self.assertEqual(
sort_on_vocab_order(('testingtype', 'unknown1', 'Link', 'Document', 'File'),
vocab=vocab),
['File', 'Link', 'Document', 'testingtype', 'unknown1'])
self.assertEqual(
sort_on_vocab_order(('testingtype', 'unknown2', 'Link', 'Document', 'File', 'unknown1'),
vocab=vocab),
['File', 'Link', 'Document', 'testingtype', 'unknown2', 'unknown1'])

0 comments on commit 6168211

Please sign in to comment.