Beispiel #1
0
def image_resize(file_path, key_type, key_id):
    """
    key_type should be one of ImageKeyType.choices
    """

    if not file_path:
        return ''
    elif isinstance(file_path, FieldFile):
        file_path = file_path.path
    elif not isinstance(file_path, str):
        return ''

    key_type = key_type.upper()

    cache_key = get_cache_key(key_type, key_id)
    url = cache.get(cache_key)
    if url: return url

    try:
        image = Image.objects.only('file').get(key_type=key_type, key_id=key_id)
        if not os.path.exists(image.file.path):
            image.regenerate()
    except Image.DoesNotExist:
        image = Image(key_type=key_type, key_id=key_id)
        try:
            image.save(file_path)
        except IntegrityError:
            image = Image.objects.only('file').get(key_type=key_type, key_id=key_id)

    cache.set(cache_key, image.file.url, IMAGE_CACHE_TIMEOUT)
    return image.file.url
Beispiel #2
0
    def get(self, request, key_type, key_id):
        key_type = key_type.upper()
        if key_type not in ImageKeyType.choices_dict.keys():
            raise Http404

        try:
            key_id = base36_to_int(key_id)
        except ValueError:
            raise Http404

        image = get_object_or_404(Image, key_type=key_type, key_id=key_id)
        if not os.path.exists(image.file.path):
            image.regenerate()
            cache_key = get_cache_key(key_type, key_id)
            cache.set(cache_key, image.file.url)

        if DEBUG:
            return serve(request, image.file.url, document_root=BASE_DIR)

        return redirect(image.file.url)
Beispiel #3
0
def manga_post_save(instance, **kwargs):
    Image.objects.filter(key_type=ImageKeyType.ACCOUNT_AVATAR, key_id=instance.id).delete()
    cache.delete(get_cache_key(ImageKeyType.ACCOUNT_AVATAR, instance.id))