- tags = models.Tag.objects.exclude(
- category__in=('set', 'book')).exclude(book_count=0)
- tags = list(tags)
- for tag in tags:
- tag.count = tag.book_count
- categories = split_tags(tags)
- fragment_tags = categories.get('theme', [])
-
- return render_to_response('catalogue/catalogue.html', locals(),
- context_instance=RequestContext(request))
+ cache_key = 'catalogue.catalogue/' + get_language()
+ output = permanent_cache.get(cache_key)
+
+ if output is None:
+ tags = models.Tag.objects.exclude(
+ category__in=('set', 'book')).exclude(book_count=0, picture_count=0)
+ tags = list(tags)
+ for tag in tags:
+ tag.count = tag.book_count + tag.picture_count
+ categories = split_tags(tags)
+ fragment_tags = categories.get('theme', [])
+ collections = models.Collection.objects.all()
+
+ render_tag_list = lambda x: render_to_string(
+ 'catalogue/tag_list.html', tag_list(x))
+ has_pictures = lambda x: filter(lambda y: y.picture_count > 0, x)
+ has_books = lambda x: filter(lambda y: y.book_count > 0, x)
+ def render_split(tags):
+ with_books = has_books(tags)
+ with_pictures = has_pictures(tags)
+ ctx = {}
+ if with_books:
+ ctx['books'] = render_tag_list(with_books)
+ if with_pictures:
+ ctx['pictures'] = render_tag_list(with_pictures)
+ return render_to_string('catalogue/tag_list_split.html', ctx)
+
+ output = {'theme': {}}
+ output['theme'] = render_tag_list(fragment_tags)
+ for category, tags in categories.items():
+ if category in ('author', 'theme'):
+ output[category] = render_tag_list(tags)
+ else:
+ output[category] = render_split(tags)
+
+ output['collections'] = render_to_string(
+ 'catalogue/collection_list.html', collection_list(collections))
+ permanent_cache.set(cache_key, output)
+ if request.is_ajax():
+ return JSONResponse(output)
+ else:
+ return render_to_response('catalogue/catalogue.html', locals(),
+ context_instance=RequestContext(request))