Edition should have only one title, so extra titles will be merged to other_title

This commit is contained in:
Your Name 2025-01-18 11:47:05 -05:00 committed by Henri Dickson
parent fcdd19e037
commit 7b3d1d8fa9
4 changed files with 40 additions and 6 deletions

View file

@ -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())

View file

@ -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__"

View file

@ -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):

@ -1 +1 @@
Subproject commit 5c09b76a4a8ce8bf588a396bf89709785ebcaf6f
Subproject commit bf2872547f95fd6dd8b5fea08682f3eb63ef8c99