X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/9aa0db6d60b6b517f0efdff4e0f7e585e1d35de1..9a6638c3bf05511aa43d6511436014921f00b169:/apps/catalogue/models.py?ds=sidebyside diff --git a/apps/catalogue/models.py b/apps/catalogue/models.py index 7e3ccfecb..9c943ca75 100644 --- a/apps/catalogue/models.py +++ b/apps/catalogue/models.py @@ -453,8 +453,7 @@ class Book(models.Model): epub_file = StringIO() try: epub.transform(BookImportDocProvider(self), self.slug, output_file=epub_file) - self.epub_file.save('%s.epub' % self.slug, ContentFile(epub_file.getvalue()), save=False) - self.save() + self.epub_file.save('%s.epub' % self.slug, ContentFile(epub_file.getvalue())) FileRecord(slug=self.slug, type='epub', sha1=sha1(epub_file.getvalue()).hexdigest()).save() except NoDublinCore: pass @@ -468,6 +467,16 @@ class Book(models.Model): child_book.save() book_descendants += list(child_book.children.all()) + def build_txt(self): + from StringIO import StringIO + from django.core.files.base import ContentFile + from librarian import text + + out = StringIO() + text.transform(open(self.xml_file.path), out) + self.txt_file.save('%s.txt' % self.slug, ContentFile(out.getvalue())) + self.save() + @classmethod def from_xml_file(cls, xml_file, overwrite=False, build_epub=True): @@ -483,7 +492,7 @@ class Book(models.Model): xml_file.close() @classmethod - def from_text_and_meta(cls, raw_file, book_info, overwrite=False, build_epub=True): + def from_text_and_meta(cls, raw_file, book_info, overwrite=False, build_epub=True, build_txt=True): from tempfile import NamedTemporaryFile from slughifi import slughifi from markupstring import MarkupString @@ -592,6 +601,9 @@ class Book(models.Model): new_fragment.save() new_fragment.tags = set(book_tags + themes + [book_tag] + ancestor_tags) + if not settings.NO_BUILD_TXT and build_txt: + book.build_txt() + if not settings.NO_BUILD_EPUB and build_epub: book.root_ancestor.build_epub()