- extra_where = "catalogue_tag.category NOT IN ('set', 'book')"
- related_tags = models.Tag.objects.related_for_model(tags, model, counts=True, extra={'where': [extra_where]})
- categories = split_tags(related_tags)
-
- if not (theme_is_set or shelf_is_set):
- model=models.Book.objects.filter(parent=None)
+ objects = only_author = pd_counter = categories = None
+
+ if theme_is_set:
+ shelf_tags = [tag for tag in tags if tag.category == 'set']
+ fragment_tags = [tag for tag in tags if tag.category != 'set']
+ fragments = models.Fragment.tagged.with_all(fragment_tags)
+
+ if shelf_tags:
+ books = models.Book.tagged.with_all(shelf_tags).order_by()
+ l_tags = [models.Tag.objects.get(slug = 'l-' + book.slug) for book in books]
+ fragments = models.Fragment.tagged.with_any(l_tags, fragments)
+
+ related_tags = models.Tag.objects.usage_for_queryset(fragments, counts=True,
+ extra={'where': ["catalogue_tag.category != 'book'"]})
+ related_tags = (tag for tag in related_tags if tag not in fragment_tags)
+ categories = split_tags(related_tags)
+
+ objects = fragments
+ else:
+ books = models.Book.tagged.with_all(tags).order_by()
+ l_tags = [models.Tag.objects.get(slug = 'l-' + book.slug) for book in books]
+ book_keys = [book.pk for book in books]
+ related_tags = models.Tag.objects.usage_for_queryset(books, counts=True,
+ extra={'where': ["catalogue_tag.category NOT IN ('set', 'book', 'theme')"]})
+ related_tags = (tag for tag in related_tags if tag not in tags)
+ categories = split_tags(related_tags)
+
+ fragments = models.Fragment.tagged.with_any(l_tags)
+ categories['theme'] = models.Tag.objects.usage_for_queryset(fragments, counts=True,
+ extra={'where': ["catalogue_tag.category = 'theme'"]})
+
+ books = books.exclude(parent__in = book_keys)
+ objects = books
+
+ if not objects:
+ only_author = len(tags) == 1 and tags[0].category == 'author'
+ pd_counter = only_author and tags[0].goes_to_pd()
+ objects = models.Book.objects.none()