Tighter sponsors html, every pageview gets ~10kB lighter.
[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 from wolnelektury.utils import localtime_to_utc
10
11
12 # TODO: move to model?
13 def touch_tag(tag):
14     update_dict = {
15         'book_count': tag.get_count(),
16         'picture_count': tag.get_picture_count(),
17         'changed_at': localtime_to_utc(datetime.now()),
18     }
19
20     type(tag).objects.filter(pk=tag.pk).update(**update_dict)
21
22
23 @task(ignore_result=True)
24 def fix_tree_tags(book):
25     book.fix_tree_tags()
26
27
28 @task
29 def index_book(book_id, book_info=None, **kwargs):
30     from catalogue.models import Book
31     try:
32         return Book.objects.get(id=book_id).search_index(book_info, **kwargs)
33     except Exception, e:
34         print "Exception during index: %s" % e
35         print_exc()
36         raise e
37
38
39 @task(ignore_result=True, rate_limit=settings.CATALOGUE_CUSTOMPDF_RATE_LIMIT)
40 def build_custom_pdf(book_id, customizations, file_name):
41     """Builds a custom PDF file."""
42     from django.core.files import File
43     from django.core.files.storage import DefaultStorage
44     from catalogue.models import Book
45
46     print "will gen %s" % DefaultStorage().path(file_name)
47     if not DefaultStorage().exists(file_name):
48         kwargs = {
49             'cover': True,
50         }
51         if 'no-cover' in customizations:
52             kwargs['cover'] = False
53             customizations.remove('no-cover')
54         pdf = Book.objects.get(pk=book_id).wldocument().as_pdf(
55                 customizations=customizations,
56                 morefloats=settings.LIBRARIAN_PDF_MOREFLOATS,
57                 **kwargs)
58         DefaultStorage().save(file_name, File(open(pdf.get_filename())))