1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 from datetime import datetime
6 from traceback import print_exc
7 from celery.task import task
8 from django.conf import settings
11 # TODO: move to model?
14 'book_count': tag.get_count(),
15 'changed_at': datetime.now(),
18 type(tag).objects.filter(pk=tag.pk).update(**update_dict)
22 def index_book(book_id, book_info=None):
23 from catalogue.models import Book
25 return Book.objects.get(id=book_id).search_index(book_info)
27 print "Exception during index: %s" % e
32 def _build_ebook(book_id, ext, transform):
33 """Generic ebook builder."""
34 from django.core.files import File
35 from catalogue.models import Book
37 book = Book.objects.get(pk=book_id)
38 out = transform(book.wldocument())
39 field_name = '%s_file' % ext
40 # Update instead of saving the model to avoid race condition.
41 getattr(book, field_name).save('%s.%s' % (book.slug, ext),
42 File(open(out.get_filename())),
45 Book.objects.filter(pk=book_id).update(**{
46 field_name: getattr(book, field_name)
50 @task(ignore_result=True)
51 def build_txt(book_id):
52 """(Re)builds the TXT file for a book."""
53 _build_ebook(book_id, 'txt', lambda doc: doc.as_text())
56 @task(ignore_result=True, rate_limit=settings.CATALOGUE_PDF_RATE_LIMIT)
57 def build_pdf(book_id):
58 """(Re)builds the pdf file for a book."""
59 from catalogue.models import Book
60 from catalogue.utils import remove_zip
61 from waiter.utils import clear_cache
63 _build_ebook(book_id, 'pdf',
64 lambda doc: doc.as_pdf(morefloats=settings.LIBRARIAN_PDF_MOREFLOATS))
65 # Remove cached downloadables
66 remove_zip(settings.ALL_PDF_ZIP)
67 book = Book.objects.get(pk=book_id)
68 clear_cache(book.slug)
71 @task(ignore_result=True, rate_limit=settings.CATALOGUE_EPUB_RATE_LIMIT)
72 def build_epub(book_id):
73 """(Re)builds the EPUB file for a book."""
74 from catalogue.utils import remove_zip
76 _build_ebook(book_id, 'epub', lambda doc: doc.as_epub())
77 # remove zip with all epub files
78 remove_zip(settings.ALL_EPUB_ZIP)
81 @task(ignore_result=True, rate_limit=settings.CATALOGUE_MOBI_RATE_LIMIT)
82 def build_mobi(book_id):
83 """(Re)builds the MOBI file for a book."""
84 from catalogue.utils import remove_zip
86 _build_ebook(book_id, 'mobi', lambda doc: doc.as_mobi())
87 # remove zip with all mobi files
88 remove_zip(settings.ALL_MOBI_ZIP)
91 @task(ignore_result=True, rate_limit=settings.CATALOGUE_FB2_RATE_LIMIT)
92 def build_fb2(book_id, *args, **kwargs):
93 """(Re)builds the FB2 file for a book."""
94 from catalogue.utils import remove_zip
96 _build_ebook(book_id, 'fb2', lambda doc: doc.as_fb2())
97 # remove zip with all fb2 files
98 remove_zip(settings.ALL_FB2_ZIP)
101 @task(rate_limit=settings.CATALOGUE_CUSTOMPDF_RATE_LIMIT)
102 def build_custom_pdf(book_id, customizations, file_name):
103 """Builds a custom PDF file."""
104 from django.core.files import File
105 from django.core.files.storage import DefaultStorage
106 from catalogue.models import Book
108 print "will gen %s" % DefaultStorage().path(file_name)
109 if not DefaultStorage().exists(file_name):
110 pdf = Book.objects.get(pk=book_id).wldocument().as_pdf(
111 customizations=customizations,
112 morefloats=settings.LIBRARIAN_PDF_MOREFLOATS)
113 DefaultStorage().save(file_name, File(open(pdf.get_filename())))