diff --git a/catalog/book/models.py b/catalog/book/models.py index 40b94e56..d0795bdd 100644 --- a/catalog/book/models.py +++ b/catalog/book/models.py @@ -293,6 +293,22 @@ class Edition(Item): if work and work not in self.works.all(): self.works.add(work) + def merge_data_from_external_resource( + self, p: "ExternalResource", ignore_existing_content: bool = False + ): + super().merge_data_from_external_resource(p, ignore_existing_content) + # Edition should have only one title, so extra titles will be merged to other_title + if len(self.localized_title) <= 1: + return + titles = self.localized_title + self.localized_title = [] + for t in titles: + if isinstance(t, dict) and t.get("text"): + if len(self.localized_title) == 0: + self.localized_title = [t] + elif t["text"] not in self.other_title: + self.other_title += [t["text"]] # type: ignore + @property def sibling_items(self): works = list(self.works.all()) diff --git a/catalog/book/tests.py b/catalog/book/tests.py index 1155b71f..3eec1250 100644 --- a/catalog/book/tests.py +++ b/catalog/book/tests.py @@ -1,19 +1,32 @@ from django.test import TestCase -from catalog.book.models import * from catalog.book.utils import * from catalog.common import * +from catalog.models import * class BookTestCase(TestCase): databases = "__all__" def setUp(self): - hyperion = Edition.objects.create(title="Hyperion") + hyperion = Edition.objects.create( + title="Hyperion", localized_title=[{"lang": "en", "text": "Hyperion"}] + ) hyperion.pages = 500 hyperion.isbn = "9780553283686" hyperion.save() # hyperion.isbn10 = '0553283685' + self.hbla = ExternalResource.objects.create( + item=hyperion, + id_type=IdType.Goodreads, + id_value="77566", + metadata={ + "localized_title": [ + {"lang": "en", "text": "Hyperion"}, + {"lang": "zh", "text": "海伯利安"}, + ] + }, + ) def test_url(self): hyperion = Edition.objects.get(title="Hyperion") @@ -57,6 +70,12 @@ class BookTestCase(TestCase): self.assertEqual(hyperion.isbn, "9780575099432") self.assertEqual(hyperion.isbn10, "0575099437") + def test_merge_external_resources(self): + hyperion = Edition.objects.get(title="Hyperion") + hyperion.merge_data_from_external_resource(self.hbla) + self.assertEqual(hyperion.localized_title, [{"lang": "en", "text": "Hyperion"}]) + self.assertEqual(hyperion.other_title, ["海伯利安"]) + class WorkTestCase(TestCase): databases = "__all__" diff --git a/catalog/management/commands/catalog.py b/catalog/management/commands/catalog.py index c37b3946..fa9d4765 100644 --- a/catalog/management/commands/catalog.py +++ b/catalog/management/commands/catalog.py @@ -3,9 +3,8 @@ from django.core.management.base import BaseCommand from django.db.models import Count, F from tqdm import tqdm -from catalog.book.tests import uniq -from catalog.models import * -from common.models.lang import detect_language +from catalog.models import Edition, Item, Podcast, TVSeason, TVShow +from common.models import detect_language, uniq class Command(BaseCommand): diff --git a/neodb-takahe b/neodb-takahe index 5c09b76a..bf287254 160000 --- a/neodb-takahe +++ b/neodb-takahe @@ -1 +1 @@ -Subproject commit 5c09b76a4a8ce8bf588a396bf89709785ebcaf6f +Subproject commit bf2872547f95fd6dd8b5fea08682f3eb63ef8c99