mirror of
https://github.com/Hopiu/django-imagekit.git
synced 2026-04-25 07:24:42 +00:00
declare celery backends in a separate module
This commit is contained in:
parent
2cf425d8a3
commit
42c79d7bb2
4 changed files with 67 additions and 35 deletions
31
imagekit/imagecache/__init__.py
Normal file
31
imagekit/imagecache/__init__.py
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.utils.importlib import import_module
|
||||
|
||||
_default_image_cache_backend = None
|
||||
|
||||
|
||||
def get_default_image_cache_backend():
|
||||
"""
|
||||
Get the default image cache backend. Uses the same method as
|
||||
django.core.file.storage.get_storage_class
|
||||
|
||||
"""
|
||||
global _default_image_cache_backend
|
||||
if not _default_image_cache_backend:
|
||||
from ..settings import DEFAULT_IMAGE_CACHE_BACKEND as import_path
|
||||
try:
|
||||
dot = import_path.rindex('.')
|
||||
except ValueError:
|
||||
raise ImproperlyConfigured("%s isn't an image cache backend module." % \
|
||||
import_path)
|
||||
module, classname = import_path[:dot], import_path[dot + 1:]
|
||||
try:
|
||||
mod = import_module(module)
|
||||
except ImportError, e:
|
||||
raise ImproperlyConfigured('Error importing image cache backend module %s: "%s"' % (module, e))
|
||||
try:
|
||||
cls = getattr(mod, classname)
|
||||
_default_image_cache_backend = cls()
|
||||
except AttributeError:
|
||||
raise ImproperlyConfigured('Image cache backend module "%s" does not define a "%s" class.' % (module, classname))
|
||||
return _default_image_cache_backend
|
||||
|
|
@ -1,7 +1,3 @@
|
|||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.utils.importlib import import_module
|
||||
|
||||
|
||||
class PessimisticImageCacheBackend(object):
|
||||
"""
|
||||
A very safe image cache backend. Guarantees that files will always be
|
||||
|
|
@ -55,33 +51,3 @@ class NonValidatingImageCacheBackend(object):
|
|||
|
||||
def clear(self, file):
|
||||
file.delete(save=False)
|
||||
|
||||
|
||||
_default_image_cache_backend = None
|
||||
|
||||
|
||||
def get_default_image_cache_backend():
|
||||
"""
|
||||
Get the default image cache backend. Uses the same method as
|
||||
django.core.file.storage.get_storage_class
|
||||
|
||||
"""
|
||||
global _default_image_cache_backend
|
||||
if not _default_image_cache_backend:
|
||||
from .settings import DEFAULT_IMAGE_CACHE_BACKEND as import_path
|
||||
try:
|
||||
dot = import_path.rindex('.')
|
||||
except ValueError:
|
||||
raise ImproperlyConfigured("%s isn't an image cache backend module." % \
|
||||
import_path)
|
||||
module, classname = import_path[:dot], import_path[dot + 1:]
|
||||
try:
|
||||
mod = import_module(module)
|
||||
except ImportError, e:
|
||||
raise ImproperlyConfigured('Error importing image cache backend module %s: "%s"' % (module, e))
|
||||
try:
|
||||
cls = getattr(mod, classname)
|
||||
_default_image_cache_backend = cls()
|
||||
except AttributeError:
|
||||
raise ImproperlyConfigured('Image cache backend module "%s" does not define a "%s" class.' % (module, classname))
|
||||
return _default_image_cache_backend
|
||||
35
imagekit/imagecache/celery.py
Normal file
35
imagekit/imagecache/celery.py
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from celery.task import task
|
||||
|
||||
from imagekit.imagecache.base import PessimisticImageCacheBackend
|
||||
|
||||
|
||||
@task
|
||||
def generate(model, pk, attr):
|
||||
try:
|
||||
instance = model._default_manager.get(pk=pk)
|
||||
except model.DoesNotExist:
|
||||
pass # The model was deleted since the task was scheduled. NEVER MIND!
|
||||
else:
|
||||
getattr(instance, attr).generate(save=True)
|
||||
|
||||
|
||||
class CeleryCacheStateBackend(PessimisticImageCacheBackend):
|
||||
"""
|
||||
A pessimistic cache state backend that uses celery to generate its spec
|
||||
images. Like PessimisticCacheStateBackend, this one checks to see if the
|
||||
file exists on validation, so the storage is hit fairly frequently, but an
|
||||
image is guaranteed to exist. However, while validation guarantees the
|
||||
existence of *an* image, it does not necessarily guarantee that you will get
|
||||
the correct image, as the spec may be pending regeneration. In other words,
|
||||
while there are `generate` tasks in the queue, it is possible to get a
|
||||
stale spec image. The tradeoff is that calling `invalidate()` won't block
|
||||
to interact with file storage.
|
||||
|
||||
"""
|
||||
|
||||
def invalidate(self, file):
|
||||
generate.delay(file.instance.__class__, file.instance.pk, file.attname)
|
||||
|
||||
def clear(self, file):
|
||||
file.delete(save=False)
|
||||
|
|
@ -2,4 +2,4 @@ from django.conf import settings
|
|||
|
||||
DEFAULT_IMAGE_CACHE_BACKEND = getattr(settings,
|
||||
'IMAGEKIT_DEFAULT_IMAGE_CACHE_BACKEND',
|
||||
'imagekit.imagecache.PessimisticImageCacheBackend')
|
||||
'imagekit.imagecache.base.PessimisticImageCacheBackend')
|
||||
|
|
|
|||
Loading…
Reference in a new issue