FB2 support
[wolnelektury.git] / apps / catalogue / tasks.py
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.
4 #
5 from datetime import datetime
6 from traceback import print_exc
7 from celery.task import task
8 from django.conf import settings
9
10
11 # TODO: move to model?
12 def touch_tag(tag):
13     update_dict = {
14         'book_count': tag.get_count(),
15         'changed_at': datetime.now(),
16     }
17
18     type(tag).objects.filter(pk=tag.pk).update(**update_dict)
19
20
21 @task
22 def index_book(book_id, book_info=None):
23     from catalogue.models import Book
24     try:
25         return Book.objects.get(id=book_id).search_index(book_info)
26     except Exception, e:
27         print "Exception during index: %s" % e
28         print_exc()
29         raise e
30
31
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
37
38     text = Book.objects.get(pk=book_id).wldocument().as_text()
39
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()))
44
45
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
53
54     pdf = Book.objects.get(pk=book_id).wldocument().as_pdf(
55             morefloats=settings.LIBRARIAN_PDF_MOREFLOATS)
56
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())))
62
63     # Remove cached downloadables
64     remove_zip(settings.ALL_PDF_ZIP)
65     clear_cache(book.slug)
66
67
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
74
75     epub = Book.objects.get(pk=book_id).wldocument().as_epub()
76     # Save the file in new instance. Building EPUB 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())))
81
82     # remove zip with all epub files
83     remove_zip(settings.ALL_EPUB_ZIP)
84
85
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
92
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())))
99
100     # remove zip with all mobi files
101     remove_zip(settings.ALL_MOBI_ZIP)
102
103
104 @task(ignore_result=True, rate_limit=settings.CATALOGUE_MOBI_RATE_LIMIT)
105 def build_fb2(book_id, *args, **kwargs):
106     """(Re)builds the MOBI file for a book."""
107     from django.core.files import File
108     from catalogue.models import Book
109     from catalogue.utils import remove_zip
110
111     fb2 = Book.objects.get(pk=book_id).wldocument().as_fb2()
112     # Save the file in new instance. Building FB2 takes time and we don't want
113     # to overwrite any interim changes.
114     book = Book.objects.get(id=book_id)
115     book.fb2_file.save('%s.fb2' % book.slug,
116              File(open(fb2.get_filename())))
117
118     # remove zip with all mobi files
119     remove_zip(settings.ALL_FB2_ZIP)
120
121
122 @task(rate_limit=settings.CATALOGUE_CUSTOMPDF_RATE_LIMIT)
123 def build_custom_pdf(book_id, customizations, file_name):
124     """Builds a custom PDF file."""
125     from django.core.files import File
126     from django.core.files.storage import DefaultStorage
127     from catalogue.models import Book
128
129     print "will gen %s" % DefaultStorage().path(file_name)
130     if not DefaultStorage().exists(file_name):
131         pdf = Book.objects.get(pk=book_id).wldocument().as_pdf(
132                 customizations=customizations,
133                 morefloats=settings.LIBRARIAN_PDF_MOREFLOATS)
134         DefaultStorage().save(file_name, File(open(pdf.get_filename())))