- books = models.Book.tagged.with_all(tags)
- l_tags = [models.Tag.objects.get(slug = 'l-' + book.slug) for book in books]
- book_keys = [book.pk for book in books]
- if book_keys:
- related_tags = models.Book.tags.usage(counts=True,
- filters={'pk__in': book_keys},
- extra={'where': ["catalogue_tag.category NOT IN ('set', 'book', 'theme')"]})
- categories = split_tags(related_tags)
-
- fragment_keys = [fragment.pk for fragment in models.Fragment.tagged.with_any(l_tags)]
- if fragment_keys:
- categories['theme'] = models.Fragment.tags.usage(counts=True,
- filters={'pk__in': fragment_keys},
- extra={'where': ["catalogue_tag.category = 'theme'"]})
-
- books = books.exclude(parent__in = book_keys)
- objects = books
+ # get relevant books and their tags
+ objects = models.Book.tagged.with_all(tags).order_by()
+ l_tags = [book.book_tag() for book in objects]
+ # eliminate descendants
+ descendants_keys = [book.pk for book in models.Book.tagged.with_any(l_tags)]
+ if descendants_keys:
+ objects = objects.exclude(pk__in=descendants_keys)