Merge remote-tracking branch 'zawadzki/new-design'
[wolnelektury.git] / src / catalogue / fields.py
index 79fe67e..581bf38 100644 (file)
@@ -1,6 +1,7 @@
 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
+import os
 from django.conf import settings
 from django.core.files import File
 from django.core.files.storage import FileSystemStorage
@@ -8,7 +9,7 @@ from django.db import models
 from django.db.models.fields.files import FieldFile
 from catalogue import app_settings
 from catalogue.constants import LANGUAGES_3TO2, EBOOK_FORMATS_WITH_CHILDREN, EBOOK_FORMATS_WITHOUT_CHILDREN
-from catalogue.utils import remove_zip, truncate_html_words, gallery_path, gallery_url
+from catalogue.utils import absolute_url, remove_zip, truncate_html_words, gallery_path, gallery_url
 from celery.task import Task, task
 from celery.utils.log import get_task_logger
 from waiter.utils import clear_cache
@@ -123,6 +124,8 @@ class EbookField(models.FileField):
 
 
 class BuildEbook(Task):
+    librarian2_api = False
+
     formats = {}
 
     @classmethod
@@ -165,7 +168,9 @@ class BuildEbook(Task):
 
     def build(self, fieldfile):
         book = fieldfile.instance
-        out = self.transform(book.wldocument(), fieldfile)
+        out = self.transform(
+            book.wldocument2() if self.librarian2_api else book.wldocument(),
+            fieldfile)
         fieldfile.save(None, File(open(out.get_filename(), 'rb')), save=False)
         self.set_file_permissions(fieldfile)
         if book.pk is not None:
@@ -191,7 +196,7 @@ class BuildPdf(BuildEbook):
     def transform(wldoc, fieldfile):
         return wldoc.as_pdf(
             morefloats=settings.LIBRARIAN_PDF_MOREFLOATS, cover=True,
-            base_url=gallery_url(wldoc.book_info.url.slug), customizations=['notoc'])
+            base_url=absolute_url(gallery_url(wldoc.book_info.url.slug)), customizations=['notoc'])
 
     def build(self, fieldfile):
         BuildEbook.build(self, fieldfile)
@@ -201,17 +206,29 @@ class BuildPdf(BuildEbook):
 @BuildEbook.register('epub')
 @task(ignore_result=True)
 class BuildEpub(BuildEbook):
+    librarian2_api = True
+
     @staticmethod
     def transform(wldoc, fieldfile):
-        return wldoc.as_epub(cover=True, base_url=gallery_url(wldoc.book_info.url.slug))
+        from librarian.builders import EpubBuilder
+        return EpubBuilder(
+                base_url='file://' + os.path.abspath(gallery_path(wldoc.meta.url.slug)) + '/',
+                fundraising=settings.EPUB_FUNDRAISING
+            ).build(wldoc)
 
 
 @BuildEbook.register('mobi')
 @task(ignore_result=True)
 class BuildMobi(BuildEbook):
+    librarian2_api = True
+
     @staticmethod
     def transform(wldoc, fieldfile):
-        return wldoc.as_mobi(cover=True, base_url=gallery_url(wldoc.book_info.url.slug))
+        from librarian.builders import MobiBuilder
+        return MobiBuilder(
+                base_url='file://' + os.path.abspath(gallery_path(wldoc.meta.url.slug)) + '/',
+                fundraising=settings.EPUB_FUNDRAISING
+            ).build(wldoc)
 
 
 @BuildEbook.register('html')
@@ -317,7 +334,7 @@ class BuildHtml(BuildEbook):
             slug = url_elem.text.rstrip('/').rsplit('/', 1)[1]
             gal_url = gallery_url(slug=slug)
             gal_path = gallery_path(slug=slug)
-        return wldoc.as_html(gallery_path=gal_path, gallery_url=gal_url, base_url=gal_url)
+        return wldoc.as_html(gallery_path=gal_path, gallery_url=gal_url, base_url=absolute_url(gal_url))
 
 
 class BuildCover(BuildEbook):