def image_resize(file_path, key_type, key_id): """ key_type should be one of ImageKeyType.choices """ if not file_path: return '' elif isinstance(file_path, FieldFile): file_path = file_path.path elif not isinstance(file_path, str): return '' key_type = key_type.upper() cache_key = get_cache_key(key_type, key_id) url = cache.get(cache_key) if url: return url try: image = Image.objects.only('file').get(key_type=key_type, key_id=key_id) if not os.path.exists(image.file.path): image.regenerate() except Image.DoesNotExist: image = Image(key_type=key_type, key_id=key_id) try: image.save(file_path) except IntegrityError: image = Image.objects.only('file').get(key_type=key_type, key_id=key_id) cache.set(cache_key, image.file.url, IMAGE_CACHE_TIMEOUT) return image.file.url
def test_manga_post_save(self): self.manga.cover = SimpleUploadedFile('test.png', self.create_test_image_file().getvalue()) self.manga.save(updated_by=self.user) image1 = Image(key_type=ImageKeyType.MANGA_COVER, key_id=self.manga.id) image1.save(self.manga.cover.path) self.manga.save(updated_by=self.user) self.assertFalse(Image.objects.filter(id=image1.id).exists())
def migrate_image_resize(file_path, key_type, key_id): if not os.path.exists(file_path): return if Image.objects.filter(key_type=key_type, key_id=key_id).exists(): return try: image = Image(key_type=key_type, key_id=key_id) image.save(file_path) except Exception as e: logger.warn(str(e))
def test_image_delete(self): self.manga.cover = SimpleUploadedFile('test.jpg', self.create_test_image_file().getvalue()) self.manga.save(self.user) image = Image(key_type=ImageKeyType.MANGA_THUMB, key_id=1) image.save(self.manga.cover.path) path = image.file.path self.assertTrue(os.path.exists(path)) image.delete() self.assertFalse(os.path.exists(path))