Skip to content

Commit

Permalink
documents: fix abstracts sort
Browse files Browse the repository at this point in the history
The languages of the abstracts that are not in the preferred
sorting languages are added at the end of the list and sorted
alphabetically by language code.

Co-Authored-by: Bertrand Zuchuat <bertrand.zuchuat@rero.ch>
  • Loading branch information
Garfield-fr committed Mar 21, 2022
1 parent 5b04528 commit d86e93e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
19 changes: 16 additions & 3 deletions sonar/modules/documents/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,22 @@ def abstracts(record):
current_i18n.locale.language)
preferred_languages = get_preferred_languages(language)

return sorted(
record['abstracts'],
key=lambda abstract: preferred_languages.index(abstract['language']))
abstractLanguage = []
abstractCode = []
for abstract in record['abstracts']:
if abstract['language'] in preferred_languages:
abstractLanguage.append(abstract)
else:
abstractCode.append(abstract)
abstractSortedLanguage = sorted(
abstractLanguage,
key=lambda abstract: preferred_languages.index(abstract['language'])
)
abstractSortedCode = sorted(
abstractCode,
key=lambda abstract: abstract['language']
)
return abstractSortedLanguage + abstractSortedCode


@blueprint.app_template_filter()
Expand Down
16 changes: 16 additions & 0 deletions tests/ui/documents/test_documents_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,22 @@ def test_abstracts(app):
# No abstract
assert views.abstracts({}) == []

# Abstract with not defined key on preferred languages
abstracts = [{
'language': 'fre',
'value': 'Résumé'
}, {
'language': 'eng',
'value': 'Summary'
}, {
'language': 'roh',
'value': 'Romancio'
}]
abstracts_sort = views.abstracts({'abstracts': abstracts});
assert abstracts_sort[0]['language'] == 'eng'
assert abstracts_sort[1]['language'] == 'fre'
assert abstracts_sort[2]['language'] == 'roh'


def test_contributors():
"""Test ordering contributors."""
Expand Down

0 comments on commit d86e93e

Please sign in to comment.