def build(self): src_image = Image.open(self.delivery_image.path) format = self.delivery_image.format res = [] # Ex /home/mondomx/disk3/albums/542/images #images_directory = os.path.join(self.disc.full_path(), # album_image_directory(self.album.pk)) images_directory = os.path.join(self.album.full_path(self.disc), main_localsettings.IMAGES_DIRECTORY) for size in metadata.IMAGES_SIZES: # Resize images im = src_image.resize((size, size)) usage = "cover%s" % (size) filename = "%s.%s" % (usage, self.delivery_image.format) filepath = os.path.join(images_directory, filename) im.save(filepath) size = os.path.getsize(filepath) (width, height) = im.size # Mapping to database imagefile_mapper = ImageFileMapper(usage=usage, path=os.path.join("/", main_localsettings.ALBUMS_DIRECTORY, str(self.album.pk), main_localsettings.IMAGES_DIRECTORY, filename), size=size, width=width, height=height, disc=self.disc, album=self.album, format=self.delivery_image.format) res.append(imagefile_mapper.create()) return res
def test_success(self): expected_path = image_path = "/albums/%s/images/%s.%s" % (self.album.pk, self.name, self.format) imagefile_mapper = ImageFileMapper(disc=self.disc, album=self.album, path=expected_path, format=self.format, size=self.size, width=self.width, height=self.height, usage=self.usage) image_file = imagefile_mapper.create() self.assertEqual(self.album, image_file.album) self.assertEqual(self.disc, image_file.disc) self.assertEqual(expected_path, image_file.path) self.assertEqual(self.width, image_file.width) self.assertEqual(self.height, image_file.height) self.assertEqual(self.format, image_file.format) self.assertEqual(self.usage, image_file.usage)