from newtagging.models import TagBase, tags_updated
from newtagging import managers
from catalogue.fields import JSONField, OverwritingFileField
-from catalogue.utils import ExistingFile, ORMDocProvider, create_zip_task, remove_zip
+from catalogue.utils import ExistingFile, ORMDocProvider, create_zip, remove_zip
from librarian import dcparser, html, epub, NoDublinCore
import mutagen
from sortify import sortify
from os import unlink
+import search
+
TAG_CATEGORIES = (
('author', _('author')),
('epoch', _('epoch')),
if slughifi(self.name) != slughifi(old.name):
self.file.save(None, ExistingFile(self.file.path), save=False, leave=True)
+ super(BookMedia, self).save(*args, **kwargs)
+
# remove the zip package for book with modified media
remove_zip(self.book.slug)
- super(BookMedia, self).save(*args, **kwargs)
extra_info = self.get_extra_info_value()
extra_info.update(self.read_meta())
self.set_extra_info_value(extra_info)
from tempfile import NamedTemporaryFile
import os
- # remove zip with all pdf files
- remove_zip(settings.ALL_PDF_ZIP)
-
try:
pdf_file = NamedTemporaryFile(delete=False)
pdf.transform(ORMDocProvider(self),
finally:
unlink(pdf_file.name)
+ # remove zip with all pdf files
+ remove_zip(settings.ALL_PDF_ZIP)
+
def build_mobi(self):
""" (Re)builds the MOBI file.
from tempfile import NamedTemporaryFile
import os
- # remove zip with all pdf files
- remove_zip(settings.ALL_MOBI_ZIP)
-
try:
mobi_file = NamedTemporaryFile(suffix='.mobi', delete=False)
mobi.transform(ORMDocProvider(self), verbose=1,
finally:
unlink(mobi_file.name)
+ # remove zip with all mobi files
+ remove_zip(settings.ALL_MOBI_ZIP)
+
def build_epub(self, remove_descendants=True):
""" (Re)builds the epub file.
If book has a parent, does nothing.
# don't need an epub
return
- # remove zip package with all epub files
- remove_zip(settings.ALL_EPUB_ZIP)
-
epub_file = StringIO()
try:
epub.transform(ORMDocProvider(self), self.slug, output_file=epub_file)
child_book.save()
book_descendants += list(child_book.children.all())
+ # remove zip package with all epub files
+ remove_zip(settings.ALL_EPUB_ZIP)
+
def build_txt(self):
from StringIO import StringIO
from django.core.files.base import ContentFile
return False
@staticmethod
- def zip_epub():
- books = Book.objects.all()
-
- paths = filter(lambda x: x is not None,
- map(lambda b: b.epub_file and b.epub_file.path or None, books))
- if settings.USE_CELERY:
- result = create_zip_task.delay(paths, settings.ALL_EPUB_ZIP)
- return result.wait()
- else:
- result = create_zip_task(paths, settings.ALL_EPUB_ZIP)
- return result
-
- @staticmethod
- def zip_pdf():
- books = Book.objects.all()
-
- paths = filter(lambda x: x is not None,
- map(lambda b: b.pdf_file and b.pdf_file.path or None, books))
- if settings.USE_CELERY:
- result = create_zip_task.delay(paths, settings.ALL_PDF_ZIP)
- return result.wait()
- else:
- result = create_zip_task(paths, settings.ALL_PDF_ZIP)
- return result
-
- @staticmethod
- def zip_mobi():
- books = Book.objects.all()
-
- paths = filter(lambda x: x is not None,
- map(lambda b: b.mobi_file and b.mobi_file.path or None, books))
- result = create_zip_task.delay(paths, settings.ALL_MOBI_ZIP)
- return settings.MEDIA_URL + result.wait()
+ def zip_format(format_):
+ def pretty_file_name(book):
+ return "%s/%s.%s" % (
+ b.get_extra_info_value()['author'],
+ b.slug,
+ format_)
+
+ field_name = "%s_file" % format_
+ books = Book.objects.filter(parent=None).exclude(**{field_name: ""})
+ paths = [(pretty_file_name(b), getattr(b, field_name).path)
+ for b in books]
+ result = create_zip.delay(paths,
+ getattr(settings, "ALL_%s_ZIP" % format_.upper()))
+ return result.wait()
def zip_audiobooks(self):
- bm = BookMedia.objects.filter(book=self)
- paths = map(lambda bm: bm.file.path, bm)
- if settings.USE_CELERY:
- result = create_zip_task.delay(paths, self.slug)
- return result.wait()
- else:
- result = create_zip_task(paths, self.slug)
- return result
+ bm = BookMedia.objects.filter(book=self, type='mp3')
+ paths = map(lambda bm: (None, bm.file.path), bm)
+ result = create_zip.delay(paths, self.slug)
+ return result.wait()
+
+ def search_index(self):
+ with search.ReusableIndex() as idx:
+ idx.index_book(self)
@classmethod
def from_xml_file(cls, xml_file, **kwargs):
@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,
+ search_index=True):
import re
# check for parts before we do anything
if not settings.NO_BUILD_MOBI and build_mobi:
book.build_mobi()
+ if not settings.NO_SEARCH_INDEX and search_index:
+ book.search_index()
+
book_descendants = list(book.children.all())
# add l-tag to descendants and their fragments
# delete unnecessary EPUB files