use librarian 1.4
authorRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Fri, 3 Feb 2012 11:54:53 +0000 (12:54 +0100)
committerRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Fri, 3 Feb 2012 11:54:53 +0000 (12:54 +0100)
fix DocProvider bug: file should contain bytes, not unicode

apps/catalogue/ebook_utils.py
apps/catalogue/models/book.py
apps/catalogue/tasks.py
apps/catalogue/views.py
lib/librarian

index 1fcf8d3..d9c9922 100644 (file)
@@ -13,7 +13,8 @@ class RedakcjaDocProvider(DocProvider):
 
     def by_slug(self, slug):
         return StringIO(Book.objects.get(dc_slug=slug
 
     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):
 
 
 def serve_file(file_path, name, mime_type):
index 13be963..ca5ccc8 100755 (executable)
@@ -318,7 +318,7 @@ class Book(models.Model):
 
         info = self.book_info()
         if info is not None:
 
         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):
         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)
 
             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.
     def publish(self, user):
         """
             Publishes a book on behalf of a (local) user.
index c84a291..ca69625 100644 (file)
@@ -1,6 +1,5 @@
 from celery.task import task
 from django.utils import translation
 from celery.task import task
 from django.utils import translation
-from django.conf import settings
 
 
 @task(ignore_result=True)
 
 
 @task(ignore_result=True)
@@ -25,7 +24,7 @@ def _publishable_error(book, language=None):
     except AssertionError, e:
         return e
     else:
     except AssertionError, e:
         return e
     else:
-       return None
+        return None
     finally:
         translation.activate(prev_language)
 
     finally:
         translation.activate(prev_language)
 
index f56ec5b..d234ab0 100644 (file)
@@ -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.")
     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
     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.")
     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
 
     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.")
 
     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
 
 
 @never_cache
@@ -272,23 +251,13 @@ def book_epub(request, slug):
     if not book.accessible(request):
         return HttpResponseForbidden("Not authorized.")
 
     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 = HttpResponse(mimetype='application/epub+zip')
     response['Content-Disposition'] = 'attachment; filename=%s' % book.slug + '.epub'
-    response.write(epub_file.getvalue())
+    response.write(epub)
     return response
 
 
     return response
 
 
index 2887829..361fd53 160000 (submodule)
@@ -1 +1 @@
-Subproject commit 28878296bacad453735a520f350ba5a971f8ffc8
+Subproject commit 361fd53b42fae8bafe8fd680a0c697757aa19cf3