def get_storage(self, storage): pickled = pickle.dumps(storage) hash = utils.get_storage_hash(pickled) if hash not in self._storage_cache: self._storage_cache[hash] = self.get_or_create(hash=hash, defaults=dict(pickle=pickled))[0] return self._storage_cache[hash]
def get_file(self, storage, name, create=False, update_modified=None, check_cache_miss=False, **kwargs): kwargs.update(dict(storage_hash=utils.get_storage_hash(storage), name=name)) if create: if update_modified: defaults = kwargs.setdefault('defaults', {}) defaults['modified'] = update_modified obj, created = self.get_or_create(**kwargs) else: created = False kwargs.pop('defaults', None) try: manager = self._get_thumbnail_manager() obj = manager.get(**kwargs) except self.model.DoesNotExist: if check_cache_miss and storage.exists(name): # File already in storage, update cache. Using # get_or_create again in case this was updated while # storage.exists was running. obj, created = self.get_or_create(**kwargs) else: return if update_modified and not created: if obj.modified != update_modified: self.filter(pk=obj.pk).update(modified=update_modified) return obj
def get_file(self, storage, name, create=False, update_modified=None, check_cache_miss=False, **kwargs): kwargs.update(dict(storage_hash=utils.get_storage_hash(storage), name=name)) if create: if update_modified: defaults = kwargs.setdefault("defaults", {}) defaults["modified"] = update_modified try: obj, created = self.get_or_create(**kwargs) except: # hack - don't ask me why this happens... return None else: created = False kwargs.pop("defaults", None) try: manager = self._get_thumbnail_manager() obj = manager.get(**kwargs) except self.model.DoesNotExist: if check_cache_miss and storage.exists(name): # File already in storage, update cache obj = self.create(**kwargs) created = True else: return if update_modified and not created: if obj.modified != update_modified: self.filter(pk=obj.pk).update(modified=update_modified) return obj
def get_storage(self, storage): pickled = pickle.dumps(DEFAULT_THUMBNAIL_STORAGE) hash = utils.get_storage_hash(pickled) if hash not in self._storage_cache: self._storage_cache[hash] = self.get_or_create(hash=hash, defaults=dict(pickle=pickled))[0] return self._storage_cache[hash]
def database_get_image_dimensions(file, close=False, dimensions=None): """ Returns the (width, height) of an image, given ThumbnailFile. Set 'close' to True to close the file at the end if it is initially in an open state. Will attempt to get the dimensions from the file itself if they aren't in the db. """ storage_hash = utils.get_storage_hash(file.storage) dimensions = None dimensions_cache = None try: thumbnail = models.Thumbnail.objects.select_related('dimensions').get( storage_hash=storage_hash, name=file.name) except models.Thumbnail.DoesNotExist: thumbnail = None else: try: dimensions_cache = thumbnail.dimensions except models.ThumbnailDimensions.DoesNotExist: dimensions_cache = None if dimensions_cache: return dimensions_cache.width, dimensions_cache.height dimensions = get_image_dimensions(file, close=close) if settings.THUMBNAIL_CACHE_DIMENSIONS and thumbnail: # Using get_or_create in case dimensions were created # while running get_image_dimensions. models.ThumbnailDimensions.objects.get_or_create( thumbnail=thumbnail, defaults={'width': dimensions[0], 'height': dimensions[1]}) return dimensions
def get_file(self, storage, name, create=False, update_modified=None, **kwargs): kwargs.update(dict(storage_hash=utils.get_storage_hash(storage), name=name)) if create: if update_modified: defaults = kwargs.setdefault('defaults', {}) defaults['modified'] = update_modified try: object, created = self.get_or_create(**kwargs) except: # in case it was saved multiple times get first try: object = self.filter(**kwargs)[0] created = False except: return else: kwargs.pop('defaults', None) try: object = self.get(**kwargs) except: # in case it was saved multiple times get first try: object = self.filter(**kwargs)[0] except: return created = False if update_modified and object and not created: if object.modified != update_modified: self.filter(pk=object.pk).update(modified=update_modified) return object
def get_file(self, storage, name, create=False, update_modified=None, check_cache_miss=False, **kwargs): kwargs.update( dict(storage_hash=utils.get_storage_hash(storage), name=name)) if create: if update_modified: defaults = kwargs.setdefault('defaults', {}) defaults['modified'] = update_modified object, created = self.get_or_create(**kwargs) else: created = False kwargs.pop('defaults', None) try: object = self.get(**kwargs) except self.model.DoesNotExist: if check_cache_miss and storage.exists(name): # File already in storage, update cache object = self.create(**kwargs) created = True else: return if update_modified and object and not created: if object.modified != update_modified: self.filter(pk=object.pk).update(modified=update_modified) return object
def get_file(self, storage, name, create=False, update_modified=None, check_cache_miss=False, **kwargs): kwargs.update(dict(storage_hash=utils.get_storage_hash(storage), name=name)) if create: if update_modified: defaults = kwargs.setdefault('defaults', {}) defaults['modified'] = update_modified object, created = self.get_or_create(**kwargs) else: created = False kwargs.pop('defaults', None) try: object = self.get(**kwargs) except self.model.DoesNotExist: if check_cache_miss and storage.exists(name): # File already in storage, update cache object = self.create(**kwargs) created = True else: return if update_modified and object and not created: if object.modified != update_modified: self.filter(pk=object.pk).update(modified=update_modified) return object
def get_file(self, storage, name, create=False, update_modified=None, check_cache_miss=False, **kwargs): kwargs.update( dict(storage_hash=utils.get_storage_hash(storage), name=name)) if create: if update_modified: defaults = kwargs.setdefault('defaults', {}) defaults['modified'] = update_modified obj, created = self.get_or_create(**kwargs) else: created = False kwargs.pop('defaults', None) try: manager = self._get_thumbnail_manager() obj = manager.get(**kwargs) except self.model.DoesNotExist: if check_cache_miss and storage.exists(name): # File already in storage, update cache. Using # get_or_create again in case this was updated while # storage.exists was running. obj, created = self.get_or_create(**kwargs) else: return if update_modified and not created: if obj.modified != update_modified: self.filter(pk=obj.pk).update(modified=update_modified) return obj
def database_get_image_dimensions(file, close=False, dimensions=None): """ Returns the (width, height) of an image, given ThumbnailFile. Set 'close' to True to close the file at the end if it is initially in an open state. Will attempt to get the dimensions from the file itself if they aren't in the db. """ storage_hash = utils.get_storage_hash(file.storage) dimensions = None dimensions_cache = None try: thumbnail = models.Thumbnail.objects.select_related('dimensions').get( storage_hash=storage_hash, name=file.name) except models.Thumbnail.DoesNotExist: thumbnail = None else: try: dimensions_cache = thumbnail.dimensions except models.ThumbnailDimensions.DoesNotExist: dimensions_cache = None if dimensions_cache: return dimensions_cache.width, dimensions_cache.height dimensions = get_image_dimensions(file, close=close) if settings.THUMBNAIL_CACHE_DIMENSIONS and thumbnail: dimensions_cache = models.ThumbnailDimensions(thumbnail=thumbnail) dimensions_cache.width, dimensions_cache.height = dimensions dimensions_cache.save() return dimensions
def setUp(self): super().setUp() self.storage = test.TemporaryStorage() self.storage_hash = utils.get_storage_hash(self.storage) self.source = Source.objects.create(name='Test source', storage_hash=self.storage_hash) # Generate a test image, save it. self.filename = self.create_image(self.storage, 'test.jpg')
def setUp(self): super(FileManagerTest, self).setUp() self.storage = test.TemporaryStorage() self.storage_hash = utils.get_storage_hash(self.storage) self.source = Source.objects.create( name='Test source', storage_hash=self.storage_hash) # Generate a test image, save it. self.filename = self.create_image(self.storage, 'test.jpg')
def get_thumbnails(self, *args, **kwargs): """ Return an iterator which returns ThumbnailFile instances. """ # First, delete any related thumbnails. source_cache = self.get_source_cache() if source_cache: thumbnail_storage_hash = utils.get_storage_hash(self.thumbnail_storage) for thumbnail_cache in source_cache.thumbnails.all(): # Only iterate files which are stored using the current # thumbnail storage. if thumbnail_cache.storage_hash == thumbnail_storage_hash: yield ThumbnailFile(name=thumbnail_cache.name, storage=self.thumbnail_storage)
def get_storage(self, storage): pickled = pickle.dumps(storage) hash = utils.get_storage_hash(pickled) # Fix this buggy optimisation # the issue is that _storage_cache is not up to date in # some circonstances #if hash not in self._storage_cache: # self._storage_cache[hash] = self.get_or_create(hash=hash, # defaults=dict(pickle=pickled))[0] #return self._storage_cache[hash] return self.get_or_create(hash=hash, defaults=dict(pickle=pickled))[0]
def get_thumbnails(self, *args, **kwargs): """ Return an iterator which returns ThumbnailFile instances. """ # First, delete any related thumbnails. source_cache = self.get_source_cache() if source_cache: thumbnail_storage_hash = utils.get_storage_hash( self.thumbnail_storage) for thumbnail_cache in source_cache.thumbnails.all(): # Only iterate files which are stored using the current # thumbnail storage. if thumbnail_cache.storage_hash == thumbnail_storage_hash: yield ThumbnailFile(name=thumbnail_cache.name, storage=self.thumbnail_storage)
def setUp(self): super(FileManagerTest, self).setUp() self.storage = test_utils.TemporaryStorage() self.storage_hash = utils.get_storage_hash(self.storage) self.source = Source.objects.create( name='Test source', storage_hash=self.storage_hash) # Generate a test image, save it. data = StringIO() Image.new('RGB', (800, 600)).save(data, 'JPEG') data.seek(0) image_file = ContentFile(data.read()) self.filename = self.storage.save('test.jpg', image_file)
def delete(self, *args, **kwargs): """ Delete the image, along with any generated thumbnails. """ # First, delete any related thumbnails. source_cache = self.get_source_cache() if source_cache: thumbnail_storage_hash = utils.get_storage_hash(self.thumbnail_storage) for thumbnail_cache in source_cache.thumbnails.all(): # Only attempt to delete the file if it was stored using the # same storage as is currently used. if thumbnail_cache.storage_hash == thumbnail_storage_hash: self.thumbnail_storage.delete(thumbnail_cache.name) # Next, delete the source image. super(ThumbnailerFieldFile, self).delete(*args, **kwargs) # Finally, delete the source cache entry (which will also delete any # thumbnail cache entries). if source_cache: source_cache.delete()
def get_file(self, storage, name, create=False, update_modified=None, **kwargs): kwargs.update(dict(storage_hash=utils.get_storage_hash(storage), name=name)) if create: if update_modified: defaults = kwargs.setdefault('defaults', {}) defaults['modified'] = update_modified object, created = self.get_or_create(**kwargs) else: kwargs.pop('defaults', None) try: object = self.get(**kwargs) except self.model.DoesNotExist: return created = False if update_modified and object and not created: if object.modified != update_modified: self.filter(pk=object.pk).update(modified=update_modified) return object
def get_file(self, storage, name, create=False, update_modified=None, check_cache_miss=False, **kwargs): kwargs.update(dict(storage_hash=utils.get_storage_hash(storage), name=name)) if settings.THUMBNAIL_CACHE: cache = caches[settings.THUMBNAIL_CACHE] cache_key = self._get_cache_key(kwargs) if create: if update_modified: defaults = kwargs.setdefault('defaults', {}) defaults['modified'] = update_modified obj, created = self.get_or_create(**kwargs) else: created = False kwargs.pop('defaults', None) obj = None if settings.THUMBNAIL_CACHE: obj = cache.get(cache_key) if obj is None: try: manager = self._get_thumbnail_manager() obj = manager.get(**kwargs) except self.model.DoesNotExist: if check_cache_miss and storage.exists(name): # File already in storage, update cache. Using # get_or_create again in case this was updated while # storage.exists was running. obj, created = self.get_or_create(**kwargs) else: return if update_modified and not created: if obj.modified != update_modified: obj.modified = update_modified obj.save() if settings.THUMBNAIL_CACHE: cache.set(cache_key, obj, None) return obj
def delete_thumbnails(self, source_cache=None): """ Delete any thumbnails generated from the source image. :arg source_cache: An optional argument only used for optimisation where the source cache instance is already known. :returns: The number of files deleted. """ source_cache = self.get_source_cache() deleted = 0 if source_cache: thumbnail_storage_hash = utils.get_storage_hash(self.thumbnail_storage) for thumbnail_cache in source_cache.thumbnails.all(): # Only attempt to delete the file if it was stored using the # same storage as is currently used. if thumbnail_cache.storage_hash == thumbnail_storage_hash: self.thumbnail_storage.delete(thumbnail_cache.name) # Delete the cache thumbnail instance too. thumbnail_cache.delete() deleted += 1 return deleted
def delete(self, *args, **kwargs): """ Delete the image, along with any generated thumbnails. """ # First, delete any related thumbnails. source_cache = self.get_source_cache() if source_cache: thumbnail_storage_hash = utils.get_storage_hash( self.thumbnail_storage) for thumbnail_cache in source_cache.thumbnails.all(): # Only attempt to delete the file if it was stored using the # same storage as is currently used. if thumbnail_cache.storage_hash == thumbnail_storage_hash: self.thumbnail_storage.delete(thumbnail_cache.name) # Next, delete the source image. super(ThumbnailerFieldFile, self).delete(*args, **kwargs) # Finally, delete the source cache entry (which will also delete any # thumbnail cache entries). if source_cache: source_cache.delete()
def get_thumbnail_cache(self, thumbnail_name, create=False, update=False): if self.remote_source: return None thumbnail_cache_hash = utils.get_storage_hash(thumbnail_name) if self.thumbnail_cache.has_key(thumbnail_cache_hash): return self.thumbnail_cache[thumbnail_cache_hash] modtime = self.get_thumbnail_modtime(thumbnail_name) update_modified = modtime and utils.fromtimestamp(modtime) if update: update_modified = update_modified or utils.now() source = self.get_source_cache(create=True) thumbnail_cache = models.Thumbnail.objects.get_file( create=create, update_modified=update_modified, storage=self.thumbnail_storage, source=source, name=thumbnail_name, check_cache_miss=self.thumbnail_check_cache_miss) if thumbnail_cache: self.thumbnail_cache[thumbnail_cache_hash] = thumbnail_cache return thumbnail_cache
def delete_thumbnails(self, source_cache=None): """ Delete any thumbnails generated from the source image. :arg source_cache: An optional argument only used for optimisation where the source cache instance is already known. :returns: The number of files deleted. """ source_cache = self.get_source_cache() deleted = 0 if source_cache: thumbnail_storage_hash = utils.get_storage_hash( self.thumbnail_storage) for thumbnail_cache in source_cache.thumbnails.all(): # Only attempt to delete the file if it was stored using the # same storage as is currently used. if thumbnail_cache.storage_hash == thumbnail_storage_hash: self.thumbnail_storage.delete(thumbnail_cache.name) # Delete the cache thumbnail instance too. thumbnail_cache.delete() deleted += 1 return deleted
def delete_thumbnails(self, source_cache=None): """ Delete any thumbnails generated from the source image. :arg source_cache: An optional argument only used for optimisation where the source cache instance is already known. :returns: The number of files deleted. """ source_cache = self.get_source_cache() deleted = 0 if source_cache: thumbnail_storage_hash = utils.get_storage_hash( self.thumbnail_storage) thumbnails = source_cache.thumbnails.all() for thumbnail_cache in thumbnails: # Only attempt to delete the file if it was stored using the # same storage as is currently used. if thumbnail_cache.storage_hash == thumbnail_storage_hash: self.thumbnail_storage.delete(thumbnail_cache.name) deleted += 1 # Delete the thumbnails from the cache. pks = [thumb.pk for thumb in thumbnails] models.Thumbnail.objects.filter(pk__in=pks).delete() return deleted
def get_file(self, storage, name, create=False, update_modified=None, **kwargs): kwargs.update( dict(storage_hash=utils.get_storage_hash(storage), name=name)) if create: if update_modified: defaults = kwargs.setdefault('defaults', {}) defaults['modified'] = update_modified try: object, created = self.get_or_create(**kwargs) except: # in case it was saved multiple times get first try: object = self.filter(**kwargs)[0] created = False except: return else: kwargs.pop('defaults', None) try: object = self.get(**kwargs) except: # in case it was saved multiple times get first try: object = self.filter(**kwargs)[0] except: return created = False if update_modified and object and not created: if object.modified != update_modified: self.filter(pk=object.pk).update(modified=update_modified) return object
def save(self, *args, **kwargs): self.hash = utils.get_storage_hash(self.pickle) super(Storage, self).save(*args, **kwargs)
def save(self, *args, **kwargs): self.hash = utils.get_storage_hash(self.pickle) super(Storage, self).save()