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