diff --git a/catalog/book/models.py b/catalog/book/models.py index 0f27f568..3fbefe83 100644 --- a/catalog/book/models.py +++ b/catalog/book/models.py @@ -37,6 +37,7 @@ from catalog.common import ( jsondata, ) from catalog.common.models import SCRIPT_CHOICES +from common.models.misc import uniq from .utils import * @@ -235,11 +236,20 @@ class Edition(Item): if target.works.all().exists(): for work in target.works.all(): self.works.add(work) + work.localized_title = uniq(work.localized_title + self.localized_title) + work.save() elif self.works.all().exists(): for work in self.works.all(): target.works.add(work) + work.localized_title = uniq( + work.localized_title + target.localized_title + ) + work.save() else: - Work.objects.create(title=self.title).editions.add(self, target) + work = Work.objects.create(title=self.title) + work.editions.add(self, target) + work.localized_title = self.localized_title + work.save() return True def unlink_from_all_works(self): @@ -290,17 +300,14 @@ class Work(Item): def merge_to(self, to_item: "Work | None"): # type: ignore[reportIncompatibleMethodOverride] super().merge_to(to_item) - if to_item: - for edition in self.editions.all(): - to_item.editions.add(edition) + if not to_item: + return + for edition in self.editions.all(): + to_item.editions.add(edition) self.editions.clear() - if ( - to_item - and self.title != to_item.title - and self.title not in to_item.other_title - ): - to_item.other_title += [self.title] # type: ignore - to_item.save() + to_item.other_title = uniq(to_item.other_title + [self.title]) # type: ignore + to_item.localized_title = uniq(to_item.localized_title + self.localized_title) + to_item.save() def delete(self, using=None, keep_parents=False, soft=True, *args, **kwargs): if soft: diff --git a/catalog/book/tests.py b/catalog/book/tests.py index 5b7e9dfe..19b222b6 100644 --- a/catalog/book/tests.py +++ b/catalog/book/tests.py @@ -84,11 +84,14 @@ class WorkTestCase(TestCase): self.assertFalse(self.hyperion_print.has_related_books()) def test_merge(self): - w1 = Work.objects.create(title="title1") - w2 = Work.objects.create(title="title2") + title1 = [{"lang": "zh", "text": "z"}] + title2 = [{"lang": "en", "text": "e"}] + w1 = Work.objects.create(title="title1", localized_title=title1) + w2 = Work.objects.create(title="title2", localized_title=title2) w2.merge_to(w1) self.assertEqual(w1.title, "title1") self.assertEqual(w1.other_title, ["title2"]) + self.assertEqual(len(w1.localized_title), 2) def test_link(self): self.hyperion_print.link_to_related_book(self.hyperion_ebook) diff --git a/catalog/management/commands/catalog.py b/catalog/management/commands/catalog.py index f06f55b7..e9020784 100644 --- a/catalog/management/commands/catalog.py +++ b/catalog/management/commands/catalog.py @@ -3,8 +3,11 @@ import pprint from django.contrib.contenttypes.models import ContentType 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 journal.models import update_journal_for_merged_item @@ -25,6 +28,11 @@ class Command(BaseCommand): action="store_true", help="purge deleted items", ) + parser.add_argument( + "--localize", + action="store_true", + help="migrate localized title/description", + ) parser.add_argument( "--integrity", action="store_true", @@ -38,8 +46,28 @@ class Command(BaseCommand): self.purge() if options["integrity"]: self.integrity() + if options["localize"]: + self.localize() self.stdout.write(self.style.SUCCESS(f"Done.")) + def localize(self): + for i in tqdm(Item.objects.all()): + localized_title = [{"lang": detect_language(i.title), "text": i.title}] + if hasattr(i, "orig_title") and i.orig_title: # type:ignore + localized_title += [ + { + "lang": detect_language(i.orig_title), # type:ignore + "text": i.orig_title, # type:ignore + } + ] + if hasattr(i, "other_title") and i.other_title: # type:ignore + for title in i.other_title: # type:ignore + localized_title += [{"lang": detect_language(title), "text": title}] + localized_desc = [{"lang": detect_language(i.brief), "text": i.brief}] + i.localized_title = uniq(localized_title) + i.localized_description = localized_desc + i.save(update_fields=["metadata"]) + def purge(self): for cls in Item.__subclasses__(): if self.fix: diff --git a/catalog/movie/models.py b/catalog/movie/models.py index d9d31d96..10a0be6d 100644 --- a/catalog/movie/models.py +++ b/catalog/movie/models.py @@ -44,7 +44,7 @@ class Movie(Item): douban_movie = PrimaryLookupIdDescriptor(IdType.DoubanMovie) METADATA_COPY_LIST = [ - # "title", + "title", "localized_title", "orig_title", # "other_title", diff --git a/catalog/performance/tests.py b/catalog/performance/tests.py index 0c1cc3c1..2fa86229 100644 --- a/catalog/performance/tests.py +++ b/catalog/performance/tests.py @@ -56,7 +56,6 @@ class DoubanDramaTestCase(TestCase): if item is None: raise ValueError() self.assertEqual(item.orig_title, "Iphigenie auf Tauris") - print(item.localized_title) self.assertEqual(len(item.localized_title), 3) self.assertEqual(item.opening_date, "1974-04-21") self.assertEqual(item.choreographer, ["Pina Bausch"]) @@ -94,7 +93,7 @@ class DoubanDramaTestCase(TestCase): ) self.assertEqual(len(resource.related_resources), 4) crawl_related_resources_task(resource.id) # force the async job to run now - productions = list(item.productions.all().order_by("title")) + productions = sorted(list(item.productions.all()), key=lambda p: p.opening_date) self.assertEqual(len(productions), 4) self.assertEqual( productions[3].actor, diff --git a/catalog/templates/_item_card.html b/catalog/templates/_item_card.html index 23e49963..d90b341d 100644 --- a/catalog/templates/_item_card.html +++ b/catalog/templates/_item_card.html @@ -4,7 +4,7 @@
- {{ comment.item.title }}: {{ comment.html|safe }} + {{ comment.item.display_title }}: {{ comment.html|safe }}
{% endfor %} diff --git a/catalog/templates/_sidebar_edit.html b/catalog/templates/_sidebar_edit.html index 0e19722d..53129333 100644 --- a/catalog/templates/_sidebar_edit.html +++ b/catalog/templates/_sidebar_edit.html @@ -42,7 +42,7 @@