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 @task(ignore_result=True)
33 def build_txt(book_id):
34 """(Re)builds the TXT file for a book."""
35 from django.core.files.base import ContentFile
36 from catalogue.models import Book
38 text = Book.objects.get(pk=book_id).wldocument().as_text()
40 # Save the file in new instance. Building TXT takes time and we don't want
41 # to overwrite any interim changes.
42 book = Book.objects.get(id=book_id)
43 book.txt_file.save('%s.txt' % book.slug, ContentFile(text.get_string()))
46 @task(ignore_result=True, rate_limit=settings.CATALOGUE_PDF_RATE_LIMIT)
47 def build_pdf(book_id):
48 """(Re)builds the pdf file for a book."""
49 from django.core.files import File
50 from catalogue.models import Book
51 from catalogue.utils import remove_zip
52 from waiter.utils import clear_cache
54 pdf = Book.objects.get(pk=book_id).wldocument().as_pdf(
55 morefloats=settings.LIBRARIAN_PDF_MOREFLOATS)
57 # Save the file in new instance. Building PDF takes time and we don't want
58 # to overwrite any interim changes.
59 book = Book.objects.get(id=book_id)
60 book.pdf_file.save('%s.pdf' % book.slug,
61 File(open(pdf.get_filename())))
63 # Remove cached downloadables
64 remove_zip(settings.ALL_PDF_ZIP)
65 clear_cache(book.slug)
68 @task(ignore_result=True, rate_limit=settings.CATALOGUE_EPUB_RATE_LIMIT)
69 def build_epub(book_id):
70 """(Re)builds the EPUB file for a book."""
71 from django.core.files import File
72 from catalogue.models import Book
73 from catalogue.utils import remove_zip
75 epub = Book.objects.get(pk=book_id).wldocument().as_epub()
76 # Save the file in new instance. Building MOBI takes time and we don't want
77 # to overwrite any interim changes.
78 book = Book.objects.get(id=book_id)
79 book.epub_file.save('%s.epub' % book.slug,
80 File(open(epub.get_filename())))
82 # remove zip with all epub files
83 remove_zip(settings.ALL_EPUB_ZIP)
86 @task(ignore_result=True, rate_limit=settings.CATALOGUE_MOBI_RATE_LIMIT)
87 def build_mobi(book_id):
88 """(Re)builds the MOBI file for a book."""
89 from django.core.files import File
90 from catalogue.models import Book
91 from catalogue.utils import remove_zip
93 mobi = Book.objects.get(pk=book_id).wldocument().as_mobi()
94 # Save the file in new instance. Building MOBI takes time and we don't want
95 # to overwrite any interim changes.
96 book = Book.objects.get(id=book_id)
97 book.mobi_file.save('%s.mobi' % book.slug,
98 File(open(mobi.get_filename())))
100 # remove zip with all mobi files
101 remove_zip(settings.ALL_MOBI_ZIP)
104 @task(rate_limit=settings.CATALOGUE_CUSTOMPDF_RATE_LIMIT)
105 def build_custom_pdf(book_id, customizations, file_name):
106 """Builds a custom PDF file."""
107 from django.core.files import File
108 from django.core.files.storage import DefaultStorage
109 from catalogue.models import Book
111 print "will gen %s" % DefaultStorage().path(file_name)
112 if not DefaultStorage().exists(file_name):
113 pdf = Book.objects.get(pk=book_id).wldocument().as_pdf(
114 customizations=customizations,
115 morefloats=settings.LIBRARIAN_PDF_MOREFLOATS)
116 DefaultStorage().save(file_name, File(open(pdf.get_filename())))