From c5e0dd54322f8969e4b2d8a9b8c7125d35b82f39 Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 9 Apr 2024 17:22:21 -0400 Subject: [PATCH] show comments from related books --- catalog/book/models.py | 14 +++++++------- catalog/common/models.py | 8 ++++++++ catalog/templates/edition.html | 2 +- catalog/templates/item_base.html | 15 --------------- catalog/templates/work.html | 14 +++++++++++++- catalog/views.py | 4 ++-- 6 files changed, 31 insertions(+), 26 deletions(-) diff --git a/catalog/book/models.py b/catalog/book/models.py index cce871bb..04beca1f 100644 --- a/catalog/book/models.py +++ b/catalog/book/models.py @@ -198,15 +198,14 @@ class Edition(Item): if work and work not in self.works.all(): self.works.add(work) - def get_related_books(self): + @property + def sibling_items(self): works = list(self.works.all()) return ( Edition.objects.filter(works__in=works) - .distinct() .exclude(pk=self.pk) .exclude(is_deleted=True) .exclude(merged_to_item__isnull=False) - .order_by("title") ) def has_related_books(self): @@ -275,16 +274,17 @@ class Work(Item): return [(i.value, i.label) for i in id_types] def merge_to(self, to_item): + super().merge_to(to_item) + 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] - super().merge_to(to_item) - for edition in self.editions.all(): - to_item.editions.add(edition) - self.editions.clear() + to_item.save() def delete(self, using=None, soft=True, *args, **kwargs): if soft: diff --git a/catalog/common/models.py b/catalog/common/models.py index 9da4040e..80529672 100644 --- a/catalog/common/models.py +++ b/catalog/common/models.py @@ -350,6 +350,14 @@ class Item(SoftDeleteMixin, PolymorphicModel): def parent_uuid(self): return self.parent_item.uuid if self.parent_item else None + @property + def sibling_items(self): + return Item.objects.none() + + @property + def sibling_item_ids(self): + return list(self.sibling_items.values_list("id", flat=True)) + @classmethod def get_ap_object_type(cls): return cls.__name__ diff --git a/catalog/templates/edition.html b/catalog/templates/edition.html index 5e0b21ef..cf80507c 100644 --- a/catalog/templates/edition.html +++ b/catalog/templates/edition.html @@ -110,7 +110,7 @@ _sidebar_auto_collapse(mm); }); - {% with related_books=item.get_related_books %} + {% with related_books=item.sibling_items %} {% if related_books.count > 0 %}
diff --git a/catalog/templates/item_base.html b/catalog/templates/item_base.html index afa62b4b..ac11a861 100644 --- a/catalog/templates/item_base.html +++ b/catalog/templates/item_base.html @@ -64,21 +64,6 @@ {% endif %} {% endfor %} - {% endblock %} -{% block left_sidebar %}{% endblock %} +{% block left_sidebar %} +
+
+ {% trans '本著作包含以下图书版本' %} + {% for b in item.editions.all %} +
+ {{ b.title }} + ({{ b.pub_house | default:'' }} {{ b.pub_year | default:'' }}) +
+ {% endfor %} +
+
+{% endblock %} {% block sidebar %}{% endblock %} diff --git a/catalog/views.py b/catalog/views.py index 4d9e1619..1eccf246 100644 --- a/catalog/views.py +++ b/catalog/views.py @@ -194,7 +194,7 @@ def comments(request, item_path, item_uuid): item = get_object_or_404(Item, uid=get_uuid_or_404(item_uuid)) if not item: raise Http404() - ids = item.child_item_ids + [item.id] + ids = item.child_item_ids + [item.id] + item.sibling_item_ids queryset = Comment.objects.filter(item_id__in=ids).order_by("-created_time") queryset = queryset.filter(q_piece_visible_to_user(request.user)) before_time = request.GET.get("last") @@ -240,7 +240,7 @@ def reviews(request, item_path, item_uuid): item = get_object_or_404(Item, uid=get_uuid_or_404(item_uuid)) if not item: raise Http404() - ids = item.child_item_ids + [item.id] + ids = item.child_item_ids + [item.id] + item.sibling_item_ids queryset = Review.objects.filter(item_id__in=ids).order_by("-created_time") queryset = queryset.filter(q_piece_visible_to_user(request.user)) before_time = request.GET.get("last")