Fixes loading inventaire data by language code

This commit is contained in:
Mouse Reeve 2021-04-30 12:50:35 -07:00
parent b4c155f134
commit 485d20696b
3 changed files with 26 additions and 2 deletions

View file

@ -217,6 +217,10 @@ def dict_from_mappings(data, mappings):
the subclass"""
result = {}
for mapping in mappings:
# sometimes there are multiple mappings for one field, don't
# overwrite earlier writes in that case
if mapping.local_field in result and result[mapping.local_field]:
continue
result[mapping.local_field] = mapping.get_value(data)
return result

View file

@ -21,6 +21,7 @@ class Connector(AbstractConnector):
]
self.book_mappings = [
Mapping("title", remote_field="wdt:P1476", formatter=get_first),
Mapping("title", remote_field="labels", formatter=get_language_code),
Mapping("subtitle", remote_field="wdt:P1680", formatter=get_first),
Mapping("inventaireId", remote_field="uri"),
Mapping(
@ -211,4 +212,8 @@ class Connector(AbstractConnector):
def get_language_code(options, code="en"):
"""when there are a bunch of translation but we need a single field"""
return options.get(code)
result = options.get(code)
if result:
return result
values = list(options.values())
return values[0] if values else None

View file

@ -5,7 +5,7 @@ from django.test import TestCase
import responses
from bookwyrm import models
from bookwyrm.connectors.inventaire import Connector
from bookwyrm.connectors.inventaire import Connector, get_language_code
class Inventaire(TestCase):
@ -156,3 +156,18 @@ class Inventaire(TestCase):
formatted.cover,
"https://covers.inventaire.io/img/entities/12345",
)
def test_get_language_code(self):
""" get english or whatever is in reach """
options = {
"de": "bip",
"en": "hi",
"fr": "there",
}
self.assertEqual(get_language_code(options), "hi")
options = {
"fr": "there",
}
self.assertEqual(get_language_code(options), "there")
self.assertIsNone(get_language_code({}))