def unzip(self): """Unzip the chapter and save its images.""" counter = 0 pages = [] dir_path = path.join('series', self.series.slug, str(self.volume), f'{self.number:g}') full_path = settings.MEDIA_ROOT / dir_path if full_path.exists(): rmtree(full_path) full_path.mkdir(parents=True) with ZipFile(self.file) as zf: for name in utils.natsort(zf.namelist()): if zf.getinfo(name).is_dir(): continue counter += 1 data = zf.read(name) dgst = blake2b(data, digest_size=16).hexdigest() filename = dgst + path.splitext(name)[-1] file_path = path.join(dir_path, filename) with open(full_path / filename, 'wb') as img: img.write(data) pages.append( Page(chapter_id=self.id, number=counter, image=file_path)) self.pages.all().delete() self.pages.bulk_create(pages) self.file.delete(save=True)
def _test_filter(self, params: Dict[str, str] = {}, results: List[str] = []): cache.clear() r = self.client.get(self.URL, params) assert r.status_code == 200 if bool(params and results): values = map(attrgetter('title'), r.context['results']) assert natsort(values) == results