mirror of
https://github.com/Hopiu/django-imagekit.git
synced 2026-03-16 21:30:23 +00:00
Error when attempting to generate image w/o source
This commit is contained in:
parent
61aa1c32e7
commit
92a3c2688c
3 changed files with 24 additions and 1 deletions
|
|
@ -13,6 +13,10 @@ class MissingGeneratorId(Exception):
|
|||
pass
|
||||
|
||||
|
||||
class MissingSource(ValueError):
|
||||
pass
|
||||
|
||||
|
||||
# Aliases for backwards compatibility
|
||||
UnknownExtensionError = UnknownExtension
|
||||
UnknownFormatError = UnknownFormat
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ from hashlib import md5
|
|||
import pickle
|
||||
from ..cachefiles.backends import get_default_cachefile_backend
|
||||
from ..cachefiles.strategies import StrategyWrapper
|
||||
from ..exceptions import MissingSource
|
||||
from ..processors import ProcessorPipeline
|
||||
from ..utils import open_image, img_to_fobj, get_by_qname
|
||||
from ..registry import generator_registry, register
|
||||
|
|
@ -41,6 +42,13 @@ class BaseImageSpec(object):
|
|||
def generate(self):
|
||||
raise NotImplementedError
|
||||
|
||||
MissingSource = MissingSource
|
||||
"""
|
||||
Raised when an operation requiring a source is attempted on a spec that has
|
||||
no source.
|
||||
|
||||
"""
|
||||
|
||||
|
||||
class ImageSpec(BaseImageSpec):
|
||||
"""
|
||||
|
|
@ -116,6 +124,10 @@ class ImageSpec(BaseImageSpec):
|
|||
])).hexdigest()
|
||||
|
||||
def generate(self):
|
||||
if not self.source:
|
||||
raise MissingSource("The spec '%s' has no source file associated"
|
||||
" with it." % self)
|
||||
|
||||
# TODO: Move into a generator base class
|
||||
# TODO: Factor out a generate_image function so you can create a generator and only override the PIL.Image creating part. (The tricky part is how to deal with original_format since generator base class won't have one.)
|
||||
img = open_image(self.source)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
from imagekit.cachefiles import ImageCacheFile
|
||||
from nose.tools import assert_false
|
||||
from nose.tools import assert_false, raises
|
||||
from .imagegenerators import TestSpec
|
||||
|
||||
|
||||
|
|
@ -10,3 +10,10 @@ def test_no_source():
|
|||
spec = TestSpec(source=None)
|
||||
file = ImageCacheFile(spec)
|
||||
assert_false(bool(file))
|
||||
|
||||
|
||||
@raises(TestSpec.MissingSource)
|
||||
def test_no_source_error():
|
||||
spec = TestSpec(source=None)
|
||||
file = ImageCacheFile(spec)
|
||||
file.generate()
|
||||
|
|
|
|||
Loading…
Reference in a new issue