+ book = get_object_or_404(Book, slug=slug)
+ if not book.accessible(request):
+ return HttpResponseForbidden("Not authorized.")
+
+ doc = book.wldocument(parse_dublincore=False)
+ html = doc.as_html(options={'gallery': "'%s'" % book.gallery_url()})
+
+ html = html.get_string() if html is not None else ''
+ # response = http.HttpResponse(html, content_type='text/html')
+ # return response
+ # book_themes = {}
+ # for fragment in book.fragments.all().iterator():
+ # for theme in fragment.tags.filter(category='theme').iterator():
+ # book_themes.setdefault(theme, []).append(fragment)
+
+ # book_themes = book_themes.items()
+ # book_themes.sort(key=lambda s: s[0].sort_key)
+ return render(request, 'catalogue/book_text.html', locals())
+
+
+@never_cache
+def book_pdf(request, slug):
+ book = get_object_or_404(Book, slug=slug)
+ if not book.accessible(request):
+ return HttpResponseForbidden("Not authorized.")
+
+ # TODO: move to celery
+ doc = book.wldocument()
+ # TODO: error handling
+ pdf_file = doc.as_pdf(cover=True, ilustr_path=book.gallery_path())
+ from catalogue.ebook_utils import serve_file
+ return serve_file(pdf_file.get_filename(),
+ book.slug + '.pdf', 'application/pdf')
+
+
+@never_cache
+def book_epub(request, slug):
+ book = get_object_or_404(Book, slug=slug)
+ if not book.accessible(request):
+ return HttpResponseForbidden("Not authorized.")
+
+ # TODO: move to celery
+ doc = book.wldocument()
+ # TODO: error handling
+ epub = doc.as_epub(ilustr_path=book.gallery_path()).get_string()
+ response = HttpResponse(content_type='application/epub+zip')
+ response['Content-Disposition'] = 'attachment; filename=%s' % book.slug + '.epub'
+ response.write(epub)
+ return response
+
+
+@never_cache
+def book_mobi(request, slug):
+ book = get_object_or_404(Book, slug=slug)
+ if not book.accessible(request):
+ return HttpResponseForbidden("Not authorized.")
+
+ # TODO: move to celery
+ doc = book.wldocument()
+ # TODO: error handling
+ mobi = doc.as_mobi(ilustr_path=book.gallery_path()).get_string()
+ response = HttpResponse(content_type='application/x-mobipocket-ebook')
+ response['Content-Disposition'] = 'attachment; filename=%s' % book.slug + '.mobi'
+ response.write(mobi)