class DependencyTesting(models.Model): # test automatic palette conversion image_1 = fields.ImageField( upload_to=UploadTo(name='image_1'), dependencies=[ # convert self to palette mode, and than to a different pallete mode FileDependency(processor=processors.ImageProcessor( format=processors.ImageFormat('BMP', mode='P'), scale={'width': 50})), FileDependency(suffix='gif', processor=processors.ImageProcessor( format=processors.ImageFormat('GIF', mode='P'))) ]) image_2 = fields.ImageField( upload_to=UploadTo(name='image_2'), dependencies=[ # testing setting a dependency on another FileField FileDependency(attname='image_3', processor=processors.ImageProcessor( format=processors.ImageFormat('PNG'), scale={'width': 100})), FileDependency(attname='image_4', processor=processors.ImageProcessor( format=processors.ImageFormat('PNG'), scale={'width': 150})), ]) image_3 = models.ImageField(upload_to=UploadTo(name='image_3')) image_4 = fields.ImageField(upload_to=UploadTo(name='image_4'))
class RenameFileTesting(models.Model): label = fields.CharField(max_length=32, dependencies=[ FileDependency(attname='dynamic_name_file', processor=processors.RenameFileProcessor()) ]) dynamic_name_file = models.FileField( upload_to=UploadTo(name=_name_getter, add_pk=False))
def test_upload_to_6(self): upload_to = UploadTo(generator=True) instance = FileTesting() self.assertRegexpMatches( upload_to(instance, 'bar.jpg'), r'test_app/filetesting/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}.jpg' )
def test_upload_to_3(self): upload_to = UploadTo(filename='foo.txt', parent_field_name='foo_bar') instance = FileTesting() instance_parent = FileTesting() instance_parent.pk = 46 instance.foo_bar = instance_parent self.assertEqual(upload_to(instance, 'bar.jpg'), 'test_app/filetesting/46/foo.txt')
class ImageTesting(models.Model): image_1 = models.ImageField( upload_to='image_1', width_field='image_1_width', height_field='image_1_height') image_1_width = models.IntegerField(null=True) image_1_height = models.IntegerField(null=True) image_2 = fields.ImageField( upload_to='image_2', width_field='image_2_width', height_field='image_2_height') image_2_width = models.IntegerField(null=True) image_2_height = models.IntegerField(null=True) image_3 = fields.ImageField(upload_to=UploadTo(name='image_3'), dependencies=[ FileDependency(suffix='png', processor=processors.ImageProcessor( format=processors.ImageFormat('PNG', mode='P'), scale={'max_width': 200, 'max_height':150})), FileDependency(suffix='bmp', processor=processors.ImageProcessor( format='BMP', scale={'width': 50})), FileDependency(suffix='eps', processor=processors.ImageProcessor( format='EPS', scale={'width': 50})), FileDependency(suffix='gif', processor=processors.ImageProcessor( format='GIF', scale={'width': 50})), FileDependency(suffix='im', processor=processors.ImageProcessor( format='IM', scale={'width': 50})), FileDependency(suffix='jpeg', processor=processors.ImageProcessor( format=processors.ImageFormat('JPEG', save_kwargs={'quality':95}), scale={'width': 50})), FileDependency(suffix='msp', processor=processors.ImageProcessor( format='MSP', scale={'width': 50})), FileDependency(suffix='pcx', processor=processors.ImageProcessor( format='PCX', scale={'width': 50})), FileDependency(suffix='ppm', processor=processors.ImageProcessor( format='PPM', scale={'width': 50})), FileDependency(suffix='tiff', processor=processors.ImageProcessor( format=processors.ImageFormat('TIFF', mode='P'))), FileDependency(suffix='resized', processor=processors.ImageProcessor( scale={'width': 50})), ]) # test problematic format image_4 = fields.ImageField(upload_to=UploadTo(name='image_4'), dependencies=[ FileDependency(suffix='jpeg2000', processor=processors.ImageProcessor(format='JPEG2000')), ]) image_5 = fields.ImageField(upload_to=UploadTo(name='image_5'), dependencies=[ FileDependency(suffix='jpeg', processor=processors.WandImageProcessor( format='JPEG', scale={'max_width': 200, 'max_height': 150})) ])
def test_upload_to_1(self): upload_to = UploadTo(basefolder='base', subfolder='subfolder', name='foo', ext='txt', app_label='app_label', model_name='model_name') self.assertEqual( upload_to, UploadTo(basefolder='base', subfolder='subfolder', name='foo', ext='txt', app_label='app_label', model_name='model_name')) instance = FileTesting() self.assertEqual(upload_to(instance, 'bar.jpg'), 'base/app_label/model_name/subfolder/foo.txt')
class VideoTesting(models.Model): video_1 = fields.FileField( upload_to=UploadTo(name='video_1'), dependencies=[ # testing html tag setter Dependency(suffix='html_tag', default=video_tag_processor), # testing conversion to webm FileDependency(suffix='webm', async_=True, processor=processors.FFMPEGProcessor( format='webm', vcodec='libvpx', vbitrate='128k', maxrate='128k', bufsize='256k', width='trunc(oh*a/2)*2', height=240, threads=4, acodec='libvorbis', abitrate='96k')), # testing conversion to mp4 FileDependency(suffix='mp4', async_=True, processor=processors.FFMPEGProcessor( format='mp4', vcodec='libx264', vbitrate='128k', maxrate='128k', bufsize='256k', width='trunc(oh*a/2)*2', height=240, threads=0, acodec='libmp3lame', abitrate='96k')), ]) def has_upload_permission(self, user, field_name=None): is_auth = user.is_authenticated if django.VERSION >= (1, 10) else user.is_authenticated() return (field_name == 'video_1' and is_auth and user.username == 'test_user')
class VideoTesting(models.Model): video_1 = fields.FileField( upload_to=UploadTo(name='video_1'), dependencies=[ # testing html tag setter Dependency(suffix='html_tag', default=video_tag_processor), # testing conversion to webm FileDependency(suffix='webm', async=True, processor=processors.FFMPEGProcessor( format='webm', vcodec='libvpx', vbitrate='128k', maxrate='128k', bufsize='256k', width='trunc(oh*a/2)*2', height=240, threads=4, acodec='libvorbis', abitrate='96k')), # testing conversion to mp4 FileDependency(suffix='mp4', async=True, processor=processors.FFMPEGProcessor( format='mp4', vcodec='libx264', vbitrate='128k', maxrate='128k', bufsize='256k', width='trunc(oh*a/2)*2', height=240, threads=0, acodec='libmp3lame', abitrate='96k')), ])
def test_upload_to_5(self): upload_to = UploadTo(generator=lambda: 'foo') instance = FileTesting() self.assertEqual(upload_to(instance, 'bar.jpg'), 'test_app/filetesting/foo.jpg')
def test_upload_to_4(self): upload_to = UploadTo(name='foo', ext='') instance = FileTesting() self.assertEqual(upload_to(instance, 'bar.jpg'), 'test_app/filetesting/foo')