def test_thumbnail(self): model = TestThumbnailFieldModel(photo=RELATIVE_PIC_NAME) thumb = model.photo.thumbnail tag = model.photo.thumbnail_tag base_name = RELATIVE_PIC_NAME.replace('.', '_') expected_filename = os.path.join(settings.MEDIA_ROOT, '%s_50x50_q85.jpg' % base_name) self.verify_thumbnail((50, 37), thumb, expected_filename) expected_tag = ('<img src="%s" width="50" height="37" alt="" />' % '/'.join([settings.MEDIA_URL.rstrip('/'), '%s_50x50_q85.jpg' % base_name])) self.assertEqual(tag, expected_tag)
def testFilenameGeneration(self): basename = RELATIVE_PIC_NAME.replace('.', '_') # Basic filename thumb = DjangoThumbnail(relative_source=RELATIVE_PIC_NAME, requested_size=(240, 120)) expected = os.path.join(settings.MEDIA_ROOT, basename) expected += '_240x120_q85.jpg' self.verify_thumbnail((160, 120), thumb, expected_filename=expected) # Changed quality and cropped thumb = DjangoThumbnail(relative_source=RELATIVE_PIC_NAME, requested_size=(240, 120), opts=['crop'], quality=95) expected = os.path.join(settings.MEDIA_ROOT, basename) expected += '_240x120_crop_q95.jpg' self.verify_thumbnail((240, 120), thumb, expected_filename=expected) # All options on processors = dynamic_import(get_thumbnail_setting('PROCESSORS')) valid_options = get_valid_options(processors) thumb = DjangoThumbnail(relative_source=RELATIVE_PIC_NAME, requested_size=(240, 120), opts=valid_options) expected = (os.path.join(settings.MEDIA_ROOT, basename) + '_240x120_' 'autocrop_bw_crop_detail_max_sharpen_upscale_q85.jpg') self.verify_thumbnail((240, 120), thumb, expected_filename=expected) # Different basedir basedir = 'sorl-thumbnail-test-basedir' self.change_settings.change({'BASEDIR': basedir}) thumb = DjangoThumbnail(relative_source=self.pic_subdir, requested_size=(240, 120)) expected = os.path.join(basedir, self.sub_dir, basename) expected += '_240x120_q85.jpg' self.verify_thumbnail((160, 120), thumb, expected_filename=expected) # Different subdir self.change_settings.change({'BASEDIR': '', 'SUBDIR': 'subdir'}) thumb = DjangoThumbnail(relative_source=self.pic_subdir, requested_size=(240, 120)) expected = os.path.join(settings.MEDIA_ROOT, os.path.basename(self.sub_dir), 'subdir', basename) expected += '_240x120_q85.jpg' self.verify_thumbnail((160, 120), thumb, expected_filename=expected) # Different prefix self.change_settings.change({'SUBDIR': '', 'PREFIX': 'prefix-'}) thumb = DjangoThumbnail(relative_source=self.pic_subdir, requested_size=(240, 120)) expected = os.path.join(self.sub_dir, 'prefix-' + basename) expected += '_240x120_q85.jpg' self.verify_thumbnail((160, 120), thumb, expected_filename=expected)
def test_thumbnail(self): model = TestThumbnailFieldModel() source = ContentFile(open(PIC_NAME).read()) dest_name = 'sorl-thumbnail-test_dest.jpg' model.avatar.save(dest_name, source, save=False) expected_filename = os.path.join(model.avatar.path) self.verify_thumbnail((300, 225), expected_filename=expected_filename) tag = model.avatar.thumbnail_tag base_name = RELATIVE_PIC_NAME.replace('.', '_') expected_tag = ( '<img src="%s" width="300" height="225" alt="" />' % '/'.join([settings.MEDIA_URL.rstrip('/'), 'test', dest_name])) self.assertEqual(tag, expected_tag)
def test_thumbnail(self): model = TestThumbnailFieldModel(photo=RELATIVE_PIC_NAME) thumb = model.photo.thumbnail tag = model.photo.thumbnail_tag base_name = RELATIVE_PIC_NAME.replace('.', '_') expected_filename = os.path.join(settings.MEDIA_ROOT, '%s_50x50_q85.jpg' % base_name) self.verify_thumbnail((50, 37), thumb, expected_filename) expected_tag = ('<img src="%s" width="50" height="37" alt="" />' % '/'.join([ settings.MEDIA_URL.rstrip('/'), '%s_50x50_q85.jpg' % base_name ])) self.assertEqual(tag, expected_tag)
def test_thumbnail(self): model = TestThumbnailFieldModel() source = SimpleUploadedFile('_', open(PIC_NAME).read()) dest_name = 'sorl-thumbnail-test_dest.jpg' model.avatar.save(dest_name, source, save=False) expected_filename = os.path.join(model.avatar.path) self.verify_thumbnail((300, 225), expected_filename=expected_filename) tag = model.avatar.thumbnail_tag base_name = RELATIVE_PIC_NAME.replace('.', '_') expected_tag = ('<img src="%s" width="300" height="225" alt="" />' % '/'.join([settings.MEDIA_URL.rstrip('/'), 'test', dest_name])) self.assertEqual(tag, expected_tag)
def testFilenameGeneration(self): basename = RELATIVE_PIC_NAME.replace('.', '_') # Basic filename thumb = DjangoThumbnail(path_or_filefield=RELATIVE_PIC_NAME, requested_size=(240, 120)) expected = os.path.join(settings.MEDIA_ROOT, basename) expected += '_240x120_q85.jpg' self.verify_thumbnail((160, 120), thumb, expected_filename=expected) # Changed quality and cropped thumb = DjangoThumbnail(path_or_filefield=RELATIVE_PIC_NAME, requested_size=(240, 120), opts=['crop'], quality=95) expected = os.path.join(settings.MEDIA_ROOT, basename) expected += '_240x120_crop_q95.jpg' self.verify_thumbnail((240, 120), thumb, expected_filename=expected) # All options on thumb = DjangoThumbnail(path_or_filefield=RELATIVE_PIC_NAME, requested_size=(240, 120), opts=VALID_OPTIONS) expected = os.path.join(settings.MEDIA_ROOT, basename) expected += '_240x120_bw_autocrop_crop_upscale_detail_sharpen_q85.jpg' self.verify_thumbnail((240, 120), thumb, expected_filename=expected) # Different basedir basedir = 'sorl-thumbnail-test-basedir' self.change_settings.change({'BASEDIR': basedir}) thumb = DjangoThumbnail(path_or_filefield=self.pic_subdir, requested_size=(240, 120)) expected = os.path.join(basedir, self.sub_dir, basename) expected += '_240x120_q85.jpg' self.verify_thumbnail((160, 120), thumb, expected_filename=expected) # Different subdir self.change_settings.change({'BASEDIR': '', 'SUBDIR': 'subdir'}) thumb = DjangoThumbnail(path_or_filefield=self.pic_subdir, requested_size=(240, 120)) expected = os.path.join(settings.MEDIA_ROOT, os.path.basename(self.sub_dir), 'subdir', basename) expected += '_240x120_q85.jpg' self.verify_thumbnail((160, 120), thumb, expected_filename=expected) # Different prefix self.change_settings.change({'SUBDIR': '', 'PREFIX': 'prefix-'}) thumb = DjangoThumbnail(path_or_filefield=self.pic_subdir, requested_size=(240, 120)) expected = os.path.join(self.sub_dir, 'prefix-'+basename) expected += '_240x120_q85.jpg' self.verify_thumbnail((160, 120), thumb, expected_filename=expected)
def testAlternateExtension(self): basename = RELATIVE_PIC_NAME.replace('.', '_') # Control JPG thumb = DjangoThumbnail(relative_source=RELATIVE_PIC_NAME, requested_size=(240, 120)) expected = os.path.join(settings.MEDIA_ROOT, basename) expected += '_240x120_q85.jpg' expected_jpg = expected self.verify_thumbnail((160, 120), thumb, expected_filename=expected) # Test PNG thumb = DjangoThumbnail(relative_source=RELATIVE_PIC_NAME, requested_size=(240, 120), extension='png') expected = os.path.join(settings.MEDIA_ROOT, basename) expected += '_240x120_q85.png' self.verify_thumbnail((160, 120), thumb, expected_filename=expected) # Compare the file size to make sure it's not just saving as a JPG with # a different extension. self.assertNotEqual(os.path.getsize(expected_jpg), os.path.getsize(expected))