af00c35bd83e10852a2cf5ff422d07499017eabf
[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(ignore_result=True)
22 def fix_tree_tags(book):
23     book.fix_tree_tags()
24
25
26 @task
27 def index_book(book_id, book_info=None):
28     from catalogue.models import Book
29     try:
30         return Book.objects.get(id=book_id).search_index(book_info)
31     except Exception, e:
32         print "Exception during index: %s" % e
33         print_exc()
34         raise e
35
36
37 @task(ignore_result=True, rate_limit=settings.CATALOGUE_CUSTOMPDF_RATE_LIMIT)
38 def build_custom_pdf(book_id, customizations, file_name):
39     """Builds a custom PDF file."""
40     from django.core.files import File
41     from django.core.files.storage import DefaultStorage
42     from catalogue.models import Book
43
44     print "will gen %s" % DefaultStorage().path(file_name)
45     if not DefaultStorage().exists(file_name):
46         pdf = Book.objects.get(pk=book_id).wldocument().as_pdf(
47                 customizations=customizations,
48                 morefloats=settings.LIBRARIAN_PDF_MOREFLOATS)
49         DefaultStorage().save(file_name, File(open(pdf.get_filename())))