def save(self, *args, **kwargs): from slughifi import slughifi from catalogue.utils import ExistingFile, remove_zip try: old = BookMedia.objects.get(pk=self.pk) except BookMedia.DoesNotExist: old = None else: # if name changed, change the file name, too if slughifi(self.name) != slughifi(old.name): self.file.save(None, ExistingFile(self.file.path), save=False, leave=True) super(BookMedia, self).save(*args, **kwargs) # remove the zip package for book with modified media if old: remove_zip("%s_%s" % (old.book.slug, old.type)) remove_zip("%s_%s" % (self.book.slug, self.type)) extra_info = self.extra_info extra_info.update(self.read_meta()) self.extra_info = extra_info self.source_sha1 = self.read_source_sha1(self.file.path, self.type) return super(BookMedia, self).save(*args, **kwargs)
def save(self, *args, **kwargs): from catalogue.utils import ExistingFile, remove_zip try: old = BookMedia.objects.get(pk=self.pk) except BookMedia.DoesNotExist: old = None else: # if name changed, change the file name, too if slughifi(self.name) != slughifi(old.name): self.file.save(None, ExistingFile(self.file.path), save=False, leave=True) super(BookMedia, self).save(*args, **kwargs) # remove the zip package for book with modified media if old: remove_zip("%s_%s" % (old.book.slug, old.type)) remove_zip("%s_%s" % (self.book.slug, self.type)) extra_info = self.extra_info if isinstance(extra_info, basestring): # Walkaround for weird jsonfield 'no-decode' optimization. extra_info = json.loads(extra_info) extra_info.update(self.read_meta()) self.extra_info = extra_info self.source_sha1 = self.read_source_sha1(self.file.path, self.type) return super(BookMedia, self).save(*args, **kwargs)
def save(self, *args, **kwargs): from fnpdjango.utils.text.slughifi import slughifi from catalogue.utils import ExistingFile, remove_zip try: old = BookMedia.objects.get(pk=self.pk) except BookMedia.DoesNotExist: old = None else: # if name changed, change the file name, too if slughifi(self.name) != slughifi(old.name): self.file.save(None, ExistingFile(self.file.path), save=False, leave=True) super(BookMedia, self).save(*args, **kwargs) # remove the zip package for book with modified media if old: remove_zip("%s_%s" % (old.book.slug, old.type)) remove_zip("%s_%s" % (self.book.slug, self.type)) extra_info = self.extra_info if isinstance(extra_info, basestring): # Walkaround for weird jsonfield 'no-decode' optimization. extra_info = json.loads(extra_info) extra_info.update(self.read_meta()) self.extra_info = extra_info self.source_sha1 = self.read_source_sha1(self.file.path, self.type) return super(BookMedia, self).save(*args, **kwargs)
def build(self, fieldfile): book = fieldfile.instance out = self.transform(book.wldocument(), fieldfile) fieldfile.save(None, File(open(out.get_filename())), save=False) if book.pk is not None: type(book).objects.filter(pk=book.pk).update( **{fieldfile.field.attname: fieldfile}) if fieldfile.field.format_name in app_settings.FORMAT_ZIPS: remove_zip(app_settings.FORMAT_ZIPS[fieldfile.field.format_name])
def build(self, fieldfile): book = fieldfile.instance out = self.transform(book.wldocument(), fieldfile) fieldfile.save(None, File(open(out.get_filename())), save=False) if book.pk is not None: type(book).objects.filter(pk=book.pk).update(**{ fieldfile.field.attname: fieldfile }) if fieldfile.field.format_name in app_settings.FORMAT_ZIPS: remove_zip(app_settings.FORMAT_ZIPS[fieldfile.field.format_name])
def test_zip_audiobooks(self): paths = [ (None, join(dirname(__file__), "files/fraszka-do-anusie.xml")), (None, join(dirname(__file__), "files/fraszki.xml")), ] url = utils.create_zip(paths, 'test-zip-slug') self.assertEqual("zip/test-zip-slug.zip", url) self.assertTrue(exists(join(settings.MEDIA_ROOT, url))) utils.remove_zip('test-zip-slug') self.assertFalse(exists(join(settings.MEDIA_ROOT, url)))
def build(self, fieldfile): book = fieldfile.instance out = self.transform(book.wldocument(), fieldfile) fieldfile.save(None, File(open(out.get_filename(), 'rb')), save=False) self.set_file_permissions(fieldfile) if book.pk is not None: books = type(book).objects.filter(pk=book.pk) books.update(**{ fieldfile.field.attname: fieldfile }) for book in books: book.save() # just to trigger post-save if fieldfile.field.format_name in app_settings.FORMAT_ZIPS: remove_zip(app_settings.FORMAT_ZIPS[fieldfile.field.format_name])
def build_mobi(book_id): """(Re)builds the MOBI file for a book.""" from django.core.files import File from catalogue.models import Book from catalogue.utils import remove_zip mobi = Book.objects.get(pk=book_id).wldocument().as_mobi() # Save the file in new instance. Building MOBI takes time and we don't want # to overwrite any interim changes. book = Book.objects.get(id=book_id) book.mobi_file.save('%s.mobi' % book.slug, File(open(mobi.get_filename()))) # remove zip with all mobi files remove_zip(settings.ALL_MOBI_ZIP)
def build_pdf(book_id): """(Re)builds the pdf file for a book.""" from django.core.files import File from catalogue.models import Book from catalogue.utils import remove_zip from waiter.utils import clear_cache pdf = Book.objects.get(pk=book_id).wldocument().as_pdf( morefloats=settings.LIBRARIAN_PDF_MOREFLOATS) # Save the file in new instance. Building PDF takes time and we don't want # to overwrite any interim changes. book = Book.objects.get(id=book_id) book.pdf_file.save('%s.pdf' % book.slug, File(open(pdf.get_filename()))) # Remove cached downloadables remove_zip(settings.ALL_PDF_ZIP) clear_cache(book.slug)
def save(self, *args, **kwargs): from catalogue.utils import ExistingFile, remove_zip parts_count = BookMedia.objects.filter(book=self.book, type=self.type).count() if parts_count == 1: self.name = self.book.pretty_title() else: no = ('%02d' if parts_count < 100 else '%03d') % self.index self.name = '%s. %s' % (no, self.book.pretty_title()) if self.part_name: self.name += ', ' + self.part_name try: old = BookMedia.objects.get(pk=self.pk) except BookMedia.DoesNotExist: old = None else: # if name changed, change the file name, too if slughifi(self.name) != slughifi(old.name): self.file.save(None, ExistingFile(self.file.path), save=False, leave=True) super(BookMedia, self).save(*args, **kwargs) # remove the zip package for book with modified media if old: remove_zip("%s_%s" % (old.book.slug, old.type)) remove_zip("%s_%s" % (self.book.slug, self.type)) extra_info = self.extra_info if isinstance(extra_info, basestring): # Walkaround for weird jsonfield 'no-decode' optimization. extra_info = json.loads(extra_info) extra_info.update(self.read_meta()) self.extra_info = extra_info self.source_sha1 = self.read_source_sha1(self.file.path, self.type) return super(BookMedia, self).save(*args, **kwargs)
def save(self, parts_count=None, *args, **kwargs): from catalogue.utils import ExistingFile, remove_zip if not parts_count: parts_count = 1 + BookMedia.objects.filter(book=self.book, type=self.type).exclude(pk=self.pk).count() if parts_count == 1: self.name = self.book.pretty_title() else: no = ('%02d' if parts_count < 100 else '%03d') % self.index self.name = '%s. %s' % (no, self.book.pretty_title()) if self.part_name: self.name += ', ' + self.part_name try: old = BookMedia.objects.get(pk=self.pk) except BookMedia.DoesNotExist: old = None else: # if name changed, change the file name, too if slugify(self.name) != slugify(old.name): self.file.save(None, ExistingFile(self.file.path), save=False) super(BookMedia, self).save(*args, **kwargs) # remove the zip package for book with modified media if old: remove_zip("%s_%s" % (old.book.slug, old.type)) remove_zip("%s_%s" % (self.book.slug, self.type)) extra_info = self.extra_info if isinstance(extra_info, str): # Walkaround for weird jsonfield 'no-decode' optimization. extra_info = json.loads(extra_info) extra_info.update(self.read_meta()) self.extra_info = extra_info self.source_sha1 = self.read_source_sha1(self.file.path, self.type) return super(BookMedia, self).save(*args, **kwargs)