mirror of
https://github.com/Hopiu/django-imagekit.git
synced 2026-04-24 23:14:42 +00:00
Extract useful backend utils
This commit is contained in:
parent
197dfb3485
commit
0fc29ee7cf
2 changed files with 40 additions and 29 deletions
|
|
@ -1,34 +1,12 @@
|
|||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.utils.importlib import import_module
|
||||
|
||||
from imagekit.imagecache.base import InvalidImageCacheBackendError, PessimisticImageCacheBackend, NonValidatingImageCacheBackend
|
||||
|
||||
_default_image_cache_backend = None
|
||||
from ..utils import get_singleton
|
||||
from .base import InvalidImageCacheBackendError, PessimisticImageCacheBackend, NonValidatingImageCacheBackend
|
||||
|
||||
|
||||
def get_default_image_cache_backend():
|
||||
"""
|
||||
Get the default image cache backend. Uses the same method as
|
||||
django.core.file.storage.get_storage_class
|
||||
Get the default validation backend.
|
||||
|
||||
"""
|
||||
global _default_image_cache_backend
|
||||
if not _default_image_cache_backend:
|
||||
from django.conf import settings
|
||||
import_path = settings.IMAGEKIT_DEFAULT_IMAGE_CACHE_BACKEND
|
||||
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
|
||||
from django.conf import settings
|
||||
return get_singleton(settings.IMAGEKIT_DEFAULT_IMAGE_CACHE_BACKEND,
|
||||
'validation backend')
|
||||
|
|
|
|||
|
|
@ -3,10 +3,12 @@ import mimetypes
|
|||
import sys
|
||||
import types
|
||||
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.core.files.base import ContentFile
|
||||
from django.db.models.loading import cache
|
||||
from django.utils.functional import wraps
|
||||
from django.utils.encoding import smart_str, smart_unicode
|
||||
from django.utils.functional import wraps
|
||||
from django.utils.importlib import import_module
|
||||
|
||||
from .lib import Image, ImageFile, StringIO
|
||||
|
||||
|
|
@ -371,3 +373,34 @@ def prepare_image(img, format):
|
|||
save_kwargs['optimize'] = True
|
||||
|
||||
return img, save_kwargs
|
||||
|
||||
|
||||
def get_class(path, desc):
|
||||
try:
|
||||
dot = path.rindex('.')
|
||||
except ValueError:
|
||||
raise ImproperlyConfigured("%s isn't a %s module." % (path, desc))
|
||||
module, classname = path[:dot], path[dot + 1:]
|
||||
try:
|
||||
mod = import_module(module)
|
||||
except ImportError, e:
|
||||
raise ImproperlyConfigured('Error importing %s module %s: "%s"' %
|
||||
(desc, module, e))
|
||||
try:
|
||||
cls = getattr(mod, classname)
|
||||
return cls
|
||||
except AttributeError:
|
||||
raise ImproperlyConfigured('%s module "%s" does not define a "%s"'
|
||||
' class.' % (desc[0].upper() + desc[1:], module, classname))
|
||||
|
||||
|
||||
_singletons = {}
|
||||
|
||||
|
||||
def get_singleton(class_path, desc):
|
||||
global _singletons
|
||||
cls = get_class(class_path, desc)
|
||||
instance = _singletons.get(cls)
|
||||
if not instance:
|
||||
instance = _singletons[cls] = cls()
|
||||
return instance
|
||||
|
|
|
|||
Loading…
Reference in a new issue