- xml = get_object_or_404(Book, slug=slug).materialize()
- output = StringIO()
- # errors?
- librarian.html.transform(StringIO(xml), output, parse_dublincore=False,
- flags=['full-page'])
- html = output.getvalue()
- response = http.HttpResponse(html, content_type='text/html', mimetype='text/html')
+ book = get_object_or_404(Book, slug=slug)
+ if not book.accessible(request):
+ return HttpResponseForbidden("Not authorized.")
+
+ doc = book.wldocument()
+ html = doc.as_html()
+
+ html = html.get_string() if html is not None else ''
+ # response = http.HttpResponse(html, content_type='text/html', mimetype='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_to_response(
+ 'catalogue/book_text.html', locals(),
+ context_instance=RequestContext(request))
+
+
+@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()
+ 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().get_string()
+ response = HttpResponse(mimetype='application/epub+zip')
+ response['Content-Disposition'] = 'attachment; filename=%s' % book.slug + '.epub'
+ response.write(epub)