def test_manga_archive_delete(self): ma = MangaArchive(manga=self.manga) ma.file.save('sample-file.zip', File(BytesIO()), save=False) ma.save() path = ma.file.path self.assertTrue(os.path.exists(path)) ma.delete() self.assertFalse(os.path.exists(path))
def generate(cls, manga): """ This method is locked for up to 30 seconds (per manga) until it returns. This prevents the server from falling over due to stampede effect. """ if not cls.acquire_lock(manga): return try: manga_archive = MangaArchive.objects.get(manga=manga) DeletedFile.objects.create(path=manga_archive.file.path) except MangaArchive.DoesNotExist: manga_archive = MangaArchive(manga=manga) manga_zip_file = BytesIO() manga_zip = zipfile.ZipFile(manga_zip_file, 'w') # write manga pages into zip file for page in MangaPage.objects.filter(manga=manga).order_by('page'): if not page.image: continue extension = get_image_extension(page.image) manga_zip.write(page.image.path, '{:03d}.{}'.format(page.page, extension)) # write info.txt into zip file info_text = manga.info_text manga_zip.writestr('info.txt', info_text) manga_zip.close() manga_archive.name = manga.archive_name manga_archive.file = UploadedFile(manga_zip_file, 'archive.zip') manga_archive.save() manga_zip_file.close() cls.release_lock(manga) return manga_archive