- model = models.Fragment
- only_author = len(tags) == 1 and tags[0].category == 'author'
- pd_counter = only_author and tags[0].goes_to_pd()
-
- user_is_owner = (len(shelf) and request.user.is_authenticated() and request.user == shelf[0].user)
-
- 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)
-
- return newtagging_views.tagged_object_list(
- request,
- tag_model=models.Tag,
- queryset_or_model=model,
- tags=tags,
- template_name='catalogue/tagged_object_list.html',
- extra_context = {
+ 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)
+ areas = PictureArea.tagged.with_all(fragment_tags)
+
+ if shelf_tags:
+ books = models.Book.tagged.with_all(shelf_tags).order_by()
+ l_tags = models.Tag.objects.filter(category='book',
+ slug__in=[book.book_tag_slug() for book in books.iterator()])
+ fragments = models.Fragment.tagged.with_any(l_tags, fragments)
+
+ # newtagging goes crazy if we just try:
+ #related_tags = models.Tag.objects.usage_for_queryset(fragments, counts=True,
+ # extra={'where': ["catalogue_tag.category != 'book'"]})
+
+ related_tags = []
+
+ fragment_keys = [fragment.pk for fragment in fragments.iterator()]
+ if fragment_keys:
+ related_tags = models.Fragment.tags.usage(counts=True,
+ filters={'pk__in': fragment_keys},
+ 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, categories)
+
+ object_queries.insert(0, fragments)
+
+ area_keys = [area.pk for area in areas.iterator()]
+ if area_keys:
+ related_tags = PictureArea.tags.usage(counts=True,
+ filters={'pk__in': area_keys})
+ related_tags = (tag for tag in related_tags if tag not in fragment_tags)
+
+ categories = split_tags(related_tags, categories)
+
+ # we want the Pictures to go first
+ object_queries.insert(0, areas)
+ objects = MultiQuerySet(*object_queries)
+ else:
+ if shelf_is_set:
+ books = models.Book.tagged.with_all(tags).order_by('sort_key_author')
+ else:
+ books = models.Book.tagged_top_level(tags).order_by('sort_key_author')
+
+ pictures = Picture.tagged.with_all(tags).order_by('sort_key_author')
+
+ if books.count() > 0:
+ # get related tags from `tag_counter` and `theme_counter`
+ related_counts = {}
+ tags_pks = [tag.pk for tag in tags]
+ for book in books:
+ for tag_pk, value in itertools.chain(book.tag_counter.iteritems(), book.theme_counter.iteritems()):
+ if tag_pk in tags_pks:
+ continue
+ related_counts[tag_pk] = related_counts.get(tag_pk, 0) + value
+ related_tags = models.Tag.objects.filter(pk__in=related_counts.keys())
+ related_tags = [tag for tag in related_tags if tag not in tags]
+ for tag in related_tags:
+ tag.count = related_counts[tag.pk]
+
+ categories = split_tags(related_tags)
+ del related_tags
+
+ if pictures.count() > 0:
+ related_counts = {}
+ tags_pks = [tag.pk for tag in tags]
+ for picture in pictures:
+ for tag_pk, value in itertools.chain(picture.tag_counter.iteritems(), picture.theme_counter.iteritems()):
+ if tag_pk in tags_pks:
+ continue
+ logging.info("counting tag not in tags_pks: %d", tag_pk)
+ related_counts[tag_pk] = related_counts.get(tag_pk, 0) + value
+ related_tags = models.Tag.objects.filter(pk__in=related_counts.keys())
+ related_tags = [tag for tag in related_tags if tag not in tags]
+ for tag in related_tags:
+ tag.count = related_counts[tag.pk]
+
+ categories = split_tags(related_tags)
+ del related_tags
+
+ logging.info("Returning %d picutres and %d books" % (pictures.count(), books.count()))
+ objects = SortedMultiQuerySet(pictures, books, order_by='sort_key_author')
+
+
+
+ if not objects:
+ only_author = len(tags) == 1 and tags[0].category == 'author'
+ objects = models.Book.objects.none()
+
+ return render_to_response('catalogue/tagged_object_list.html',
+ {
+ 'object_list': objects,