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 @@
- {{ item.title }} + {{ item.display_title }} {% if not hide_category %}[{{ item.category.label }}]{% endif %} diff --git a/catalog/templates/_item_card_metadata_base.html b/catalog/templates/_item_card_metadata_base.html index 94921873..eff4b653 100644 --- a/catalog/templates/_item_card_metadata_base.html +++ b/catalog/templates/_item_card_metadata_base.html @@ -29,7 +29,7 @@ {% endif %} {% if item.parent_item %} - {% trans "part of" %} {{ item.parent_item.type.label }}: {{ item.parent_item.title }} + {% trans "part of" %} {{ item.parent_item.type.label }}: {{ item.parent_item.display_title }} {% endif %}
diff --git a/catalog/templates/_item_card_metadata_edition.html b/catalog/templates/_item_card_metadata_edition.html index 2ed49ac5..f6a01a95 100644 --- a/catalog/templates/_item_card_metadata_edition.html +++ b/catalog/templates/_item_card_metadata_edition.html @@ -26,7 +26,7 @@ {% comment %} {% include '_people.html' with people=item.language role='' max=5 %} {% endcomment %} {% include '_people.html' with people=item.platform role='' max=5 %} {% if item.show %} - {{ item.show.type.label }}: {{ item.show.title }} + {{ item.show.type.label }}: {{ item.show.display_title }} {% endif %}
{% endblock brief %} diff --git a/catalog/templates/_item_comments.html b/catalog/templates/_item_comments.html index 6adfc17d..bf9be3db 100644 --- a/catalog/templates/_item_comments.html +++ b/catalog/templates/_item_comments.html @@ -40,8 +40,8 @@ data-cover="{{ comment.item.cover_url|default:item.cover.url }}" class="episode" data-hosts="{{ item.hosts|join:' / ' }}" - data-title="{{ comment.item.title }}" - data-album="{{ item.title }}" + data-title="{{ comment.item.display_title }}" + data-album="{{ item.display_title }}" {% if request.user.is_authenticated %} data-comment-href="{% url 'journal:comment' comment.item.uuid %}" {% endif %} data-uuid="{{ comment.item.uuid }}"> @@ -65,7 +65,7 @@ {% if comment.item != item %} - {{ comment.item.title }}{{ comment.item.title_deco }} + {{ comment.item.display_title }}{{ comment.item.title_deco }} {% endif %}
diff --git a/catalog/templates/_item_comments_by_episode.html b/catalog/templates/_item_comments_by_episode.html index fccb1a25..4133af57 100644 --- a/catalog/templates/_item_comments_by_episode.html +++ b/catalog/templates/_item_comments_by_episode.html @@ -67,8 +67,10 @@ {{ comment.mark.action_label }} - {% if comment.focus_item %}{{ comment.focus_item.title }}{% endif %} - {% if comment.item != item %}{{ comment.item.title }}{% endif %} + {% if comment.focus_item %} + {{ comment.focus_item.display_title }} + {% endif %} + {% if comment.item != item %}{{ comment.item.display_title }}{% endif %}
{{ comment.html|safe }}
{% else %} diff --git a/catalog/templates/_item_reviews.html b/catalog/templates/_item_reviews.html index 69ce5006..bf806c2f 100644 --- a/catalog/templates/_item_reviews.html +++ b/catalog/templates/_item_reviews.html @@ -26,7 +26,7 @@ {% trans "review" %} {% if review.item != item %} - {{ review.item.title }}{{ comment.item.title_deco }} + {{ review.item.display_title }}{{ comment.item.title_deco }} {% endif %}
diff --git a/catalog/templates/_item_user_pieces.html b/catalog/templates/_item_user_pieces.html index b5c4810e..af22562f 100644 --- a/catalog/templates/_item_user_pieces.html +++ b/catalog/templates/_item_user_pieces.html @@ -56,7 +56,7 @@ {% comment %} {{ comment.created_time|date }} {% endcomment %}

- {{ 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 @@ {% endif %} {% endfor %} @@ -83,7 +83,7 @@

{% for ep in item.child_items %} - {{ ep.episode_number|default:ep.title|default:"#" }} + {{ ep.episode_number|default:ep.display_title|default:"#" }} {% endfor %}

@@ -226,7 +226,7 @@ {% trans "This edition belongs to the following work" %} {% for i in item.works.all %}
  • - {{ i.title }} + {{ i.display_title }}
  • {% endfor %}
    {{ site_name }} - {% if form.instance.id %} - {% trans 'Edit' %} {{ form.instance.title }} + {% trans 'Edit' %} {{ form.instance.display_title }} {% else %} {% trans 'Create' %} {% endif %} diff --git a/catalog/templates/catalog_history.html b/catalog/templates/catalog_history.html index 7d14f137..bd090cea 100644 --- a/catalog/templates/catalog_history.html +++ b/catalog/templates/catalog_history.html @@ -6,7 +6,7 @@ <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> - <title>{{ site_name }} - {{ item.title }} - {% trans "revision history" %} + {{ site_name }} - {{ item.display_title }} - {% trans "revision history" %} {% include "common_libs.html" %}