-from catalogue.utils import split_tags
-from newtagging import views as newtagging_views
-
-
-class LazyEncoder(simplejson.JSONEncoder):
- def default(self, obj):
- if isinstance(obj, Promise):
- return force_unicode(obj)
- return obj
-
-
-@cache.cache_control(must_revalidate=True, max_age=3600)
-def main_page(request):
- if request.user.is_authenticated():
- shelves = models.Tag.objects.filter(category='set', user=request.user)
- new_set_form = forms.NewSetForm()
- extra_where = 'NOT catalogue_tag.category = "set"'
- tags = models.Tag.objects.usage_for_model(models.Book, counts=True, extra={'where': [extra_where]})
- fragment_tags = models.Tag.objects.usage_for_model(models.Fragment, counts=True,
- extra={'where': ['catalogue_tag.category = "theme"'] + [extra_where]})
- categories = split_tags(tags)
-
- form = forms.SearchForm()
- return render_to_response('catalogue/main_page.html', locals(),
- context_instance=RequestContext(request))
+from catalogue.utils import split_tags, MultiQuerySet, SortedMultiQuerySet
+from catalogue.templatetags.catalogue_tags import tag_list, collection_list
+from pdcounter import models as pdcounter_models
+from pdcounter import views as pdcounter_views
+from suggest.forms import PublishingSuggestForm
+from picture.models import Picture, PictureArea
+from picture.views import picture_list_thumb
+import logging
+staff_required = user_passes_test(lambda user: user.is_staff)
+permanent_cache = get_cache('permanent')
+
+
+@vary_on_headers('X-Requested-With')
+def catalogue(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))