merge new librarian changes with custom pdf fix
authorMarcin Koziej <marcin.koziej@nowoczesnapolska.org.pl>
Mon, 12 Dec 2011 08:18:49 +0000 (09:18 +0100)
committerMarcin Koziej <marcin.koziej@nowoczesnapolska.org.pl>
Mon, 12 Dec 2011 08:18:49 +0000 (09:18 +0100)
1  2 
apps/catalogue/models.py

@@@ -202,8 -202,8 +202,10 @@@ def get_customized_pdf_path(book, custo
      """
      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
  
  
@@@ -498,42 -552,29 +554,30 @@@ class Book(models.Model)
          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.