fix podcast player
[wolnelektury.git] / src / 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 celery.utils.log import get_task_logger
9 from django.conf import settings
10
11 from catalogue.utils import gallery_path
12 from wolnelektury.utils import localtime_to_utc
13 from waiter.models import WaitedFile
14
15 task_logger = get_task_logger(__name__)
16
17
18 # TODO: move to model?
19 def touch_tag(tag):
20     update_dict = {
21         'changed_at': localtime_to_utc(datetime.now()),
22     }
23
24     type(tag).objects.filter(pk=tag.pk).update(**update_dict)
25
26
27 @task
28 def index_book(book_id, book_info=None, **kwargs):
29     from catalogue.models import Book
30     try:
31         return Book.objects.get(id=book_id).search_index(book_info, **kwargs)
32     except Exception, e:
33         print "Exception during index: %s" % e
34         print_exc()
35         raise e
36
37
38 @task(ignore_result=True, rate_limit=settings.CATALOGUE_CUSTOMPDF_RATE_LIMIT)
39 def build_custom_pdf(book_id, customizations, file_name, waiter_id=None):
40     """Builds a custom PDF file."""
41     try:
42         from django.core.files import File
43         from django.core.files.storage import DefaultStorage
44         from catalogue.models import Book
45
46         task_logger.info(DefaultStorage().path(file_name))
47         if not DefaultStorage().exists(file_name):
48             kwargs = {
49                 'cover': True,
50             }
51             if 'nocover' in customizations:
52                 kwargs['cover'] = False
53                 customizations.remove('nocover')
54             wldoc = Book.objects.get(pk=book_id).wldocument()
55             pdf = wldoc.as_pdf(
56                 customizations=customizations,
57                 morefloats=settings.LIBRARIAN_PDF_MOREFLOATS,
58                 ilustr_path=gallery_path(wldoc.book_info.url.slug),
59                 **kwargs)
60             DefaultStorage().save(file_name, File(open(pdf.get_filename())))
61     finally:
62         if waiter_id is not None:
63             WaitedFile.objects.filter(pk=waiter_id).delete()
64
65
66 @task(ignore_result=True)
67 def update_counters():
68     from .helpers import update_counters
69     update_counters()