better related books
[wolnelektury.git] / apps / catalogue / templatetags / catalogue_tags.py
index 583c9c7..2254299 100644 (file)
@@ -345,16 +345,9 @@ def related_books(book, limit=6):
         common_slug=book.common_slug).exclude(pk=book.pk)[:limit])
     limit -= len(related)
     if limit:
-        tagged = Book.tagged.with_any(book.tags).exclude(pk=book.pk)
-        book_tag = book.book_tag()
-        for rel_book in tagged:
-            if book_tag in rel_book.tags.all():
-                continue
-            related += [rel_book]
-            limit -= 1
-            if not limit:
-                break
-
+        related += Book.tagged.related_to(book,
+                Book.objects.exclude(common_slug=book.common_slug),
+                ignore_by_tag=book.book_tag())[:limit]
     return {
         'books': related,
     }