2012-03-05 16:47:19 +00:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class InvalidImageCacheBackendError(ImproperlyConfigured):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
2012-02-12 21:18:34 +00:00
|
|
|
class PessimisticImageCacheBackend(object):
|
2012-02-11 05:15:58 +00:00
|
|
|
"""
|
2012-02-12 21:18:34 +00:00
|
|
|
A very safe image cache backend. Guarantees that files will always be
|
2012-02-11 05:15:58 +00:00
|
|
|
available, but at the cost of hitting the storage backend.
|
|
|
|
|
|
|
|
|
|
"""
|
2012-02-02 03:37:58 +00:00
|
|
|
|
|
|
|
|
def is_invalid(self, file):
|
|
|
|
|
if not getattr(file, '_file', None):
|
|
|
|
|
# No file on object. Have to check storage.
|
|
|
|
|
return not file.storage.exists(file.name)
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
def validate(self, file):
|
|
|
|
|
"""
|
|
|
|
|
Generates a new image by running the processors on the source file.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
if self.is_invalid(file):
|
2012-02-03 14:16:50 +00:00
|
|
|
file.generate(save=True)
|
2012-02-02 03:37:58 +00:00
|
|
|
|
|
|
|
|
def invalidate(self, file):
|
|
|
|
|
file.delete(save=False)
|
|
|
|
|
|
2012-02-03 04:20:09 +00:00
|
|
|
def clear(self, file):
|
|
|
|
|
file.delete(save=False)
|
|
|
|
|
|
2012-02-02 03:37:58 +00:00
|
|
|
|
2012-02-12 21:18:34 +00:00
|
|
|
class NonValidatingImageCacheBackend(object):
|
2012-02-11 05:42:56 +00:00
|
|
|
"""
|
|
|
|
|
A backend that is super optimistic about the existence of spec files. It
|
|
|
|
|
will hit your file storage much less frequently than the pessimistic
|
|
|
|
|
backend, but it is technically possible for a cache file to be missing
|
|
|
|
|
after validation.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def validate(self, file):
|
|
|
|
|
"""
|
2012-02-12 21:18:34 +00:00
|
|
|
NonValidatingImageCacheBackend has faith, so validate's a no-op.
|
2012-02-11 05:42:56 +00:00
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def invalidate(self, file):
|
|
|
|
|
"""
|
|
|
|
|
Immediately generate a new spec file upon invalidation.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
file.generate(save=True)
|
|
|
|
|
|
|
|
|
|
def clear(self, file):
|
|
|
|
|
file.delete(save=False)
|