From 1019e504d3ffa1edc034428d5e3b5cbf7bd2fc4f Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Fri, 3 Feb 2012 12:54:53 +0100 Subject: [PATCH] use librarian 1.4 fix DocProvider bug: file should contain bytes, not unicode --- apps/catalogue/ebook_utils.py | 3 +- apps/catalogue/models/book.py | 11 +++++- apps/catalogue/tasks.py | 3 +- apps/catalogue/views.py | 63 +++++++++-------------------------- lib/librarian | 2 +- 5 files changed, 30 insertions(+), 52 deletions(-) diff --git a/apps/catalogue/ebook_utils.py b/apps/catalogue/ebook_utils.py index 1fcf8d33..d9c99227 100644 --- a/apps/catalogue/ebook_utils.py +++ b/apps/catalogue/ebook_utils.py @@ -13,7 +13,8 @@ class RedakcjaDocProvider(DocProvider): def by_slug(self, slug): return StringIO(Book.objects.get(dc_slug=slug - ).materialize(publishable=self.publishable)) + ).materialize(publishable=self.publishable + ).encode('utf-8')) def serve_file(file_path, name, mime_type): diff --git a/apps/catalogue/models/book.py b/apps/catalogue/models/book.py index 13be963d..ca5ccc87 100755 --- a/apps/catalogue/models/book.py +++ b/apps/catalogue/models/book.py @@ -318,7 +318,7 @@ class Book(models.Model): info = self.book_info() if info is not None: - update['dc_slug'] = info.slug + update['dc_slug'] = info.url.slug Book.objects.filter(pk=self.pk).update(**update) def touch(self): @@ -370,6 +370,15 @@ class Book(models.Model): changes = self.get_current_changes(publishable) return compile_text(change.materialize() for change in changes) + def wldocument(self, publishable=True, changes=None, parse_dublincore=True): + from catalogue.ebook_utils import RedakcjaDocProvider + from librarian.parser import WLDocument + + return WLDocument.from_string( + self.materialize(publishable=publishable, changes=changes), + provider=RedakcjaDocProvider(publishable=publishable), + parse_dublincore=parse_dublincore) + def publish(self, user): """ Publishes a book on behalf of a (local) user. diff --git a/apps/catalogue/tasks.py b/apps/catalogue/tasks.py index c84a2912..ca696256 100644 --- a/apps/catalogue/tasks.py +++ b/apps/catalogue/tasks.py @@ -1,6 +1,5 @@ from celery.task import task from django.utils import translation -from django.conf import settings @task(ignore_result=True) @@ -25,7 +24,7 @@ def _publishable_error(book, language=None): except AssertionError, e: return e else: - return None + return None finally: translation.activate(prev_language) diff --git a/apps/catalogue/views.py b/apps/catalogue/views.py index f56ec5b5..d234ab0c 100644 --- a/apps/catalogue/views.py +++ b/apps/catalogue/views.py @@ -210,13 +210,9 @@ def book_txt(request, slug): book = get_object_or_404(Book, slug=slug) if not book.accessible(request): return HttpResponseForbidden("Not authorized.") - xml = book.materialize() - output = StringIO() - # errors? - import librarian.text - librarian.text.transform(StringIO(xml), output) - text = output.getvalue() + doc = book.wldocument() + text = doc.as_text().get_string() response = http.HttpResponse(text, content_type='text/plain', mimetype='text/plain') response['Content-Disposition'] = 'attachment; filename=%s.txt' % slug return response @@ -227,14 +223,9 @@ def book_html(request, slug): book = get_object_or_404(Book, slug=slug) if not book.accessible(request): return HttpResponseForbidden("Not authorized.") - xml = book.materialize() - output = StringIO() - # errors? - import librarian.html - librarian.html.transform(StringIO(xml), output, parse_dublincore=False, - flags=['full-page']) - html = output.getvalue() + doc = book.wldocument() + html = doc.as_html(parse_dublincore=False, flags=['full-page']).get_string() response = http.HttpResponse(html, content_type='text/html', mimetype='text/html') return response @@ -245,25 +236,13 @@ def book_pdf(request, 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) + # 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 @@ -272,23 +251,13 @@ def book_epub(request, 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) + # 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_file.getvalue()) + response.write(epub) return response diff --git a/lib/librarian b/lib/librarian index 28878296..361fd53b 160000 --- a/lib/librarian +++ b/lib/librarian @@ -1 +1 @@ -Subproject commit 28878296bacad453735a520f350ba5a971f8ffc8 +Subproject commit 361fd53b42fae8bafe8fd680a0c697757aa19cf3 -- 2.20.1