+ # Create EPUB
+ epub_file = StringIO()
+ try:
+ epub.transform(BookImportDocProvider(book), book.slug, epub_file)
+ book.epub_file.save('%s.epub' % book.slug, ContentFile(epub_file.getvalue()), save=False)
+ FileRecord(slug=book.slug, type='epub', sha1=sha1(epub_file.getvalue()).hexdigest()).save()
+ except NoDublinCore:
+ pass
+
+ delete_epubs = book.has_epub_file()
+ book_descendants = list(book.children.all())
+ # add l-tag to descendants and their fragments
+ # delete unnecessary EPUB files
+ while len(book_descendants) > 0:
+ child_book = book_descendants.pop(0)
+ child_book.tags = list(child_book.tags) + [book_tag]
+ if delete_epubs:
+ child_book.epub_file.delete()
+ child_book.save()
+ for fragment in child_book.fragments.all():
+ fragment.tags = set(list(fragment.tags) + [book_tag])
+ book_descendants += list(child_book.children.all())
+