FB2 support
authorMarcin Koziej <marcin.koziej@nowoczesnapolska.org.pl>
Mon, 28 May 2012 15:03:00 +0000 (17:03 +0200)
committerMarcin Koziej <marcin.koziej@nowoczesnapolska.org.pl>
Mon, 28 May 2012 15:03:00 +0000 (17:03 +0200)
apps/catalogue/models.py
apps/catalogue/tasks.py
apps/catalogue/templates/catalogue/book_short.html
lib/librarian
wolnelektury/settings/custom.py

index 29106b1..f283cce 100644 (file)
@@ -346,7 +346,7 @@ class Book(models.Model):
 
     cover = models.FileField(_('cover'), upload_to=book_upload_path('png'),
                 null=True, blank=True)
 
     cover = models.FileField(_('cover'), upload_to=book_upload_path('png'),
                 null=True, blank=True)
-    ebook_formats = ['pdf', 'epub', 'mobi', 'txt']
+    ebook_formats = ['pdf', 'epub', 'mobi', 'fb2', 'txt']
     formats = ebook_formats + ['html', 'xml']
 
     parent        = models.ForeignKey('self', blank=True, null=True, related_name='children')
     formats = ebook_formats + ['html', 'xml']
 
     parent        = models.ForeignKey('self', blank=True, null=True, related_name='children')
@@ -546,6 +546,9 @@ class Book(models.Model):
     def build_mobi(self, *args, **kwargs):
         """(Re)builds MOBI."""
         return tasks.build_mobi.delay(self.pk, *args, **kwargs)
     def build_mobi(self, *args, **kwargs):
         """(Re)builds MOBI."""
         return tasks.build_mobi.delay(self.pk, *args, **kwargs)
+    def build_fb2(self, *args, **kwargs):
+        """(Re)build FB2"""
+        return tasks.build_fb2.delay(self.pk, *args, **kwargs)
     def build_txt(self, *args, **kwargs):
         """(Re)builds TXT."""
         return tasks.build_txt.delay(self.pk, *args, **kwargs)
     def build_txt(self, *args, **kwargs):
         """(Re)builds TXT."""
         return tasks.build_txt.delay(self.pk, *args, **kwargs)
@@ -603,7 +606,7 @@ class Book(models.Model):
 
     @classmethod
     def from_text_and_meta(cls, raw_file, book_info, overwrite=False,
 
     @classmethod
     def from_text_and_meta(cls, raw_file, book_info, overwrite=False,
-            build_epub=True, build_txt=True, build_pdf=True, build_mobi=True,
+            build_epub=True, build_txt=True, build_pdf=True, build_mobi=True, build_fb2=True,
             search_index=True, search_index_tags=True, search_index_reuse=False):
 
         # check for parts before we do anything
             search_index=True, search_index_tags=True, search_index_reuse=False):
 
         # check for parts before we do anything
@@ -673,6 +676,9 @@ class Book(models.Model):
         if not settings.NO_BUILD_MOBI and build_mobi:
             book.build_mobi()
 
         if not settings.NO_BUILD_MOBI and build_mobi:
             book.build_mobi()
 
+        if not settings.NO_BUILD_FB2 and build_fb2:
+            book.build_fb2()
+
         if not settings.NO_SEARCH_INDEX and search_index:
             book.search_index(index_tags=search_index_tags, reuse_index=search_index_reuse)
             #index_book.delay(book.id, book_info)
         if not settings.NO_SEARCH_INDEX and search_index:
             book.search_index(index_tags=search_index_tags, reuse_index=search_index_reuse)
             #index_book.delay(book.id, book_info)
index 6d19ee1..d043782 100755 (executable)
@@ -73,7 +73,7 @@ def build_epub(book_id):
     from catalogue.utils import remove_zip
 
     epub = Book.objects.get(pk=book_id).wldocument().as_epub()
     from catalogue.utils import remove_zip
 
     epub = Book.objects.get(pk=book_id).wldocument().as_epub()
