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.
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 from wolnelektury.utils import localtime_to_utc
11 from waiter.models import WaitedFile
13 task_logger = get_task_logger(__name__)
16 # TODO: move to model?
19 'changed_at': localtime_to_utc(datetime.now()),
22 type(tag).objects.filter(pk=tag.pk).update(**update_dict)
26 def index_book(book_id, book_info=None, **kwargs):
27 from catalogue.models import Book
29 return Book.objects.get(id=book_id).search_index(book_info, **kwargs)
31 print "Exception during index: %s" % e
36 @task(ignore_result=True, rate_limit=settings.CATALOGUE_CUSTOMPDF_RATE_LIMIT)
37 def build_custom_pdf(book_id, customizations, file_name, waiter_id=None):
38 """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
44 task_logger.info(DefaultStorage().path(file_name))
45 if not DefaultStorage().exists(file_name):
49 if 'no-cover' in customizations:
50 kwargs['cover'] = False
51 customizations.remove('no-cover')
52 pdf = Book.objects.get(pk=book_id).wldocument().as_pdf(
53 customizations=customizations,
54 morefloats=settings.LIBRARIAN_PDF_MOREFLOATS,
56 DefaultStorage().save(file_name, File(open(pdf.get_filename())))
58 if waiter_id is not None:
59 WaitedFile.objects.filter(pk=waiter_id).delete()