+@never_cache
+def book_pdf(request, slug):
+ book = get_object_or_404(Book, slug=slug)
+ if not book.accessible(request):
+ return HttpResponseForbidden("Not authorized.")
+
+ from tempfile import NamedTemporaryFile
+ from os import unlink
+ from librarian import pdf
+ from catalogue.ebook_utils import RedakcjaDocProvider, serve_file
+
+ xml = book.materialize()
+ xml_file = NamedTemporaryFile()
+ xml_file.write(xml.encode('utf-8'))
+ xml_file.flush()
+
+ try:
+ pdf_file = NamedTemporaryFile(delete=False)
+ pdf.transform(RedakcjaDocProvider(publishable=True),
+ file_path=xml_file.name,
+ output_file=pdf_file,
+ )
+ return serve_file(pdf_file.name, book.slug + '.pdf', 'application/pdf')
+ finally:
+ unlink(pdf_file.name)
+
+
+@never_cache
+def book_epub(request, slug):
+ book = get_object_or_404(Book, slug=slug)
+ if not book.accessible(request):
+ return HttpResponseForbidden("Not authorized.")
+
+ from StringIO import StringIO
+ from tempfile import NamedTemporaryFile
+ from librarian import epub
+ from catalogue.ebook_utils import RedakcjaDocProvider
+
+ xml = book.materialize()
+ xml_file = NamedTemporaryFile()
+ xml_file.write(xml.encode('utf-8'))
+ xml_file.flush()
+
+ epub_file = StringIO()
+ epub.transform(RedakcjaDocProvider(publishable=True),
+ file_path=xml_file.name,
+ output_file=epub_file)
+ response = HttpResponse(mimetype='application/epub+zip')
+ response['Content-Disposition'] = 'attachment; filename=%s' % book.slug + '.epub'
+ response.write(epub_file.getvalue())
+ return response
+
+