def test_all_no_global(self): self.assertEqual( aliases.all('some_app.Profile', include_global=False), { 'banner': { 'size': (600, 80), 'crop': True }, 'large': { 'size': (200, 200) }, }) self.assertEqual( aliases.all('some_app.Profile.avatar', include_global=False), { 'avatar': { 'size': (80, 80), 'crop': True }, 'banner': { 'size': (600, 80), 'crop': True }, 'large': { 'size': (200, 200) }, 'small': { 'crop': True, 'size': (20, 20) }, })
def test_all(self): self.assertEqual( aliases.all(), {"large": {"size": (500, 500)}, "medium": {"size": (300, 300)}, "small": {"size": (100, 100)}}, ) self.assertEqual( aliases.all("unknown_app"), {"large": {"size": (500, 500)}, "medium": {"size": (300, 300)}, "small": {"size": (100, 100)}}, ) self.assertEqual( aliases.all("some_app.Profile"), { "banner": {"size": (600, 80), "crop": True}, "large": {"size": (200, 200)}, "medium": {"size": (300, 300)}, "small": {"size": (100, 100)}, }, ) self.assertEqual( aliases.all("some_app.Profile.avatar"), { "avatar": {"size": (80, 80), "crop": True}, "banner": {"size": (600, 80), "crop": True}, "large": {"size": (200, 200)}, "medium": {"size": (300, 300)}, "small": {"crop": True, "size": (20, 20)}, }, )
def test_all(self): self.assertEqual( aliases.all(), { 'large': {'size': (500, 500)}, 'medium': {'size': (300, 300)}, 'small': {'size': (100, 100)}, }) self.assertEqual( aliases.all('unknown_app'), { 'large': {'size': (500, 500)}, 'medium': {'size': (300, 300)}, 'small': {'size': (100, 100)}, }) self.assertEqual( aliases.all('easy_thumbnails_tests.Profile'), { 'banner': {'size': (600, 80), 'crop': True}, 'large': {'size': (200, 200)}, 'medium': {'size': (300, 300)}, 'small': {'size': (100, 100)}, }) self.assertEqual( aliases.all('easy_thumbnails_tests.Profile.avatar'), { 'avatar': {'size': (80, 80), 'crop': True}, 'banner': {'size': (600, 80), 'crop': True}, 'large': {'size': (200, 200)}, 'medium': {'size': (300, 300)}, 'small': {'crop': True, 'size': (20, 20)}, })
def test_all(self): self.assertEqual( aliases.all(), { 'large': {'size': (500, 500)}, 'medium': {'size': (300, 300)}, 'small': {'size': (100, 100)}, }) self.assertEqual( aliases.all('unknown_app'), { 'large': {'size': (500, 500)}, 'medium': {'size': (300, 300)}, 'small': {'size': (100, 100)}, }) self.assertEqual( aliases.all('some_app.Profile'), { 'banner': {'size': (600, 80), 'crop': True}, 'large': {'size': (200, 200)}, 'medium': {'size': (300, 300)}, 'small': {'size': (100, 100)}, }) self.assertEqual( aliases.all('some_app.Profile.avatar'), { 'avatar': {'size': (80, 80), 'crop': True}, 'banner': {'size': (600, 80), 'crop': True}, 'large': {'size': (200, 200)}, 'medium': {'size': (300, 300)}, 'small': {'crop': True, 'size': (20, 20)}, })
def test_all_no_global(self): self.assertEqual( aliases.all("some_app.Profile", include_global=False), {"banner": {"size": (600, 80), "crop": True}, "large": {"size": (200, 200)}}, ) self.assertEqual( aliases.all("some_app.Profile.avatar", include_global=False), { "avatar": {"size": (80, 80), "crop": True}, "banner": {"size": (600, 80), "crop": True}, "large": {"size": (200, 200)}, "small": {"crop": True, "size": (20, 20)}, }, )
def test_all_no_global(self): self.assertEqual( aliases.all('some_app.Profile', include_global=False), { 'banner': {'size': (600, 80), 'crop': True}, 'large': {'size': (200, 200)}, }) self.assertEqual( aliases.all('some_app.Profile.avatar', include_global=False), { 'avatar': {'size': (80, 80), 'crop': True}, 'banner': {'size': (600, 80), 'crop': True}, 'large': {'size': (200, 200)}, 'small': {'crop': True, 'size': (20, 20)}, })
def avatar(self): if not self.avatar_image: if self.avatar_url: return {key: self.avatar_url for key in settings.THUMBNAIL_ALIASES['project_name.User.avatar_image']} return None all_options = aliases.all(self.avatar_image) return {key: self.avatar_image[key].url for key in all_options.keys()}
def dehydrate_artwork(self, bundle): artworks = {} if not bundle.obj.icon: return artworks for name, settings in aliases.all(target='radioportal.Show.icon').iteritems(): if not "v2" in name: continue thumb = bundle.obj.icon.get_existing_thumbnail(settings) if thumb: artworks[settings['size'][0]] = thumb.url return artworks
def as_dict(self): attachment = { 'label': self.label, 'url': self.value, 'order': self.order, } if EASY_THUMBNAILS and self.kind == self.IMAGE and self.file is not None: thumbnailer = get_thumbnailer(self.file) for alias, options in aliases.all(target='shopit.Attachment').items(): attachment['url_%s' % alias] = thumbnailer.get_thumbnail(options).url return attachment
def thumbnails(self, request, pk): ''' Return a list of the image's thumbnails ''' image = ExerciseImage.objects.get(pk=pk) thumbnails = {} for alias in aliases.all(): t = get_thumbnailer(image.image) thumbnails[alias] = {'url': t.get_thumbnail(aliases.get(alias)).url, 'settings': aliases.get(alias)} thumbnails['original'] = image.image.url return Response(thumbnails)
def dehydrate(self, bundle): ''' Also send the URLs for the thumbnailed pictures ''' thumbnails = {} for alias in aliases.all(): t = get_thumbnailer(bundle.obj.image) thumbnails[alias] = {'url': t.get_thumbnail(aliases.get(alias)).url, 'settings': aliases.get(alias)} bundle.data['thumbnails'] = thumbnails return bundle
def generate_all_aliases(fieldfile, include_global): """ Generate all of a file's aliases. :param fieldfile: A ``FieldFile`` instance. :param include_global: A boolean which determines whether to generate thumbnails for project-wide aliases in addition to field, model, and app specific aliases. """ all_options = aliases.all(fieldfile, include_global=include_global) if all_options: thumbnailer = get_thumbnailer(fieldfile) for options in all_options.values(): thumbnailer.get_thumbnail(options)
def generate_all_aliases(fieldfile, include_global, force=False): """ Generate all of a file's aliases. :param fieldfile: A ``FieldFile`` instance. :param include_global: A boolean which determines whether to generate thumbnails for project-wide aliases in addition to field, model, and app specific aliases. :param force: Force the generation behaviour by setting the generate param to either True or False as required. """ all_options = aliases.all(fieldfile, include_global=include_global) if all_options: thumbnailer = get_thumbnailer(fieldfile) for options in all_options.values(): thumbnailer.get_thumbnail(options, force=force)
def as_dict(self): attachment = { 'label': self.label, 'url': self.value, 'order': self.order, } if EASY_THUMBNAILS and self.kind == self.IMAGE and self.file is not None: try: thumbnailer = get_thumbnailer(self.file) for alias, options in aliases.all( target='shopit.Attachment').items(): options.update( {'subject_location': self.file.subject_location}) attachment['url_%s' % alias] = thumbnailer.get_thumbnail(options).url except InvalidImageFormatError: pass return attachment
def thumbnails(self, request, pk): """ Return a list of the image's thumbnails """ try: image = ExerciseImage.objects.get(pk=pk) except ExerciseImage.DoesNotExist: return Response([]) thumbnails = {} for alias in aliases.all(): t = get_thumbnailer(image.image) thumbnails[alias] = { 'url': t.get_thumbnail(aliases.get(alias)).url, 'settings': aliases.get(alias) } thumbnails['original'] = image.image.url return Response(thumbnails)
post_delete.connect(delete_file_and_thumbnails, sender=MonFichier) def populate_aliases(app_label): THUMBNAIL_ALIASES = { app_label: { 'cover_medium': { 'size': (270, 200), 'crop': True }, 'cover_small': { 'size': (140, 140), 'crop': True }, 'preview': { 'size': (158, 140), 'crop': True }, } } for target, target_aliases in THUMBNAIL_ALIASES.iteritems(): for alias, options in target_aliases.iteritems(): aliases.set(alias, options, target=target) _app_label = MonAlbum._meta.app_label if not aliases.all(target=_app_label): populate_aliases(_app_label)
def handle(self, **options): self.only_info = options.get('info') for app in ORPHANED_APPS_MEDIABASE_DIRS.keys(): if (ORPHANED_APPS_MEDIABASE_DIRS[app].has_key('root')): needed_files = [] all_files = [] possible_empty_dirs = [] empty_dirs = [] total_freed_bytes = 0 total_freed = '0' delete_files = [] skip = ORPHANED_APPS_MEDIABASE_DIRS[app].get('skip', ()) exclude = ORPHANED_APPS_MEDIABASE_DIRS[app].get('exclude', ()) media_root = ORPHANED_APPS_MEDIABASE_DIRS[app].get( 'media_root', settings.MEDIA_ROOT) for model in ContentType.objects.filter(app_label=app): mc = model.model_class() if mc is None: continue fields = [] for field in mc._meta.fields: if (field.get_internal_type() == 'FileField' or field.get_internal_type() == 'ImageField'): fields.append(field.name) # we have found a model with FileFields if len(fields) > 0: files = mc.objects.all().values_list(*fields) options = aliases.all(include_global=True) with_aliases = (os.path.join(media_root, file) for file in chain.from_iterable((get_aliases(filename, options) for filename in filter(None, chain.from_iterable(files))))) needed_files.extend(with_aliases) # traverse root folder and store all files and empty directories def should_skip(dir): for skip_dir in skip: if dir.startswith(skip_dir): return True return False # process each root of the app app_roots = ORPHANED_APPS_MEDIABASE_DIRS[app]['root'] if isinstance(app_roots, basestring): # backwards compatibility app_roots = [app_roots] for app_root in app_roots: for root, dirs, files in os.walk(app_root): if should_skip(root): continue if len(files) > 0: for basename in files: if basename not in exclude: all_files.append(os.path.join(root, basename)) elif not os.path.samefile(root, app_root): possible_empty_dirs.append(root) # ignore empty dirs with subdirs + files for ed in possible_empty_dirs: dont_delete = False for files in all_files: try: if files.index(ed) == 0: dont_delete = True except ValueError: pass for skip_dir in skip: try: if (skip_dir.index(ed) == 0): dont_delete = True except ValueError: pass if not dont_delete: empty_dirs.append(ed) # select deleted files (delete_files = all_files - needed_files) aa = set(all_files) delete_files = list(aa.difference(needed_files)) delete_files.sort() empty_dirs.sort() empty_dirs = set(empty_dirs) #remove possible duplicates # to be freed for df in delete_files: total_freed_bytes += os.path.getsize(df) total_freed = "%0.1f MB" % (total_freed_bytes/(1024*1024.0)) # only show if (self.only_info): print "\r\n=== %s ===" % app if len(empty_dirs) > 0: print "\r\nFollowing empty dirs will be removed:\r\n" for file in empty_dirs: print " ", file if len(delete_files) > 0: print "\r\nFollowing files will be deleted:\r\n" for file in delete_files: print " ", file print "\r\nTotally %s files will be deleted, and "\ "totally %s will be freed.\r\n" % (len(delete_files), total_freed) else: print "No files to delete!" # DELETE NOW! else: for file in delete_files: os.remove(file) for dirs in empty_dirs: shutil.rmtree(dirs, ignore_errors=True)
class Meta: verbose_name_plural = "Mes Albums" def __unicode__(self): return self.name def delete_file_and_thumbnails(instance, **kwargs): instance.file.delete() post_delete.connect(delete_file_and_thumbnails, sender=MonFichier) def populate_aliases(app_label): THUMBNAIL_ALIASES = { app_label: { "cover_medium": {"size": (270, 200), "crop": True}, "cover_small": {"size": (140, 140), "crop": True}, "preview": {"size": (158, 140), "crop": True}, } } for target, target_aliases in THUMBNAIL_ALIASES.iteritems(): for alias, options in target_aliases.iteritems(): aliases.set(alias, options, target=target) _app_label = MonAlbum._meta.app_label if not aliases.all(target=_app_label): populate_aliases(_app_label)