def test_create_persona_preview_image(self, pngcrush_image_mock): addon = addon_factory() addon.modified = self.days_ago(41) # Given an image, a 680x100 and a 32x32 thumbnails need to be generated # and processed with pngcrush. expected_dst1 = tempfile.NamedTemporaryFile(mode='r+w+b', suffix=".png", delete=False, dir=settings.TMP_PATH) expected_dst2 = tempfile.NamedTemporaryFile(mode='r+w+b', suffix=".png", delete=False, dir=settings.TMP_PATH) create_persona_preview_images( src=get_image_path('persona-header.jpg'), full_dst=[expected_dst1.name, expected_dst2.name], set_modified_on=addon.serializable_reference(), ) # pngcrush_image should have been called twice, once for each # destination thumbnail. assert pngcrush_image_mock.call_count == 2 assert pngcrush_image_mock.call_args_list[0][0][0] == ( expected_dst1.name) assert pngcrush_image_mock.call_args_list[1][0][0] == ( expected_dst2.name) assert image_size(expected_dst1.name) == (680, 100) assert image_size(expected_dst2.name) == (32, 32) addon.reload() self.assertCloseToNow(addon.modified)
def test_create_persona_preview_image(self, pngcrush_image_mock): addon = addon_factory() addon.modified = self.days_ago(41) # Given an image, a 680x100 and a 32x32 thumbnails need to be generated # and processed with pngcrush. expected_dst1 = tempfile.NamedTemporaryFile( mode='wb', suffix=".png", delete=False, dir=settings.TMP_PATH) expected_dst2 = tempfile.NamedTemporaryFile( mode='wb', suffix=".png", delete=False, dir=settings.TMP_PATH) create_persona_preview_images( src=get_image_path('persona-header.jpg'), full_dst=[expected_dst1.name, expected_dst2.name], set_modified_on=addon.serializable_reference(), ) # pngcrush_image should have been called twice, once for each # destination thumbnail. assert pngcrush_image_mock.call_count == 2 assert pngcrush_image_mock.call_args_list[0][0][0] == ( expected_dst1.name) assert pngcrush_image_mock.call_args_list[1][0][0] == ( expected_dst2.name) assert image_size(expected_dst1.name) == (680, 100) assert image_size(expected_dst2.name) == (32, 32) addon.reload() self.assertCloseToNow(addon.modified)
def get_preview_sizes(ids, **kw): log.info('[%s@%s] Getting preview sizes for addons starting at id: %s...' % (len(ids), get_preview_sizes.rate_limit, ids[0])) addons = Addon.objects.filter(pk__in=ids).no_transforms() for addon in addons: previews = addon.previews.all() log.info('Found %s previews for: %s' % (previews.count(), addon.pk)) for preview in previews: try: log.info('Getting size for preview: %s' % preview.pk) sizes = { 'thumbnail': image_size(preview.thumbnail_path), 'image': image_size(preview.image_path), } preview.update(sizes=sizes) except Exception as err: log.error('Failed to find size of preview: %s, error: %s' % (addon.pk, err))
def _uploader(resize_size, final_size): img = get_image_path('mozilla.png') original_size = (339, 128) src = tempfile.NamedTemporaryFile(mode='r+b', suffix='.png', delete=False, dir=settings.TMP_PATH) if not isinstance(final_size, list): final_size = [final_size] resize_size = [resize_size] uploadto = user_media_path('addon_icons') try: os.makedirs(uploadto) except OSError: pass for rsize, expected_size in zip(resize_size, final_size): # resize_icon moves the original shutil.copyfile(img, src.name) src_image = Image.open(src.name) assert src_image.size == original_size dest_name = os.path.join(uploadto, '1234') with mock.patch('olympia.amo.utils.pngcrush_image') as pngcrush_mock: return_value = tasks.resize_icon(src.name, dest_name, [rsize]) dest_image = '%s-%s.png' % (dest_name, rsize) assert pngcrush_mock.call_count == 1 assert pngcrush_mock.call_args_list[0][0][0] == dest_image assert image_size(dest_image) == expected_size # original should have been moved to -original orig_image = '%s-original.png' % dest_name assert os.path.exists(orig_image) # Return value of the task should be a dict with an icon_hash key # containing the 8 first chars of the md5 hash of the source file, # which is bb362450b00f0461c6bddc6b97b3c30b. assert return_value == {'icon_hash': 'bb362450'} os.remove(dest_image) assert not os.path.exists(dest_image) os.remove(orig_image) assert not os.path.exists(orig_image) shutil.rmtree(uploadto) assert not os.path.exists(src.name)
def _uploader(resize_size, final_size): img = get_image_path('mozilla.png') original_size = (339, 128) src = tempfile.NamedTemporaryFile( mode='r+w+b', suffix='.png', delete=False, dir=settings.TMP_PATH) if not isinstance(final_size, list): final_size = [final_size] resize_size = [resize_size] uploadto = user_media_path('addon_icons') try: os.makedirs(uploadto) except OSError: pass for rsize, expected_size in zip(resize_size, final_size): # resize_icon moves the original shutil.copyfile(img, src.name) src_image = Image.open(src.name) assert src_image.size == original_size dest_name = os.path.join(uploadto, '1234') with mock.patch('olympia.amo.utils.pngcrush_image') as pngcrush_mock: return_value = tasks.resize_icon(src.name, dest_name, [rsize]) dest_image = '%s-%s.png' % (dest_name, rsize) assert pngcrush_mock.call_count == 1 assert pngcrush_mock.call_args_list[0][0][0] == dest_image assert image_size(dest_image) == expected_size # original should have been moved to -original orig_image = '%s-original.png' % dest_name assert os.path.exists(orig_image) # Return value of the task should be a dict with an icon_hash key # containing the 8 first chars of the md5 hash of the source file, # which is bb362450b00f0461c6bddc6b97b3c30b. assert return_value == {'icon_hash': 'bb362450'} os.remove(dest_image) assert not os.path.exists(dest_image) os.remove(orig_image) assert not os.path.exists(orig_image) shutil.rmtree(uploadto) assert not os.path.exists(src.name)