Skip to content

Commit

Permalink
#540 bugfix missing labels (#554)
Browse files Browse the repository at this point in the history
  • Loading branch information
roefem committed Jun 8, 2020
1 parent ac19a5e commit c005b0f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
14 changes: 10 additions & 4 deletions atramhasis/views/views.py
Expand Up @@ -28,6 +28,13 @@ def get_definition(notes):
return note.note


def sort_by_labels(concepts, locale, reverse=False):
return sorted([x for x in concepts if x.label(locale)],
reverse=reverse,
key=lambda child: child.label(locale).label.lower()
) + [x for x in concepts if not x.label(locale)]


@view_defaults(accept='text/html')
class AtramhasisView(object):
"""
Expand Down Expand Up @@ -269,8 +276,7 @@ def get_scheme(self, scheme, locale):
tco = self.conceptscheme_manager.get_concepts_for_scheme_tree(conceptscheme_id)
tcl = self.conceptscheme_manager.get_collections_for_scheme_tree(conceptscheme_id)

scheme_tree = sorted(tco, key=lambda child: child.label(locale).label.lower()) + \
sorted(tcl, key=lambda child: child.label(locale).label.lower())
scheme_tree = sort_by_labels(tco, locale) + sort_by_labels(tcl, locale)

return scheme_tree

Expand All @@ -284,13 +290,13 @@ def parse_thing(self, thing, parent_tree_id):
cs = [c for c in thing.narrower_concepts]
cs = cs + [c for c in thing.narrower_collections]

sortedcs = sorted(cs, key=lambda child: child.label(locale).label.lower())
sortedcs = sort_by_labels(cs, locale)
children = [self.parse_thing(c, tree_id) for index, c in enumerate(sortedcs, 1)]
dict_thing = {
'id': tree_id,
'concept_id': thing.concept_id,
'type': thing.type,
'label': thing.label(locale).label,
'label': thing.label(locale).label if thing.label(locale) else None,
'children': children
}

Expand Down
19 changes: 19 additions & 0 deletions tests/__init__.py
Expand Up @@ -94,6 +94,20 @@ def fill_db():
import_provider(data.geo,
ConceptScheme(id=2, uri='urn:x-vioe:geography'),
session)
import_provider(
DictionaryProvider(
{'id': 'MISSING_LABEL', 'default_language': 'nl'},
[{'id': '1', 'uri': 'urn:x-skosprovider:test/1'},
{
'id': '2',
'uri': 'urn:x-skosprovider:test/2',
'labels': [
{'type': 'prefLabel', 'language': 'nl', 'label': 'label'}
],
}]
),
ConceptScheme(id=9, uri='urn:x-vioe:test'),
session)
session.add(ConceptScheme(id=3, uri='urn:x-vioe:styles'))
for scheme_id in (5, 6, 7, 8):
session.add(
Expand Down Expand Up @@ -182,10 +196,15 @@ def create_registry(request):
[data.larch, data.chestnut, data.species],
concept_scheme=ConceptScheme('http://id.trees.org')
)
missing_label = SQLAlchemyProvider(
{'id': 'MISSING_LABEL', 'conceptscheme_id': 9},
request.db
)

registry.register_provider(trees)
registry.register_provider(geo)
registry.register_provider(styles)
registry.register_provider(materials)
registry.register_provider(test)
registry.register_provider(missing_label)
return registry
8 changes: 8 additions & 0 deletions tests/test_functional.py
Expand Up @@ -512,6 +512,14 @@ def test_tree(self):
self.assertEqual(2, len(response.json))
self.assertEqual('World', response.json[0]['label'])

def test_missing_labels(self):
response = self.testapp.get('/conceptschemes/MISSING_LABEL/tree?_LOCALE_=nl', headers=self._get_default_headers())
self.assertEqual('200 OK', response.status)
self.assertIsNotNone(response.json)
self.assertEqual(2, len(response.json))
self.assertEqual('label', response.json[0]['label'])
self.assertEqual(None, response.json[1]['label'])

def test_no_tree(self):
response = self.testapp.get('/conceptschemes/FOO/tree?_LOCALE_=nl', headers=self._get_default_headers(),
status=404, expect_errors=True)
Expand Down

0 comments on commit c005b0f

Please sign in to comment.