-    # Save the file in new instance. Building MOBI takes time and we don't want
+    # Save the file in new instance. Building EPUB takes time and we don't want
     # to overwrite any interim changes.
     book = Book.objects.get(id=book_id)
     book.epub_file.save('%s.epub' % book.slug,
     # to overwrite any interim changes.
     book = Book.objects.get(id=book_id)
     book.epub_file.save('%s.epub' % book.slug,
@@ -101,6 +101,24 @@ def build_mobi(book_id):
     remove_zip(settings.ALL_MOBI_ZIP)
 
 
     remove_zip(settings.ALL_MOBI_ZIP)
 
 
+@task(ignore_result=True, rate_limit=settings.CATALOGUE_MOBI_RATE_LIMIT)
+def build_fb2(book_id, *args, **kwargs):
+    """(Re)builds the MOBI file for a book."""
+    from django.core.files import File
+    from catalogue.models import Book
+    from catalogue.utils import remove_zip
+
+    fb2 = Book.objects.get(pk=book_id).wldocument().as_fb2()
+    # Save the file in new instance. Building FB2 takes time and we don't want
+    # to overwrite any interim changes.
+    book = Book.objects.get(id=book_id)
+    book.fb2_file.save('%s.fb2' % book.slug,
+             File(open(fb2.get_filename())))
+
+    # remove zip with all mobi files
+    remove_zip(settings.ALL_FB2_ZIP)
+
+
 @task(rate_limit=settings.CATALOGUE_CUSTOMPDF_RATE_LIMIT)
 def build_custom_pdf(book_id, customizations, file_name):
     """Builds a custom PDF file."""
 @task(rate_limit=settings.CATALOGUE_CUSTOMPDF_RATE_LIMIT)
 def build_custom_pdf(book_id, customizations, file_name):
     """Builds a custom PDF file."""
index d9b5b76..6942bc4 100644 (file)
@@ -95,6 +95,9 @@
              {% if  book.mobi_file %}
              <span><a href="{{ book.mobi_file.url}}">MOBI</a> {% trans "for Kindle" %}</span>
              {% endif %}
              {% if  book.mobi_file %}
              <span><a href="{{ book.mobi_file.url}}">MOBI</a> {% trans "for Kindle" %}</span>
              {% endif %}
+             {% if  book.fb2_file %}
+             <span><a href="{{ book.fb2_file.url}}">FB2</a> {% trans "FictionBook" %}</span>
+             {% endif %}
              {% if  book.txt_file %}
              <span><a href="{{ book.txt_file.url}}">TXT</a> {% trans "for advanced usage" %}</span>
              {% endif %}
              {% if  book.txt_file %}
              <span><a href="{{ book.txt_file.url}}">TXT</a> {% trans "for advanced usage" %}</span>
              {% endif %}
index fed2483..bd2ad23 160000 (submodule)
@@ -1 +1 @@
-Subproject commit fed2483b39e23ecd91ec2d04495a4caac208e94c
+Subproject commit bd2ad2356d916f72762de867890b5d176830f73b
index e059b8f..76aa581 100644 (file)
@@ -7,15 +7,18 @@ MAX_TAG_LIST = 6
 NO_SEARCH_INDEX = False
 NO_BUILD_EPUB = False
 NO_BUILD_TXT = False
 NO_SEARCH_INDEX = False
 NO_BUILD_EPUB = False
 NO_BUILD_TXT = False
+NO_BUILD_FB2 = False
 # You'll need XeLaTeX to generate PDF files.
 NO_BUILD_PDF = True
 NO_CUSTOM_PDF = True
 # You'll need Calibre installed to generate MOBI files.
 NO_BUILD_MOBI = True
 
 # You'll need XeLaTeX to generate PDF files.
 NO_BUILD_PDF = True
 NO_CUSTOM_PDF = True
 # You'll need Calibre installed to generate MOBI files.
 NO_BUILD_MOBI = True
 
+
 ALL_EPUB_ZIP = 'wolnelektury_pl_epub'
 ALL_PDF_ZIP = 'wolnelektury_pl_pdf'
 ALL_MOBI_ZIP = 'wolnelektury_pl_mobi'
 ALL_EPUB_ZIP = 'wolnelektury_pl_epub'
 ALL_PDF_ZIP = 'wolnelektury_pl_pdf'
 ALL_MOBI_ZIP = 'wolnelektury_pl_mobi'
+ALL_FB2_ZIP = 'wolnelektury_pl_fb2'
 
 CATALOGUE_DEFAULT_LANGUAGE = 'pol'
 PUBLISH_PLAN_FEED = 'http://redakcja.wolnelektury.pl/documents/track/editor-proofreading/?published=false'
 
 CATALOGUE_DEFAULT_LANGUAGE = 'pol'
 PUBLISH_PLAN_FEED = 'http://redakcja.wolnelektury.pl/documents/track/editor-proofreading/?published=false'