X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/71209be8f9c399340bddb819f71e99ecf116187b..0aafa951948f151fb74c704d7901d9bbc136ac74:/apps/catalogue/utils.py?ds=sidebyside diff --git a/apps/catalogue/utils.py b/apps/catalogue/utils.py index 185f5fa34..29f40d16b 100644 --- a/apps/catalogue/utils.py +++ b/apps/catalogue/utils.py @@ -9,23 +9,18 @@ import re import time from base64 import urlsafe_b64encode -from django.http import HttpResponse, HttpResponseRedirect, Http404, HttpResponsePermanentRedirect +from django.http import HttpResponse from django.core.files.uploadedfile import UploadedFile -from django.core.files.base import File from django.core.files.storage import DefaultStorage from django.utils.encoding import force_unicode from django.utils.hashcompat import sha_constructor from django.conf import settings -from celery.task import task from os import mkdir, path, unlink from errno import EEXIST, ENOENT from fcntl import flock, LOCK_EX from zipfile import ZipFile -from traceback import print_exc from reporting.utils import read_chunks -from celery.task import task -import catalogue.models # Use the system (hardware-based) random number generator if it exists. if hasattr(random, 'SystemRandom'): @@ -79,12 +74,12 @@ class LockFile(object): try: unlink(self.lockname) except OSError as oe: - if oe.errno != oe.EEXIST: + if oe.errno != EEXIST: raise oe self.lock.close() -@task +#@task def create_zip(paths, zip_slug): """ Creates a zip in MEDIA_ROOT/zip directory containing files from path. @@ -140,25 +135,6 @@ class AttachmentHttpResponse(HttpResponse): for chunk in read_chunks(f): self.write(chunk) -@task -def async_build_pdf(book_id, customizations, file_name): - """ - A celery task to generate pdf files. - Accepts the same args as Book.build_pdf, but with book id as first parameter - instead of Book instance - """ - try: - book = catalogue.models.Book.objects.get(id=book_id) - print "will gen %s" % DefaultStorage().path(file_name) - if not DefaultStorage().exists(file_name): - book.build_pdf(customizations=customizations, file_name=file_name) - print "done." - except Exception, e: - print "Error during pdf creation: %s" % e - print_exc - raise e - - class MultiQuerySet(object): def __init__(self, *args, **kwargs): self.querysets = args @@ -260,3 +236,24 @@ def truncate_html_words(s, num, end_text='...'): out += '' % tag # Return string return out + + +def customizations_hash(customizations): + customizations.sort() + return hash(tuple(customizations)) + + +def get_customized_pdf_path(book, customizations): + """ + Returns a MEDIA_ROOT relative path for a customized pdf. The name will contain a hash of customization options. + """ + h = customizations_hash(customizations) + return 'book/%s/%s-custom-%s.pdf' % (book.slug, book.slug, h) + + +def clear_custom_pdf(book): + """ + Returns a list of paths to generated customized pdf of a book + """ + from waiter.utils import clear_cache + clear_cache('book/%s' % book.slug)