"""
customizations.sort()
h = hash(tuple(customizations))
- pdf_name = '%s-custom-%s' % (book.slug, h)
++
+ pdf_name = '%s-custom-%s' % (book.fileid(), h)
- pdf_file = models.get_dynamic_path(None, pdf_name, ext='pdf')
+ pdf_file = get_dynamic_path(None, pdf_name, ext='pdf')
++
return pdf_file
customizations - customizations which are passed to LaTeX class file.
file_name - save the pdf file under a different name and DO NOT save it in db.
"""
- from tempfile import NamedTemporaryFile
from os import unlink
from django.core.files import File
- from librarian import pdf
- from catalogue.utils import ORMDocProvider, remove_zip
+ from catalogue.utils import remove_zip
- try:
- pdf_file = NamedTemporaryFile(delete=False)
- pdf.transform(ORMDocProvider(self),
- file_path=str(self.xml_file.path),
- output_file=pdf_file,
- customizations=customizations
- )
-
- if file_name is None:
- # we'd like to be sure not to overwrite changes happening while
- # (timely) pdf generation is taking place (async celery scenario)
- current_self = Book.objects.get(id=self.id)
- current_self.pdf_file.save('%s.pdf' % self.slug, File(open(pdf_file.name)))
- self.pdf_file = current_self.pdf_file
-
- # remove cached downloadables
- remove_zip(settings.ALL_PDF_ZIP)
-
- for customized_pdf in get_existing_customized_pdf(self):
- unlink(customized_pdf)
- else:
- print "save %s to: %s" % (file_name, DefaultStorage().path(file_name))
+ pdf = self.wldocument().as_pdf(customizations=customizations)
- pdf_django_file = File(open(pdf_file.name))
- DefaultStorage().save(file_name, pdf_django_file)
- pdf_django_file.close()
- finally:
- pass
- unlink(pdf_file.name)
+ if file_name is None:
+ # we'd like to be sure not to overwrite changes happening while
+ # (timely) pdf generation is taking place (async celery scenario)
+ current_self = Book.objects.get(id=self.id)
+ current_self.pdf_file.save('%s.pdf' % self.fileid(),
+ File(open(pdf.get_filename())))
+ self.pdf_file = current_self.pdf_file
++
++ # remove cached downloadables
++ remove_zip(settings.ALL_PDF_ZIP)
++
++ for customized_pdf in get_existing_customized_pdf(self):
++ unlink(customized_pdf)
+ else:
+ print "saving %s" % file_name
+ print "to: %s" % DefaultStorage().path(file_name)
+ DefaultStorage().save(file_name, File(open(pdf.get_filename())))
- # remove cached downloadables
- remove_zip(settings.ALL_PDF_ZIP)
- for customized_pdf in get_existing_customized_pdf(self):
- unlink(customized_pdf)
-
def build_mobi(self):
""" (Re)builds the MOBI file.