コード例 #1
0
ファイル: models.py プロジェクト: jkal/django-estates
 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]
コード例 #2
0
ファイル: models.py プロジェクト: joshdrake/easy-thumbnails
    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
コード例 #3
0
ファイル: models.py プロジェクト: alainwolf/openbroadcast.org
    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
コード例 #4
0
 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]
コード例 #5
0
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
コード例 #6
0
 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
コード例 #7
0
    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
コード例 #8
0
ファイル: models.py プロジェクト: frankk00/easy-thumbnails
    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
コード例 #9
0
    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
コード例 #10
0
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
コード例 #11
0
    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')
コード例 #12
0
ファイル: models.py プロジェクト: AL000/easy-thumbnails
    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')
コード例 #13
0
ファイル: files.py プロジェクト: vlinhart/easy-thumbnails
 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)
コード例 #14
0
ファイル: models.py プロジェクト: yml/easy-thumbnails
 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]
コード例 #15
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)
コード例 #16
0
ファイル: models.py プロジェクト: samluescher/easy-thumbnails
    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)
コード例 #17
0
ファイル: files.py プロジェクト: zodiak/django_ondev
 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()
コード例 #18
0
 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
コード例 #19
0
    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
コード例 #20
0
ファイル: files.py プロジェクト: vlinhart/easy-thumbnails
    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
コード例 #21
0
ファイル: files.py プロジェクト: marcusfreire0504/iellos
 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()
コード例 #22
0
ファイル: files.py プロジェクト: nuannuanwu/zhuyuan112
    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
コード例 #23
0
    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
コード例 #24
0
    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
コード例 #25
0
ファイル: files.py プロジェクト: robboyle/easy-thumbnails
    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
コード例 #26
0
 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
コード例 #27
0
 def save(self, *args, **kwargs):
     self.hash = utils.get_storage_hash(self.pickle)
     super(Storage, self).save(*args, **kwargs)
コード例 #28
0
ファイル: models.py プロジェクト: jkal/django-estates
 def save(self, *args, **kwargs):
     self.hash = utils.get_storage_hash(self.pickle)
     super(Storage, self).save()