Exemple #1
0
def fetch_thumbnails(images, sizes=None):
    """
    Regenerate EXISTING thumbnails, so we don't need to call redis when using
    thumbnails.get() or thumbnails.all(). Currently only support redis backend.
    NotImeplementedError will be raised, if backend is not supported
    """
    # NOTE: This is just working for redis based backend and same backend
    # different backend among thumbnails may results in bugs
    if not images:
        return

    backend = images[0].thumbnails.metadata_backend
    try:
        pipeline = backend.redis.pipeline()
    except AttributeError:
        raise NotImplementedError('Only Redis metadata backend is implemented')

    for image in images:
        thumbnails = image.thumbnails
        key = thumbnails.metadata_backend.get_thumbnail_key(
            thumbnails.source_image.name)

        if sizes:
            pipeline.hmget(key, sizes)
        else:
            pipeline.hgetall(key)

    # if sizes is provided results will be list of lists, else it will be list of dicts
    results = pipeline.execute()
    for image, data in zip(images, results):
        thumbnails = image.thumbnails
        source_name = thumbnails.source_image.name
        thumbnails._thumbnails = {}

        if sizes:
            # data shold be list, thus group it with its size beforehand
            items = zip(sizes, data)
        else:
            # data should be dict
            items = data.items()

        for size, name in items:
            if not name:
                continue
            image_meta = ImageMeta(source_name, name, size)
            thumbnails._thumbnails[compat.as_text(size)] = Thumbnail(
                image_meta, thumbnails.storage)
 def __init__(self, source_name, name, size):
     self.source_name = source_name
     self.name = compat.as_text(name)
     self.size = compat.as_text(size)
 def get_source(self, name):
     return compat.as_text(self.redis.hget(self.get_source_key(name), name))
 def get_thumbnail(self, source_name, size):
     name = compat.as_text(self.redis.hget(self.get_thumbnail_key(source_name), size))
     if name:
         return ImageMeta(source_name, name, size)
     return None
 def get_source(self, name):
     return compat.as_text(self.redis.hget(self.get_source_key(name), name))
 def get_thumbnail(self, source_name, size):
     name = compat.as_text(
         self.redis.hget(self.get_thumbnail_key(source_name), size))
     if name:
         return ImageMeta(source_name, name, size)
     return None